using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RankUI : MonoBehaviour {
public GameObject enterUI;
public GameObject rankPlayerItemPref;
public UIGrid grid;
void OnEnable()
{
if (RankPlayerInfosManager.Instance.RankPLayers.Count == 0)
{
// 加载初始列表
XmlManager.Instance.LoadXML(Application.dataPath + "/Players.xml");
}
InitRankUI();
}
// 初始化排行榜UI界面
private void InitRankUI()
{
RefreshRankPlayerMenu();
}
// 刷新排行榜玩家列表
private void RefreshRankPlayerMenu()
{
ClearList();
for (int i = 0; i < RankPlayerInfosManager.Instance.RankPLayers.Count; i++)
{
GameObject go = GameObject.Instantiate(rankPlayerItemPref) as GameObject;
go.transform.SetParent(grid.transform);
go.transform.localScale = Vector3.one;
// 初始化Item(显示方面)
go.GetComponent<RankPlayerInfoItem>().InitItem(RankPlayerInfosManager.Instance.RankPLayers[i]);
go.SetActive(true);
}
// 排列grid下的item(两种方式)
grid.repositionNow = true;
//grid.Reposition();
}
public void ClearList()
{
// 清空之前grid下的对象(item)
List<Transform> items = grid.GetChildList();
if (items.Count != 0)
{
for (int i = 0; i < items.Count; i++)
{
DestroyImmediate(items[i].gameObject);
}
}
}
public void OnClickCloseBtn()
{
this.gameObject.SetActive(false);
enterUI.SetActive(true);
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public enum Sex
{
male = 0,
female = 1,
errorCode = 2,
}
/// <summary>
/// 排行榜玩家信息的数据模型
/// </summary>
public class RankPLayerInfo{
public int id;
public int rankValue;
public string name;
public int level;
public Sex sex;
public int priceValue;
public RankPLayerInfo(int id, int rankValue, string name, int level, Sex sex, int priceValue)
{
this.id = id;
this.rankValue = rankValue;
this.name = name;
this.level = level;
this.sex = sex;
this.priceValue = priceValue;
}
}
// 排行榜玩家信息管理类
public class RankPlayerInfosManager
{
// 单例
private static RankPlayerInfosManager instance;
private List<RankPLayerInfo> rankPLayers = new List<RankPLayerInfo>();
public List<RankPLayerInfo> RankPLayers
{
get {
sort();
return rankPLayers;
}
set { rankPLayers = value; }
}
public static RankPlayerInfosManager Instance
{
get {
if (instance == null)
{
instance = new RankPlayerInfosManager();
}
return instance;
}
}
private RankPlayerInfosManager()
{
}
// 添加玩家
public void AddPlayerInfo(int id, int rankValue, string name, int level, Sex sex, int priceValue)
{
RankPLayerInfo info = new RankPLayerInfo(id, rankValue, name, level, sex, priceValue);
rankPLayers.Add(info);
}
// 删除玩家
public void RemovePlayerInfo(int id)
{
RankPLayerInfo info = GetPLayerInfoByID(id);
if (info == null)
{
// 若当前玩家列表中没有该id的玩家,终止操作
return;
}
else
{
// 若有,则删除该玩家
rankPLayers.Remove(info);
}
}
// 通过玩家ID找到该玩家信息
public RankPLayerInfo GetPLayerInfoByID(int id)
{
foreach (RankPLayerInfo item in rankPLayers)
{
if (item.id == id)
{
return item;
}
}
return null;
}
public void sort()
{
rankPLayers.Sort(sortRankPlayers);
}
// 排序规则
private int sortRankPlayers(RankPLayerInfo info1, RankPLayerInfo info2)
{
if (info1.rankValue != info2.rankValue)
{
if (info1.rankValue > info2.rankValue)
{
return 1;
}
else
{
return -1;
}
}
else
{
if (info1.level <= info2.level)
{
return 1;
}
else
{
return -1;
}
}
}
}
using UnityEngine;
using System.Collections;
public class RankPlayerInfoItem : MonoBehaviour {
public RankPLayerInfo itemInfo;
public UILabel rankLabel;
public UILabel nameLabel;
public UILabel levelLabel;
public UILabel priceLabel;
public void InitItem(RankPLayerInfo info)
{
this.itemInfo = info;
rankLabel.text = info.rankValue.ToString();
nameLabel.text = info.name;
levelLabel.text = info.level.ToString();
priceLabel.text = info.priceValue.ToString();
changeNameColorBySex(info.sex);
}
public void changeNameColorBySex(Sex sex)
{
if (sex == Sex.male)
{
nameLabel.color = Color.blue;
}
else if (sex == Sex.female)
{
nameLabel.color = Color.red;
}
else
{
return;
}
}
}
using UnityEngine;
using System.Collections;
using System.Xml;
public class XmlManager : MonoBehaviour {
// 单例,负责Xml读取
private static XmlManager instance;
public static XmlManager Instance
{
get { return instance; }
}
// Use this for initialization
void Awake () {
instance = this;
}
public void LoadXML(string path)
{
// 创建一个XML文件
XmlDocument xml = new XmlDocument();
// 读取设置,忽略xml中的注释
XmlReaderSettings set = new XmlReaderSettings();
set.IgnoreComments = true;
// 加载xml
xml.Load(XmlReader.Create(path, set));
// 读取根节点里面的数据
XmlNodeList xmlNodeList = xml.SelectSingleNode("PlayerInfos").ChildNodes;
// 该节点下所有成员遍历
foreach (XmlElement item in xmlNodeList)
{
// 读取玩家所有数据,并添加到数据列表中
int id = int.Parse(item.GetAttribute("id"));
int rankValue = int.Parse(item.GetAttribute("rank"));
string name = item.GetAttribute("name");
int level = int.Parse(item.GetAttribute("lv"));
Sex sex = (Sex)(int.Parse(item.GetAttribute("sex")));
int priceValue = int.Parse(item.GetAttribute("price_value"));
RankPlayerInfosManager.Instance.AddPlayerInfo(id, rankValue, name, level, sex, priceValue);
}
}
}
NGUI+xml解析
最新推荐文章于 2024-05-30 11:29:41 发布
3243

被折叠的 条评论
为什么被折叠?



