无编程基础求助: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脚本
- 右键资源库 → 新建 → Script → Behavior,命名为
CarouselImageBinder - 双击打开脚本,替换为以下代码:
// 绑定全屏显示的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」
- 在场景中选中
Image Carousel组件对象 - 在检查器面板找到「On Item Set Call」区域,点击
+号 - 将刚才创建的
CarouselImageBinder脚本拖到弹出的绑定框中 - 在函数选择下拉菜单中,选择
onCarouselItemSelected
4. 设置全屏Image与大图数组
- 在场景中添加一个
Screen Image(右键场景 → Add → UI → Screen Image),调整其大小为全屏覆盖屏幕 - 选中
Image Carousel对象,找到绑定的CarouselImageBinder脚本组件 - 将全屏
Screen Image拖到fullScreenImage变量的输入框中 - 点击
fullImageTextures数组的+号,依次把资源库中的全屏大图按轮播顺序拖入数组(索引0对应轮播第一个图标,以此类推)
5. 测试效果
点击预览按钮,滑动或点击轮播图标,屏幕上的大图会同步切换到对应内容。
内容的提问来源于stack exchange,提问作者Awais Baba
相关产品推荐
相关产品推荐

