scriptingsnapchat

Image Carousel component in SnapChat's Lens Studio


:)

I am not a coder & never did any scripting. I am working on a lens in Lens Studio that displays certain artworks on screen. I want to add a carousel so the users can go back and forth the index rather than just tapping in forward direction cycle. I downloaded a component from assets library called Image Carousel by Snap Inc. It allows me to add the icons for the carousel which I can scroll and tap but there is a script that is required and snapchat itself has not dedicated a page in their website that shows how to use this component. (I also posted the similar question in the their community forum no luck so far.)

When clicked on component in the resources panel a box with this text appears in the inspector panel: Image Carousel

Provides a carousel UI for the selection of one image between many. Requires a ScreenTransform. For a vertical orientation (or any other orientation) change the z rotation of your screen transform.

Image 1 Image 2

*=== EVENTS ===

Registering for the event from another script:

 script.carouselCC.onSelectionUpdate.add(function(i,tex) {...});

API

I have no idea which one to use or how.*

All I want is a way to connect the icons srceen images for example Image 1 with Screen Image 1, Image 2 with Screen Image 2, Image 3 with Screen Image 3, so that when I scroll and tap to Image 3 in carousel the Screen Image 3 appears on screen.

The image carousel component has a panel called "On Item Set Call" where you put the script but using Behavior script and then the function name. This area

Since I don't know what or how hard this is I might be asking alot for being a noob in this so I apologize in advance on the same hand I will be very very thankful in anyone spares some time helping me out with this.

Cheers!!!!

The Command are from SnapChat Lens Studio


Solution

  • You can use this component for what you want to achieve. The Carousel allows you to take the index of the main item, and pass it to a separate script that you create.

    You need to drag an active script component (from the objects panel) into the field 'Script' under the menu title 'On Item Set Call' and then expose a function from your script that is passed the index of the current carousel item (the one facing the front).

    In my example, I've created a script called 'UI Script' with this public function:

    // -----JS CODE-----
    //@input Component.Image[] items;
    
    script.setItem = function(index){
        for(var i = 0;i < script.items.length; i++){
            if(i == index){
                script.items[i].enabled = true;
            }
            else{
                script.items[i].enabled = false;
            }
        }
    }
    

    It enables a given image from an array of images corresponding with the index. Of course you can do different things with the script, for example taking an array of texture assets as an input and setting the texture of an image component based on the index.

    Hope this helps!

    the script component and function referenced in the carousel