本文实例为大家分享了UGUI实现卡片椭圆方向滚动的具体代码,供大家参考,具体内容如下
搭建简单的场景
运行效果
卡片移动动画通过插件DoTween实现
控制脚本:
使用UnityEngine; 使用System.Collections; 使用UnityEngine.UI; 使用DG.Tweening; 公共类CardMove: MonoBehaviour { GameObject[]精灵; int halfSize; Vector2 screenCenterPos; 公共浮动startAngle;//中间卡牌的角度 公共浮动deltaAngle;//相邻卡牌的角度差值 公共漂浮移动速度;//移动动画的速度 公共Vector3中心;//椭圆中心点 公开上市=1;//长轴 公共浮动B=1;//短轴 int cardcount;//用它来初始化 无效的Start () { init (); }//更新called>指数。你们;/param>///& lt;参数name=" userTweener祝辞是否使用渐变动画。;/param> 空白setPosition (int指数,bool userTweener=true) {//计算每一张卡片在椭圆上相对中间卡牌的角度 浮动角=0; 如果(indexhalfSize){//右 角=startAngle +(指数- halfSize) * deltaAngle; }{//medim 角=startAngle; }//通过卡牌的角度,计算对应的位置 漂浮xpos=* Mathf.Cos((角/180)* Mathf.PI);//+ center.x; 浮动ypos=B * Mathf.Sin((角/180)* Mathf.PI);//+ center.y; 调试。日志(“指数=" +指数+”,xpos=" + xpos +”, ypos=" + ypos); Vector2 pos=new Vector2 (xpos ypos);//调试。日志(“screenPos=" + screenPos +”, wordPos=" + wordPos);//通过doTween控制卡片移动动画 如果(! userTweener) { 精灵(指数).GetComponent ().rectTransform。DOMove(新Vector2 (screenCenterPos.x + pos.x screenCenterPos.y + pos.y), 0 f); 其他} 精灵(指数).GetComponent ().rectTransform。DOMove(新Vector2 (screenCenterPos.x + pos.x screenCenterPos.y + pos.y), 1 f); }///& lt; summary>///计算每一张卡片的层级///& lt;/summary>///& lt;参数name="指数”在指数。;/param> 空白setDeeps (int指数){ int深=0; 如果(指数& lt;halfSize){//左侧卡牌层级,从左侧到中间,层级依此递增 深=指数; }else if(深比;halfSize){//右侧卡牌层级,从中间到右侧,层级依此递减 深=sprites.Length -(指数+ 1); 其他}{ 深=halfSize; } 精灵(指数).GetComponent ().SetSiblingIndex(深); }///& lt; summary>///左侧按钮点击,向左移动///& lt;/summary> 公共空间OnLeftBtnClick () { int长度=sprites.Length; GameObject temp=精灵[0]; for (int i=0;我& lt;长度;我+ +){//移动卡片在数组中的位置,依此向前移动一位 如果长度(i==- 1) 精灵[我]=temp; 其他的 精灵[我]=精灵(i + 1); } for (int i=0;我& lt;长度;我+ +){//跟新数组卡片需要显示的位置和层级 setPosition(我); setDeeps(我); } }///& lt; summary>///右侧按钮点击,向右移动///& lt;/summary> 公共空间RightBtnClick () { int长度=sprites.Length; GameObject temp=精灵(长度是1); for (int i=长度是1;我在=0;我(){ 如果(i==0) 精灵[我]=temp; 其他的 精灵[我]=精灵(i - 1); } for (int i=0;我& lt;长度;我+ +){ setPosition(我); setDeeps(我); } } } >之前 源码下载:地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
统一UGUI实现卡片椭圆方向滚动