Unity 3D迴圈滾動效果
阿新 • • 發佈:2018-12-26
然後通過SetDepthAndPosition這個方法,實現圖片的空間空間展開
Z軸和Y軸,係數是一樣的
經過上面設定,空間就擺開了
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SelectRole : MonoBehaviour {
public GameObject rolesObj;
private int _half = 0;//一側的卡片數
private int _movX = 150 ;//X軸移動距離
private int _movY = 50;//Y軸移動距離
private int _movZ = 60;//Z軸移動距離
private int count = 3;//元件數
private List<RoleItem> _roleList = new List<RoleItem>();
// Use this for initialization
void Start () {
//載入圖片
Object[] textureList = (Object[])Resources.LoadAll("Pictures" );
int maxDepth = textureList.Length % 2 == 1 ? textureList.Length / 2 + 1 : textureList.Length / 2;//最大深度
_half = maxDepth;
for (int i = 0; i < textureList.Length; i++)
{
//載入角色圖片預設
GameObject role = Instantiate(Resources.Load("Role" , typeof(GameObject))) as GameObject;
role.transform.parent = rolesObj.transform;
role.transform.localScale = Vector3.one;
EventDelegate.Add(role.GetComponent<UIToggle>().onChange , RoleToggleChange);
RoleItem item = role.GetComponent<RoleItem>();
item.texture.mainTexture = textureList[i] as Texture;
//設定角色卡片排序命名
role.name = maxDepth.ToString();
if (i > 0)
{
//奇數設定為右邊,下標為正數
if (i % 2 == 1)
{
maxDepth--;
role.name = maxDepth.ToString();
}
//偶數設定為左邊,下標為負數
else
{
role.name = "-" + maxDepth.ToString();
}
}
SetDepthAndPosition(item,0,0);
_roleList.Add(item);
}
}
private void SetDepthAndPosition(RoleItem role,int dir,int index)
{
int indexDepth = 0;
//左右移動後,重新排序命名
if (dir != 0)
{
if (index*dir > _half )
indexDepth = -dir * (_half - 1);
else
indexDepth = index > -1 && index < 1 ? dir : index;
role.name = indexDepth.ToString();
}
else
{
indexDepth = int.Parse(role.name);
}
TweenPosition tp = role.GetComponent<TweenPosition>();
int x = indexDepth < 0 ? -(_half + indexDepth) * _movX : (_half - indexDepth) * _movX;
indexDepth = System.Math.Abs(indexDepth);
tp.to = new Vector3(x, (_half - indexDepth) * _movY, (_half - indexDepth) * _movZ);
role.bg.depth = count * indexDepth;
role.active.depth = 1 + count * indexDepth;
role.texture.depth = 2 + count * indexDepth;
role.GetComponent<UIToggle>().value = indexDepth == _half ? true:false;
tp.PlayForward();
}
/// <summary>
/// 左邊
/// </summary>
public void LeftClick()
{
//重新排列順序
foreach (RoleItem role in _roleList)
{
int index = int.Parse(role.name);
print(index);
SetDepthAndPosition(role,1,++index);
}
}
/// <summary>
/// 右邊
/// </summary>
public void RightClick()
{
//重新排列順序
foreach (RoleItem role in _roleList)
{
int index = int.Parse(role.name);
SetDepthAndPosition(role,-1,--index);
}
}
/// <summary>
/// 滑鼠選中某個角色
/// </summary>
public void RoleToggleChange()
{
if(UIToggle.current.value)
{
int index = int.Parse(UIToggle.current.name);
int moveCount = _half - System.Math.Abs(index);//移動個數
for (int i = 0; i < moveCount;i++ )
{
if (index > 0)
LeftClick();
else
RightClick();
}
}
}
}
FR:海濤高軟(hunk Xu)