排行榜UI列表

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Pai_hang_manager : MonoBehaviour
{
    public ScrollRect scrollRect;
    public RectTransform content;
    public Dropdown dropdown;
    public float grid_dis;
    public float time_add=1;
    [Header("prefab width=150")]
    public GameObject item;
    
    List<People_paihang> people_Paihangs_new = new List<People_paihang>();
    // Start is called before the first frame update
    void Start()
    {
        List<People_paihang> people_Paihangs = new List<People_paihang>();
        people_Paihangs.Add(new People_paihang("小李", 59, 1200));
        people_Paihangs.Add(new People_paihang("小李2", 58, 1260));
        people_Paihangs.Add(new People_paihang("小fang", 12, 1290));
        people_Paihangs.Add(new People_paihang("小fan", 57, 124700));
        people_Paihangs.Add(new People_paihang("小金", 79, 12700));
       
        content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, people_Paihangs.Count * (150 + grid_dis - 1));
        scrollRect.verticalNormalizedPosition = 1;
        Swpan(people_Paihangs);
    }
    void Swpan(List<People_paihang> people_Paihangs)
    {
        people_Paihangs_new.Clear();
        for (int i = 0; i < people_Paihangs.Count; i++)
        {
            GameObject gameObject =Instantiate(item, content.transform);
           
            People_paihang people_Paihang= gameObject.GetComponent<People_paihang>();
            people_Paihang.Name = people_Paihangs[i].Name;
            people_Paihang.Score = people_Paihangs[i].Score;
            people_Paihang.Harm = people_Paihangs[i].Harm;
         
            people_Paihang.Set_();
            people_Paihangs_new.Add(people_Paihang);
        }
        dropdown.onValueChanged.AddListener((int number) =>
        {
            if (number == 0)
            {
                Chose_score();
            }
            else
            {
                Chose_harm();
            }

        });
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F1))
        {
            Chose_score();
        }
        if (Input.GetKeyDown(KeyCode.F2))
        {
            Chose_harm();
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Change_people();
        }
    }
    /// <summary>
    /// 改变属性
    /// </summary>
    void Change_people()
    {
        Get_from_name("小fan").Score=1;
        Get_from_name("小fan").Set_();
        Chose_score();
    }
    /// <summary>
    /// 按分数排序
    /// </summary>
    void Chose_score()
    {
        people_Paihangs_new.Sort();
        for (int i = 0; i < people_Paihangs_new.Count; i++)
        {
           
            people_Paihangs_new[i].Move(i, grid_dis, time_add);
        }
    }
    /// <summary>
    /// 按伤害排序
    /// </summary>
    void Chose_harm()
    {
        people_Paihangs_new.Sort(new HarmComper());
        for (int i = 0; i < people_Paihangs_new.Count; i++)
        {
            people_Paihangs_new[i].Move(i, grid_dis, time_add);
        }
    }
    public People_paihang Get_from_name(string name)
    {
        People_paihang people_;
        for (int i = 0; i < people_Paihangs_new.Count; i++)
        {
            if(people_Paihangs_new[i].Name== name)
            {
                people_ = people_Paihangs_new[i];
                return people_;
            }
        }
        return null;
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class People_paihang : MonoBehaviour,IComparable<People_paihang>
{
   
    public string Name;
  
    public int Score;
 
    public float Harm;
   
    RectTransform rectTransform;
    public People_paihang()
    {
       
    }
    public People_paihang(string name,int score,float harm)
    {
        Name = name;
        Score = score;
        Harm = harm;
    }
    void Start()
    {
        rectTransform = transform.GetComponent<RectTransform>();
    }

    public int CompareTo(People_paihang other)
    {
        return -Score.CompareTo(other.Score);
    }
    public void Set_()
    {
        transform.GetChild(0).GetComponent<Text>().text = Name;
        transform.GetChild(1).GetComponent<Text>().text = Score.ToString();
        transform.GetChild(2).GetComponent<Text>().text = Harm.ToString();
    }
    bool stop;

    public int current_index;
    float offet_get;
    int grid_dis_number;
    float time_current;
    public void Move(int index_current,float offet,float time)
    {
        time_current = time;
        if (current_index != index_current)
        {

            grid_dis_number =Mathf.Abs(index_current - current_index);
            current_index = index_current;
            offet_get = offet;
            stop = true;
        }
    }
    void Update()
    {
        if (stop)
        {
            rectTransform.anchoredPosition = Vector2.Lerp(rectTransform.anchoredPosition, new Vector2(rectTransform.anchoredPosition.x, -150 * current_index + -offet_get * current_index), grid_dis_number * Time.deltaTime* time_current);
            if (Vector3.Distance(rectTransform.anchoredPosition, new Vector2(rectTransform.anchoredPosition.x, -150 * current_index + -offet_get * current_index)) < 0.01f)
            {
                stop = false;
            }
        }
    }


}
class HarmComper : IComparer<People_paihang>
{
    public int Compare(People_paihang x, People_paihang y)
    {
        return -x.Harm.CompareTo(y.Harm);
    }
}

猜你喜欢

转载自blog.csdn.net/fanfan_hongyun/article/details/129025110