using UnityEngine;--------low版------------------------
public class UIHero:MonoBehaviour
{
public string _name;
public int score;
public UIHero(string name, int score)
{
this._name = name;
this.score = score;
}
}
using UnityEngine;
using UnityEngine.UI;
public class UILeaderboard : MonoBehaviour
{
//预设体
public GameObject leaderPre;
//生成列表的数组
private GameObject[] _leaders;
//英雄类数组
public UIHero[] heroes;
private void Start()
{
//生成英雄
UIHero h1 = new UIHero("亚瑟", 80);
UIHero h2 = new UIHero("妲己", 95);
UIHero h3 = new UIHero("安琪拉", 96);
UIHero h4 = new UIHero("赵云", 85);
UIHero h5 = new UIHero("项羽", 70);
UIHero h6 = new UIHero("王昭君", 97);
UIHero h7 = new UIHero("夏侯惇", 81);
UIHero h8 = new UIHero("白起", 70);
UIHero h9 = new UIHero("老夫子", 84);
UIHero h10 = new UIHero("不知火舞", 93);
UIHero h11 = new UIHero("孙尚香", 99);
UIHero h12 = new UIHero("孙悟空", 90);
UIHero h13 = new UIHero("干将莫邪", 94);
UIHero h14 = new UIHero("刘邦", 74);
UIHero h15 = new UIHero("韩信", 88);
UIHero h16 = new UIHero("刘禅", 70);
UIHero h17= new UIHero("钟馗", 81);
UIHero h18 = new UIHero("宫本武藏", 90);
UIHero h19 = new UIHero("武则天", 93);
UIHero h20 = new UIHero("吕布", 89);
//将英雄添加到数组
heroes[0] = h1;
heroes[1] = h2;
heroes[2] = h3;
heroes[3] = h4;
heroes[4] = h5;
heroes[5] = h6;
heroes[6] = h7;
heroes[7] = h8;
heroes[8] = h9;
heroes[9] = h10;
heroes[10] = h11;
heroes[11] = h12;
heroes[12] = h13;
heroes[13] = h14;
heroes[14] = h15;
heroes[15] = h16;
heroes[16] = h17;
heroes[17] = h18;
heroes[18] = h19;
heroes[19] = h20;
//生成一个空英雄
//赋予列表长度
_leaders = new GameObject[20];
//根据分数进行冒泡排序
for (int i = 0; i < heroes.Length-1; i++)
{
for (int j = 0; j < heroes.Length-i-1; j++)
{
if (heroes[j].score<heroes[j+1].score)
{
var nulll = heroes[j];
heroes[j] = heroes[j + 1];
heroes[j + 1] = nulll;
}
}
}
for (int i = 0; i <_leaders.Length ; i++)
{
//生成预设体
_leaders[i] = Instantiate(leaderPre, gameObject.transform);
//生成三个文字
_leaders[i].transform.GetChild(0).GetComponent<Text>().text = (i+1).ToString();
_leaders[i].transform.GetChild(1).GetComponent<Text>().text = heroes[i]._name;
_leaders[i].transform.GetChild(2).GetComponent<Text>().text = heroes[i].score.ToString();
}
}
private void Update()
{
}
}
//数据模型类 主要负责数据
public class CharmModel
{
public string Name
{
get;
set;
}
public int Score
{
get;
set;//
}
public int Number
{
get;
set;
}
public CharmModel(string name, int score, int number)
{
Name = name;
Score = score;
Number = number;
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//排行榜视图类:负责UI数据的显示,向外界提供UI数据初始化的方法
public class CharmView : MonoBehaviour
{
private Text numberText;
private Text nameText;
private Text scoreText;
private void Awake()
{
nameText = transform.Find("Msg2").GetComponent<Text>();
numberText = transform.Find("Msg1").GetComponent<Text>();
scoreText = transform.Find("Msg3").GetComponent<Text>();
}
public void Init(CharmModel charmModel)
{
nameText.text = charmModel.Name;
numberText.text = charmModel.Number.ToString();
scoreText.text = charmModel.Score.ToString();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;
//排行榜控制类,负责数据的处理,游戏逻辑的相关内容
public class CharmController : MonoBehaviour
{
//创建预设体
public GameObject rankPre;
//数据集合
private List<CharmModel> _charmModels;
//视图集合
private List<CharmView> _charmViews;
//冒泡优化开关
private bool k;
//人物名字号码
private int playerNum;
//要删除人物的名字
private string remName;
//声明模型数组
private CharmModel[] topArr;
private void Awake()
{
//初始化数据列表
_charmModels=new List<CharmModel>();
//初始化视图列表
_charmViews=new List<CharmView>();
//初始化数组
topArr=new CharmModel[5];
}
private void Start()
{
LoadData();
//Sort();
//DisplayData();
DisPlayArrData();
}
/// <summary>
/// 初始化数据
/// </summary>
private void LoadData()
{
CharmModel c1=new CharmModel("孙悟空",88,1);
_charmModels.Add(c1);
CharmModel c2=new CharmModel("唐僧",78,1);
_charmModels.Add(c2);
CharmModel c3=new CharmModel("沙和尚",89,1);
_charmModels.Add(c3);
CharmModel c4=new CharmModel("白龙马",87,1);
_charmModels.Add(c4);
CharmModel c5=new CharmModel("白骨精",98,1);
_charmModels.Add(c5);
CharmModel c6=new CharmModel("如来佛祖",58,1);
_charmModels.Add(c6);
CharmModel c7=new CharmModel("观世音菩萨",82,1);
_charmModels.Add(c7);
CharmModel c8=new CharmModel("哪吒",81,1);
_charmModels.Add(c8);
topArr = _charmModels.ToArray();
//Sort();
ArrSort();
}
/// <summary>
/// 创建单元格
/// </summary>
/// <returns></returns>
private CharmView CreatView()
{
//创建预设体
GameObject obj = Instantiate(rankPre, transform);
//添加单元格组件
CharmView view = obj.AddComponent<CharmView>();
//添加到集合内
_charmViews.Add(view);
return view;
}
/// <summary>
/// 刷新数据
/// </summary>
private void DisplayData()
{
for (int i = 0; i < _charmModels.Count; i++)
{
//传递数据
CreatView().Init(_charmModels[i]);
}
}
/// <summary>
/// 对数组数据进行刷新
/// </summary>
private void DisPlayArrData()
{
for (int i = 0; i < topArr.Length; i++)
{
CreatView().Init(topArr[i]);
}
}
/// <summary>
/// 进行数据排序
/// </summary>
private void Sort()
{
for (int i = 0; i < _charmModels.Count-1; i++)
{
bool k = false;
for (int j = 0; j < _charmModels.Count-1-i; j++)
{
if (_charmModels[j].Score<_charmModels[j+1].Score)
{
CharmModel mm;
mm = _charmModels[j];
_charmModels[j] = _charmModels[j + 1];
_charmModels[j + 1] = mm;
k = true;
}
}
if (!k)
{
break;
}
}
for (int i = 0; i < _charmModels.Count; i++)
{
_charmModels[i].Number = i + 1;
}
}
/// <summary>
/// 对数组进行排序
/// </summary>
private void ArrSort()
{
for (int i = 0; i < topArr.Length-1; i++)
{
k = false;
for (int j = 0; j < topArr.Length-1-i; j++)
{
if (topArr[j].Score<topArr[j+1].Score)
{
CharmModel cc;
cc = topArr[j];
topArr[j] = topArr[j + 1];
topArr[j + 1] = cc;
k = true;
}
}
if (!k)
{
break;
}
}
for (int i = 0; i < topArr.Length; i++)
{
topArr[i].Number = i + 1;
}
}
/// <summary>
/// 更新数据
/// </summary>
private void RefreshData()
{
for (int i = 0; i < _charmModels.Count; i++)
{
//传递数据
_charmViews[i].Init(_charmModels[i]);
}
}
/// <summary>
/// 对数组数据进行跟新
/// </summary>
private void RefreshData(CharmModel[] topArr)
{
for (int i = 0; i < topArr.Length; i++)
{
_charmViews[i].Init(topArr[i]);
}
}
/// <summary>
/// 添加玩家
/// </summary>
private void AddPlayer()
{
playerNum++;
int score = Random.Range(1, 100);
CharmModel cc=new CharmModel("玩家"+playerNum,score,0);
_charmModels.Add(cc);
CreatView();
Sort();
RefreshData();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.K))
{
//AddPlayer();
playerNum++;
int score = Random.Range(1, 100);
CharmModel cc=new CharmModel("玩家"+playerNum,score,0);
_charmModels.Add(cc);
//替换对后一名
topArr[7] = cc;
//重新排序
ArrSort();
//刷新数据
RefreshData(topArr);
}
}
}
using UnityEngine;
//数据模型类 主要负责数据
public class UIGradeModel
{
public string Name
{
get;
set;
}
public int Number
{
get;
set;
}
public int Score
{
get;
set;
}
public UIGradeModel(string name, int score, int number)
{
Name = name;
Number = number;
Score = score;
}
}
using UnityEngine;
using UnityEngine.UI;
public class UIGradeView : MonoBehaviour
{
private Text nameText;
private Text numberText;
private Text scoreText;
private void Awake()
{
nameText = transform.Find("Msg2").GetComponent<Text>();
numberText = transform.Find("Msg1").GetComponent<Text>();
scoreText = transform.Find("Msg3").GetComponent<Text>();
}
public void Init(UIGradeModel uiGradeModel)
{
nameText.text = uiGradeModel.Name;
scoreText.text = uiGradeModel.Score.ToString();
numberText.text = uiGradeModel.Number.ToString();
}
}
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class UIGradeController : MonoBehaviour
{
public GameObject rankPre;
private List<UIGradeModel> _uiGradeModels;
private List<UIGradeView> _uiGradeViews;
private bool k;
private int playerNum;
//private int rankNum = 8;
private UIGradeModel[] gradeModels;
private void Awake()
{
_uiGradeModels=new List<UIGradeModel>();
_uiGradeViews=new List<UIGradeView>();
}
private void Start()
{
LoadData();
DisPlayData();
}
private void LoadData()
{
UIGradeModel g1=new UIGradeModel("孙悟空",99,0);
_uiGradeModels.Add(g1);
UIGradeModel g2=new UIGradeModel("唐僧",89,0);
_uiGradeModels.Add(g2);
UIGradeModel g3=new UIGradeModel("沙和尚",92,0);
_uiGradeModels.Add(g3);
UIGradeModel g4=new UIGradeModel("白龙马",49,0);
_uiGradeModels.Add(g4);
UIGradeModel g5=new UIGradeModel("白骨精",95,0);
_uiGradeModels.Add(g5);
UIGradeModel g6=new UIGradeModel("如来佛祖",79,0);
_uiGradeModels.Add(g6);
UIGradeModel g7=new UIGradeModel("观世音菩萨",59,0);
_uiGradeModels.Add(g7);
UIGradeModel g8=new UIGradeModel("哪吒",96,0);
_uiGradeModels.Add(g8);
Sort();
}
private UIGradeView CreatView()
{
GameObject obj = Instantiate(rankPre, transform);
UIGradeView view = obj.AddComponent<UIGradeView>();
_uiGradeViews.Add(view);
return view;
}
private void DisPlayData()
{
for (int i = 0; i < _uiGradeModels.Count; i++)
{
CreatView().Init(_uiGradeModels[i]);
}
}
private void Sort()
{
for (int i = 0; i < _uiGradeModels.Count-1; i++)
{
k = false;
for (int j = 0; j < _uiGradeModels.Count-1-i; j++)
{
UIGradeModel gg;
if (_uiGradeModels[j].Score<_uiGradeModels[j+1].Score)
{
gg = _uiGradeModels[j];
_uiGradeModels[j] = _uiGradeModels[j + 1];
_uiGradeModels[j + 1] = gg;
k = true;
}
}
if (!k)
{
break;
}
}
for (int i = 0; i < _uiGradeModels.Count; i++)
{
_uiGradeModels[i].Number = i + 1;
}
}
private void RefreshData()
{
for (int i = 0; i < _uiGradeModels.Count; i++)
{
_uiGradeViews[i].Init(_uiGradeModels[i]);
}
}
private void AddPlayer()
{
playerNum++;
int score = Random.Range(1, 100);
UIGradeModel gg=new UIGradeModel("玩家"+playerNum,score,0);
_uiGradeModels.Add(gg);
CreatView();
Sort();
RefreshData();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.K))
{
AddPlayer();
}
}
}
|