带动态插入效果的排行榜
Posted llstart-new0201
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带动态插入效果的排行榜相关的知识,希望对你有一定的参考价值。
最近有需求做一个排行榜,需要根据某一参数大小插入到排行榜中,但是要有一个插入后后续榜单元素下移的效果。代码如下:
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]); } }
以上是关于带动态插入效果的排行榜的主要内容,如果未能解决你的问题,请参考以下文章