赞
踩
首先找一些排行榜的背景图片当作排行榜的背景,也可以添加前三名的排名图片,将前三名的排名变成图片显示出来,我这里没做注解。也可以在建立的空UI中添加Rect Transform可以让排行榜有一个滑动效果,注意添加这个组件一定要添加Image组件。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RankItem : MonoBehaviour
{
private Text pm = null;
private Text xm = null;
private Text fs = null;
public void Init(int rank,string name,string score)
{
pm = transform.Find("pm").GetComponent<Text>();
xm = transform.Find("xm").GetComponent<Text>();
fs = transform.Find("fs").GetComponent<Text>();
pm.text = "第" + rank + "名";
xm.text = name;
fs.text = " " + score;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RankListPanel : MonoBehaviour
{
private RankItem RankItemmould; //模板
private Transform RankItemparent; //父亲
private List<RankItem > RankItemList ; //列表
void Start()
{
//初始化
RankItemmould = transform.Find("rankItem").GetComponent<RankItem >();
RankItemparent = transform.Find("scrollView/viewPort/content/grip");
RankItemList = new List<RankItem>();
RankItemmanage.Instance.save("张三", 27000);
RankItemmanage.Instance.save("王二",28000);
RankItemmanage.Instance.save("赵六", 26000);
RankItemmanage.Instance.save("拉拉啦",31000);
RankItemmanage.Instance.save("李四儿", 34000);
RankItemmanage.Instance.save("孙六", 32000);
Init();
}
public void Init()
{
List<Rank> _allRankInfoList = RankItemmanage .Instance.get ();
if(_allRankInfoList.Count <= 0)
{
return;
}
for (int i = 0; i < _allRankInfoList.Count; i++)
{
RankItem rankItem = GetARankItem ();
rankItem.Init(i+1, _allRankInfoList[i].name, ""+ _allRankInfoList[i].score );
}
}
//新的排行项目
RankItem CreateANewRankItem()
{
GameObject go = Instantiate(RankItemmould.gameObject) as GameObject; //复制排行相对应
RankItem Newpaihang = go.GetComponent<RankItem>();
Newpaihang.transform.SetParent(RankItemparent);
RankItemList.Add(Newpaihang);
return Newpaihang;
}
RankItem GetARankItem()
{
RankItem newrankItem = CreateANewRankItem ();
return newrankItem;
}
}
using System.Collections.Generic;
using UnityEngine;
//排行信息管理
public class Rank
{
public string name = "";
public int score = 0;
}
public class RankItemmanage
{
private List <Rank > Linelist=null; //
private static RankItemmanage Xpaihang = null;
private int max=10;
public static RankItemmanage Instance
{
get
{
if (Xpaihang == null)
{
Xpaihang = new RankItemmanage();
}
return Xpaihang;
}
}
//存储信息
public void save(string name,int score)
{
Analyze();
//最新信息加入列表
Rank rankInfo = new Rank();
rankInfo.name = name;
rankInfo.score = score ;
Linelist.Add(rankInfo);
//新加入的信息进行排序
//对超过排名的信息进行抹去
Linelist.Sort(CompareSpeedFromBigToSmall);
if (Linelist.Count > max)
{
Linelist.RemoveAt(Linelist.Count - 1);
}
string Memory = ""; //所有排行信息
for(int i=0;i< Linelist.Count; i++)
{
//每一个排行信息
string HMemory = "";
HMemory += Linelist[i].name;
HMemory += "+";
HMemory += Linelist[i].score;
if (i != (Linelist.Count - 1))
{
HMemory += "|";
}
Memory += HMemory;
}
//存储排行信息
PlayerPrefs.SetString("RankInfo", Memory);
}
//获得排行信息
public List<Rank> get()
{
Analyze();
return Linelist;
}
//分离解析存储的排行信息
private void Analyze()
{
Linelist = new List<Rank>();
string rankString = PlayerPrefs.GetString("RankInfo");
//如果存储有信息进行解析
if(!string.IsNullOrEmpty (rankString))
{
string[] rankItemArray = rankString.Split('|');
for(int i=0;i<rankItemArray.Length; i++)
{
string[] rankItem = rankItemArray[i].Split('+');
Rank rankInfo = new Rank();
rankInfo.name = rankItem[0];
rankInfo.score = int.Parse(rankItem[1]);
Linelist.Add(rankInfo);
}
}
if (Linelist.Count > 0)
{
Linelist.Sort(CompareSpeedFromBigToSmall);
}
}
//冒泡排序
private int CompareSpeedFromBigToSmall(Rank rank1, Rank rank2)
{
double a = rank1.score;
double b = rank2.score;
if (a==b)
{
return 0;
}
else if (a>b)
{
return -1;
}
else
{
return 1;
}
}
}
注:每次运行都会存储一遍游戏者的信息。这个排行需要和游戏类结合,这边我为了测试直接输入信息并直接调用了 Init()函数。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。