自定义 content 中显示个数,一次向content中间移动一个,支持缩放,支持透明度更改。注意content中的子物体需要是单数个。还有瑕疵,以后完善。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HandleMove : MonoBehaviour
{
public RectTransform content;
public Transform[] items;
public Vector2[] pos;
public float[] scaleBaseTime;
public float[] alphaBaseTime;
private Vector2 tempPos;
public AudioClip TempClip;
[Range(3, 10, order = 5)]
public int viewfieldCount = 5;
public float itemY = 0;
public float itemEmptyW = 0;
public int itemCount = 0;
public float itemWidth;
public float contentWidth;
public float screenWidth;
public int index = 10000;
public float speed = 1;
public bool scaleSize = false;
public bool changeAlpha = false;
public Transform toggleParent;
private void Start()
{
itemCount = content.childCount;
pos = new Vector2[itemCount];
items = new Transform[itemCount];
scaleBaseTime = new float[itemCount];
alphaBaseTime = new float[itemCount];
for (int i = 0; i < itemCount; i++)
{
items[i] = content.GetChild(i);
}
if (scaleSize)
{
for (int i = 0; i < itemCount; i++)
{
if (i < itemCount / 2)
{
scaleBaseTime[i] = scaleBaseTime[itemCount - 1 - i] = (float)i * 2 / (itemCount - 1);
}
else if (i == itemCount / 2)
{
scaleBaseTime[i] = 1;
}
}
}
else
{
for (int i = 0; i < itemCount; i++)
{
scaleBaseTime[i] = 1;
}
}
if (changeAlpha)
{
for (int i = 0; i < itemCount; i++)
{
if (i < itemCount / 2)
{
alphaBaseTime[i] = alphaBaseTime[itemCount - 1 - i] = (float)i * 2 / (itemCount - 1);
}
else if (i == itemCount / 2)
{
alphaBaseTime[i] = 1;
}
}
}
else
{
for (int i = 0; i < itemCount; i++)
{
alphaBaseTime[i] = 1;
}
}
contentWidth = content.rect.width;
itemWidth = items[0].GetComponent().rect.width;
screenWidth = Screen.width;
float gridWidth = screenWidth / viewfieldCount;
float gridEmptyWidth = gridWidth - itemWidth;
float realContentWidth = gridWidth * itemCount;
float outViewContentWidth = realContentWidth - screenWidth;
content.offsetMax = new Vector2(outViewContentWidth / 2, 0);
content.offsetMin = new Vector2(-outViewContentWidth / 2, 0);
for (int i = 0; i < itemCount; i++)
{
pos[i] = new Vector2(i * gridWidth + gridWidth / 2, itemY);
}
for (int i = 0; i < itemCount; i++)
{
int ind = Mathf.Abs((i + index) % itemCount);
items[i].GetComponent().SetSelfPos(pos[ind], scaleBaseTime[ind], alphaBaseTime[ind]);
}
if (toggleParent != null)
{
GameObject toggleOri = toggleParent.GetChild(0).gameObject;
for (int i = 1; i < itemCount; i++)
{
GameObject temp = Instantiate(toggleOri, toggleParent);
}
toggleOri.GetComponent().isOn = true;
}
}
public int toggleIndex = 9999;
public void MoveToggle(int dir)
{
toggleIndex += dir;
toggleParent.GetChild(toggleIndex % itemCount).GetComponent().isOn = true;
}
private void Update()
{
if (count > 0)
{
count -= Time.deltaTime;
return;
}
if (HLC_GeographyManager.Instance.inputManager.MoveLeft)
{
count = 0.5f;
if (TempClip != null)
{
HLC_GeographyManager.Instance.audioManager.PlayClickOneShot(TempClip);
}
Down();
MoveToggle(1);
}
else if (HLC_GeographyManager.Instance.inputManager.MoveRight)
{
count = 0.5f;
if (TempClip != null)
{
HLC_GeographyManager.Instance.audioManager.PlayClickOneShot(TempClip);
}
Up();
MoveToggle(-1);
}
}
float count = 1;
public void Up()
{
index++;
for (int i = 0; i < itemCount; i++)
{
int ind = Mathf.Abs((i + index) % itemCount);
tempPos = pos[ind];
items[i].GetComponent().SetSelfPos(tempPos, speed, scaleBaseTime[ind], alphaBaseTime[ind]);
}
}
public void Down()
{
index--;
for (int i = itemCount - 1; i >= 0; i--)
{
int ind = Mathf.Abs((i + index) % itemCount);
tempPos = pos[ind];
items[i].GetComponent().SetSelfPos(tempPos, speed, scaleBaseTime[ind], alphaBaseTime[ind]);
}
}
}
SelfItem代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SelfItem : MonoBehaviour
{
public RectTransform mTrans;
public Vector2 targetPos;
public Vector3 targetScale = Vector3.one;
public CanvasGroup cg;
public GameObject grid;
public BoxCollider2D myCollider;
private void Awake()
{
mTrans = GetComponent();
grid = transform.Find("Grid").gameObject;
cg = GetComponent();
myCollider = GetComponent();
myCollider.enabled = false;
}
public bool moveing = false;
public float moveSpeed = 0;
public float scaleBase = 0;
public float alpha = 0;
private float dis = 0;
private void Update()
{
if (moveing)
{
dis = Vector2.Distance(mTrans.anchoredPosition, targetPos);
if (dis < 960 && Vector2.Distance(mTrans.anchoredPosition, targetPos) > 2)
{
mTrans.anchoredPosition = Vector2.MoveTowards(mTrans.anchoredPosition, targetPos, moveSpeed);
mTrans.localScale = Vector3.Lerp(mTrans.localScale, targetScale, Time.deltaTime * moveSpeed * 0.75f);
cg.alpha = cg.alpha == alpha ? alpha : cg.alpha > alpha ? cg.alpha -= Time.deltaTime * moveSpeed * 0.25f : cg.alpha += Time.deltaTime * moveSpeed * 0.25f;
}
else
{
mTrans.anchoredPosition = targetPos;
mTrans.localScale = targetScale;
moveing = false;
cg.alpha = alpha;
if (mTrans.localScale.x == 1 && alpha == 1)
{
grid.SetActive(true);
myCollider.enabled = true;
}
else if (grid.activeInHierarchy)
{
grid.SetActive(false);
myCollider.enabled = false;
}
}
}
}
public void SetSelfPos(Vector2 pos, float speed, float scaleBase = 1, float alphaBase = 1)
{
targetPos = pos;
moveSpeed = speed;
targetScale = Vector3.one * scaleBase;
alpha = alphaBase;
moveing = true;
}
public void SetSelfPos(Vector2 pos, float scaleBase = 1, float alphaBase = 1)
{
targetPos = pos;
alpha = alphaBase;
mTrans.anchoredPosition = targetPos;
mTrans.localScale = Vector3.one * scaleBase;
cg.alpha = alphaBase;
if (scaleBase == 1 && alphaBase == 1)
{
grid.SetActive(true);
myCollider.enabled = true;
}
else if (grid.activeInHierarchy)
{
grid.SetActive(false);
myCollider.enabled = false;
}
}
}