最近有需求做一个排行榜,需要根据某一参数大小插入到排行榜中,但是要有一个插入后后续榜单元素下移的效果。代码如下:
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]); } }
排行榜单行时插入(或者原来排名升高)效果
int CatchLocation(InitSettingsData newFace) { int location = 10; int locationOri = 9; for(int i=0;i<faceList.Count;i++) { if(newFace.score>faceList[faceList.Count-1-i].score { location = faceList.Count - 1 - i; } if(newFace.faceID== faceList[i].faceID) { locationOri = i; } } if (location > numFaces - 1) { isSorting = false; return 0; } if(locationOri<location) { isSorting = false; return 0; } UIChange(newFace, location, locationOri); faceList.RemoveAt(locationOri); faceList.Insert(location, newFace); faceList = faceList.GetRange(0, numFaces); return location; } void UIChange(InitSettingsData newFace, int location, int locationOri) { List<GameObject> facesGoList = new List<GameObject>(); foreach (Transform t in faceContainer.transform) { facesGoList.Add(t.gameObject); } Vector2 newAnchorPos = facesGoList[location].GetComponent<RectTransform>().anchoredPosition; faceContainer.GetComponent<VerticalLayoutGroup>().enabled = false; Debug.Log("Vertical LyOut set false"); GameObject newFaceGo = Instantiate(faceElem, faceContainer.transform); newFaceGo.GetComponent<FaceElem>().SetFaceElem(newFace,false); newFaceGo.GetComponent<RectTransform>().anchoredPosition = newAnchorPos; newFaceGo.transform.SetSiblingIndex(location); StartCoroutine(DoMoveMovie(facesGoList.GetRange(location, locationOri - location+1))); } 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]); isSorting = false; //Debug.Log("sort end:" + isSorting); }
排行榜两行显示时插入(或者原来排名升高)效果
int CatchLocation(InitSettings.FaceData newFace) { int location = 10; int locationOri = 7; for (int i=0;i<faceList.Count;i++) { if(newFace.score>=faceList[faceList.Count-1-i].score) { location = faceList.Count - 1 - i; } if (newFace.faceID == faceList[i].faceID) { locationOri = i; } } if (location > numFaces - 1) { isSorting = false; return 0; } if (locationOri < location) { isSorting = false; return 0; } UIChange(newFace, location, locationOri); //faceList.Insert(location, newFace); faceList.RemoveAt(locationOri); faceList.Insert(location, newFace); faceList = faceList.GetRange(0, numFaces); Debug.Log("Face location:" + location); return location; } IEnumerator DoMoveMovie(List<GameObject> movedGo, int posOri, int posDest) { if (posDest < 4 && posOri >= 4) { Vector2 pos = movedGo[3-posDest].GetComponent<RectTransform>().anchoredPosition; movedGo[3 - posDest].GetComponent<RectTransform>().anchoredPosition = new Vector2(-yColumnSpace, -xLineSpace); } 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 + movedDelta, pos.y); } yield return new WaitForSeconds(speed * Time.deltaTime); } Destroy(movedGo[movedGo.Count - 1]); isSorting = false; } void UIChange(InitSettings.FaceData newFace, int location, int locationOri) { List<GameObject> facesGoList = new List<GameObject>(); foreach (Transform t in faceContainer.transform) { facesGoList.Add(t.gameObject); } Vector2 newAnchorPos = facesGoList[location].GetComponent<RectTransform>().anchoredPosition; GameObject newFaceGo = Instantiate(faceElem, faceContainer.transform); newFaceGo.GetComponent<FaceElem>().SetFaceElem(newFace,false); newFaceGo.GetComponent<RectTransform>().anchoredPosition = newAnchorPos; newFaceGo.transform.SetSiblingIndex(location); StartCoroutine(DoMoveMovie(facesGoList.GetRange(location, locationOri - location+1),locationOri,location)); }
PS:
插入的元素不要实例化出来,直接把原有的元素更改属性,然后移动上去性能会更好