赞
踩
要求:实现如图所示UI界面,按下某一按键实现排行榜的更新
M:
-
- //数据模型类 主要负责数据(玩家的数据)
- public class RankModel
- {
- //定义三个属性 姓名、分数、排名
- //定义构造函数
- public string Name{
- set;
- get;
- }
-
- public int Score{
- set;
- get;
- }
-
- public int Number{
- set;
- get;
- }
-
- public RankModel(string name,int score,int number){
- Name = name;
- Score = score;
- Number = number;
- }
-
- }
V:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- //排行榜视图类:负责UI数据的显示,向外界提供UI数据初始化的方法
- public class RankElementSc : MonoBehaviour
- {
- private Text numberText;//显示排名的子物体的text
- private Text nameText;//显示名字的子物体的text
- private Text scoreText;//显示分数的子物体的text
-
- void Awake()
- {
- numberText = transform.Find("NumberText").GetComponent<Text>();
- nameText = transform.Find("NameText").GetComponent<Text>();
- scoreText = transform.Find("ScoreText").GetComponent<Text>();
- }
-
- //UI初始化
- public void Init(RankModel rank){
- Debug.Log(numberText + "" + rank.Number);
- numberText.text = rank.Number.ToString();
- nameText.text = rank.Name;
- scoreText.text = rank.Score.ToString();
- }
-
- }
C:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- //排行榜控制类,负责数据的处理,游戏的逻辑
- public class RankController : MonoBehaviour
- {
- public GameObject rankElementPr;//排行榜元素预设体
- List<RankModel> rankModels;//数据的集合
- List<RankElementSc> elementScs;//单元格脚本的集合
- public int RANKNUMBER = 5;//显示排名的数量
- RankModel[] top5Arr;//存放前5名+1的数据
- private string playerData;
- private void Awake()
- {
- rankModels = new List<RankModel>();//初始化玩家列表
- elementScs = new List<RankElementSc>();//初始化单元格脚本列表
- top5Arr = new RankModel[RANKNUMBER + 1];//初始化前5名玩家数组
- }
- void Start()
- {
- LoadData();//加载初始数据
- SortRankdataOrScore();//对所有玩家进行排序
- //DisplayData();//更新所有玩家数据
- DisplayTop5Data();//更新前5名数据
- }
-
- //加载初始数据
- private void LoadData()
- {
- //创建一个数据
- //RankModel m1 = new RankModel("孙悟空", 99, 0);
- //rankModels.Add(m1);
- //RankModel m2 = new RankModel("李寻欢", 66, 0);
- //rankModels.Add(m2);
- //RankModel m3 = new RankModel("猪八戒", 70, 0);
- //rankModels.Add(m3);
- //RankModel m4 = new RankModel("沙僧", 89, 0);
- //rankModels.Add(m4);
- //RankModel m5 = new RankModel("傻吊", 30, 0);
- //rankModels.Add(m5);
- //RankModel m6 = new RankModel("测试玩家", 0, 0);
- //rankModels.Add(m6);
- //PlayerPrefs.DeleteAll();
- //初始数据
- //if (!PlayerPrefs.HasKey("RankData"))
- //{
- // playerData = "孙悟空 99 0\n李寻欢 66 0\n猪八戒 70 0\n沙僧 89 0\n傻吊 30 0\n测试玩家 0 0";
- // PlayerPrefs.SetString("RankData", playerData);
- //}
- 解析数据
- //string Datas = PlayerPrefs.GetString("RankData");
- //string[] playerdatas = Datas.Split('\n');
- //foreach (string item in playerdatas)
- //{
- // string[] playerdata = item.Split(' ');
- // string modelName = playerdata[0];
- // string modelScore = playerdata[1];
- // string modelRank = playerdata[2];
- // RankModel model = new RankModel(modelName, int.Parse(modelScore), int.Parse(modelRank));
- // rankModels.Add(model);
- //}
- if (!PlayerPrefs.HasKey("RankData"))
- {
- playerData = "孙悟空 99 0\n李寻欢 66 0\n猪八戒 70 0\n沙僧 89 0\n傻吊 30 0\n测试玩家 0 0";
- PlayerPrefs.SetString("RankData",playerData);
- }
- string Datas =PlayerPrefs.GetString("RankData");
- string[] playerdatas = Datas.Split('\n');
- foreach (string item in playerdatas)
- {
- string[] playerdata = item.Split(' ');
- string modelName = playerdata[0];
- string modelScore = playerdata[1];
- string modelRank = playerdata[2];
- RankModel model = new RankModel(modelName, int.Parse(modelScore), int.Parse(modelRank));
- rankModels.Add(model);
-
- }
-
-
-
-
- //将6名玩家添加到排行榜前五名数组中
- top5Arr = rankModels.ToArray();
- //对前五名数组排序
- SortTop5Arr();
- }
-
- //刷新数据--创建UI
- private void DisplayData()
- {
- for (int i = 0; i < rankModels.Count; i++)
- {
- //调用UI初始化方法,传递数据
- CreatElement().Init(rankModels[i]);
- }
- }
-
- //更新前5名数据
- void DisplayTop5Data()
- {
- for (int i = 0; i < top5Arr.Length - 1; i++)
- {
- //调用UI初始化方法,传递数据
- CreatElement().Init(top5Arr[i]);
- }
- }
-
-
- //创建一个单元格
- RankElementSc CreatElement()
- {
- //创建UI排行榜元素
- GameObject element = Instantiate<GameObject>(rankElementPr);
- //设置父物体,自身缩放与父物体缩放无关
- element.transform.SetParent(transform, false);
- //添加组件
- RankElementSc ranksc = element.AddComponent<RankElementSc>();
- //将脚本添加到集合
- elementScs.Add(ranksc);
- return ranksc;
- }
- //对所有玩家进行排序
- private void SortRankdataOrScore()
- {
- for (int i = 0; i < rankModels.Count - 1; i++)
- {
- bool k = false;
- for (int j = 0; j < rankModels.Count - 1 - i; j++)
- {
- if (rankModels[j].Score < rankModels[j + 1].Score)
- {
- RankModel temp;
- temp = rankModels[j];
- rankModels[j] = rankModels[j + 1];
- rankModels[j + 1] = temp;
- k = true;
- }
- }
- if (!k)
- {
- break;
- }
- }
-
- //设置玩家排名
- for (int i = 0; i < rankModels.Count; i++)
- {
- rankModels[i].Number = i + 1;
- }
- }
-
- //对前5名数组进行排序
- private void SortTop5Arr()
- {
- for (int i = 0; i < top5Arr.Length - 1; i++)
- {
- bool k = false;
- for (int j = 0; j < top5Arr.Length - 1 - i; j++)
- {
- if (top5Arr[j].Score < top5Arr[j + 1].Score)
- {
- RankModel temp;
- temp = top5Arr[j];
- top5Arr[j] = top5Arr[j + 1];
- top5Arr[j + 1] = temp;
- k = true;
- }
- }
- if (!k)
- {
- break;
- }
- }
-
- //排名
- for (int i = 0; i < top5Arr.Length; i++)
- {
- top5Arr[i].Number = i + 1;
- }
- }
-
- //增加玩家
- int playerName = 0;
- public void AddPlayer()
- {
- //增模拟加一名新玩家
- playerName++;
- int score = Random.Range(0, 100);
- RankModel model = new RankModel("玩家" + playerName, score, 0);
- rankModels.Add(model);
-
- //创建单元格
- CreatElement();
- //重新排序
- SortRankdataOrScore();
- //刷新数据
- RefreshData();
- }
-
- //刷新数据
- void RefreshData()
- {
- for (int i = 0; i < rankModels.Count; i++)
- {
- elementScs[i].Init(rankModels[i]);
- }
- }
-
- //刷新数据 -- 前五名数组
- void RefreshData(RankModel[] topArr)
- {
- for (int i = 0; i < topArr.Length - 1; i++)
- {
- elementScs[i].Init(topArr[i]);
- }
- }
-
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.Space))
- {
- //AddPlayer();
- //增模拟加一名新玩家
- playerName++;
- int score = Random.Range(0, 100);
- RankModel model = new RankModel("玩家" + playerName, score, 0);
- rankModels.Add(model);
- //替换掉原排名数组中第6人的名单
- top5Arr[RANKNUMBER] = model;
- //重新排序
- SortTop5Arr();
- //刷新数据
- RefreshData(top5Arr);
- }
- }
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。