You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无编程基础求助:Snapchat Lens Studio Image Carousel组件使用

Snapchat Lens Studio Image Carousel组件使用指南(绑定轮播图标与全屏大图)

问题背景

本人无编程经验,正在Snapchat Lens Studio中制作展示艺术作品的镜头,已使用官方提供的Image Carousel组件实现轮播图标滑动/点击,但需要实现点击轮播图标时,屏幕同步显示对应全屏大图,不清楚脚本用法及「On Item Set Call」面板的操作方法。


组件官方说明

Image Carousel
提供用于在多张图片中选择的轮播UI,需搭配ScreenTransform使用。若需垂直或其他方向,可修改ScreenTransform的Z轴旋转角度。

核心事件

  • onSelectionUpdate(i,tex):当轮播选中项更新时触发,i为选中项目的索引,tex为选中图标的纹理
  • 注册事件代码示例:
    script.carouselCC.onSelectionUpdate.add(function(i,tex) {...});
    

常用API(按需使用)

  • getSelected():返回当前选中项目的索引(空轮播返回-1)
  • setSelected(int index, bool animate):跳转到指定索引项目,animate为true时带过渡动画
  • add(Texture image):向轮播末尾添加图片,返回新索引
  • getSelectedImage():返回当前选中图标的纹理

具体操作步骤(无编程经验友好版)

1. 准备资源

将轮播用的小图标和对应的全屏大图全部导入Lens Studio资源库,建议按顺序命名(如Thumb_0、Full_0,Thumb_1、Full_1),方便对应索引。

2. 创建Behavior脚本

  1. 右键资源库 → 新建 → Script → Behavior,命名为CarouselImageBinder
  2. 双击打开脚本,替换为以下代码:
    // 绑定全屏显示的Screen Image对象
    @input Component.ScreenImage fullScreenImage
    // 绑定所有全屏大图的纹理数组(按轮播顺序排列)
    @input Asset.Texture[] fullImageTextures = []
    
    function onCarouselItemSelected(index, thumbTexture) {
        // 检查索引是否在数组范围内
        if (index >= 0 && index < fullImageTextures.length) {
            // 将对应索引的大图纹理设置到全屏Image组件
            fullScreenImage.mainPass.baseTex = fullImageTextures[index];
        }
    }
    
    // 注册轮播选中更新事件
    script.createEvent("OnStartEvent").bind(function() {
        // 获取Image Carousel组件
        var carousel = script.getSceneObject().getComponent("Component.ImageCarousel");
        if (carousel) {
            carousel.onSelectionUpdate.add(onCarouselItemSelected);
        }
    });
    

3. 绑定脚本与「On Item Set Call」

  1. 在场景中选中Image Carousel组件对象
  2. 在检查器面板找到「On Item Set Call」区域,点击+号
  3. 将刚才创建的CarouselImageBinder脚本拖到弹出的绑定框中
  4. 在函数选择下拉菜单中,选择onCarouselItemSelected

4. 设置全屏Image与大图数组

  1. 在场景中添加一个Screen Image(右键场景 → Add → UI → Screen Image),调整其大小为全屏覆盖屏幕
  2. 选中Image Carousel对象,找到绑定的CarouselImageBinder脚本组件
  3. 将全屏Screen Image拖到fullScreenImage变量的输入框中
  4. 点击fullImageTextures数组的+号,依次把资源库中的全屏大图按轮播顺序拖入数组(索引0对应轮播第一个图标,以此类推)

5. 测试效果

点击预览按钮,滑动或点击轮播图标,屏幕上的大图会同步切换到对应内容。


内容的提问来源于stack exchange,提问作者Awais Baba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 01:25:09