Unity UGUI实现卡片椭圆方向滚动实现卡片椭圆方向滚动
主要为大家详细介绍了UGUI实现卡片椭圆方向滚动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了UGUI实现卡片椭圆方向滚动的具体代码,供大家参考,具体内容如下
搭建简单的场景
运行效果
卡片移动动画通过插件DoTween实现
控制脚本:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using DG.Tweening;
public class CardMove : MonoBehaviour {
GameObject[] sprites;
int halfSize;
Vector2 screenCenterPos;
public float startAngle;//中间卡牌的角度
public float deltaAngle;//相邻卡牌的角度差值
public float moveSpeed;//移动动画的速度
public Vector3 center;//椭圆中心点
public float A = 1;//long axis
public float B = 1;//short axis
int cardcount;
// Use this for initialization
void Start () {
init ();
}
// Update is called once per frame
void Update () {
}
/// <summary>
/// 初始化卡牌显示位置
/// </summary>
void init(){
screenCenterPos = new Vector2 (Screen.width*0.5f,Screen.height*0.5f);
cardcount = transform.childCount;
halfSize = (cardcount - 1) / 2;
sprites=new GameObject[cardcount];
for (int i = 0; i < cardcount; i++) {
sprites [i] = transform.GetChild (i).gameObject;
setPosition (i,false);
setDeeps (i);
}
}
/// <summary>
/// 椭圆的半长轴为A,半短轴为B,计算椭圆上一点的位置
/// x=A*cos(angle),y=B*sin(angle)
/// </summary>
/// <param name="index">Index.</param>
/// <param name="userTweener">是否使用tween动画.</param>
void setPosition(int index,bool userTweener=true){
//计算每一张卡片在椭圆上相对中间卡牌的角度
float angle = 0;
if(index<halfSize){//left
angle=startAngle-(halfSize-index)*deltaAngle;
}else if(index>halfSize){//right
angle = startAngle + (index - halfSize) * deltaAngle;
}else{//medim
angle=startAngle;