码迷,mamicode.com
首页 > 其他好文 > 详细

带动态插入效果的排行榜

时间:2018-08-14 11:26:30      阅读:163      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!