标签:contain enable cal 需要 nta tco for input nbsp
最近有需求做一个排行榜,需要根据某一参数大小插入到排行榜中,但是要有一个插入后后续榜单元素下移的效果。代码如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class DynamicChange : MonoBehaviour { public GameObject container; public GameObject elem; public InputField input; public float deltaH = 110; public float movieTime = 0.5f; float deltaTime = 0.1f / 5; float movedDeltaH = 110 / 5; float speed = 3; public void Insert() { int location = int.Parse(input.text);//插入元素的位置 List<GameObject> childList = new List<GameObject>(); foreach (Transform t in container.transform) { childList.Add(t.gameObject); } Vector2 anchorPos = childList[location].GetComponent<RectTransform>().anchoredPosition;//插入元素的位置坐标 container.GetComponent<VerticalLayoutGroup>().enabled = false; GameObject eleInst = Instantiate(elem, container.transform); eleInst.GetComponent<Image>().color = Color.black; eleInst.transform.SetSiblingIndex(location); eleInst.GetComponent<RectTransform>().anchoredPosition = anchorPos;//实例化出插入的元素并赋值相关参数 //eleInst.GetComponent<RectTransform>().anchoredPosition = new Vector2(0,-deltaH*location); StartCoroutine(DoMoveMovie(childList.GetRange(location,childList.Count-location)));//后续元素下移效果 //eleInst.transform.SetSiblingIndex(location); } IEnumerator DoMoveMovie(List<GameObject> movedGo)//通过协程把瞬间位置的变化改为渐变的过程 { for(int i=0;i<5;i++) { foreach (GameObject go in movedGo) { Vector2 pos = go.GetComponent<RectTransform>().anchoredPosition; go.GetComponent<RectTransform>().anchoredPosition = new Vector2(pos.x, pos.y - movedDeltaH); } yield return new WaitForSeconds(speed*Time.deltaTime); } Destroy(movedGo[movedGo.Count - 1]); } }
标签:contain enable cal 需要 nta tco for input nbsp
原文地址:https://www.cnblogs.com/llstart-new0201/p/9472551.html