今天主要学习加载XML表中的数据,按每条数据对应一个预制体方块显示在UI界面上:
一、图鉴界面上加载预制体方块和相应数据:
using UnityEngine;
using System.Collections;
using System.Collections .Generic ;
public class PetIllustrations : MonoBehaviour {
public static PetIllustrations self;
public GameObject parentObj;
public int number
{
get
{
return ConfigManager.Instance.GetpetXmlDictCount();
}
}
private GameObject[] gridItemobj = null;//加载数量即为xml所有数量
void OnEnable()
{
InitPetIllustrations();
}
void Awake()
{
self = this;
}
public void InitPetIllustrations()//加载所有宠物
{
UnityEngine.Object gridItem = Resources.Load("Prefabs/item/"+"petItem");
if (gridItemobj == null)
{
// gridItemobj = new GameObject[ConfigManager.Instance.GetpetXmlDictCount()];
gridItemobj = new GameObject[number];
}
for (int i = 0; i < gridItemobj.Length; i++)//
{
gridItemobj[i] = GameObject.Instantiate(gridItem) as GameObject;//实例化
gridItemobj[i].name = "petItem" + (i + 1).ToString();//添加的预制体的名字
gridItemobj[i].transform.parent = parentObj.transform;//确定父物体位置,
gridItemobj[i].transform.localPosition = Vector3.zero;//落点为中心点
gridItemobj[i].transform.localScale = Vector3.one;//大小正常(1)
gridItemobj[i].GetComponent<petItem>().UpdatepetItemData(i + 1);//给预制体加载数据。
}
}
public void OnClickBtnBack()
{
UIManager.instance.HidePanel(UIPanelNames.PetIllustrationsPanel);
RoleControl.self.IsCanMove = false;
}
}
二、预制体加载数据:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class petItem : MonoBehaviour {
public static petItem self;
public PetXmlData PetXmlVO;
public Image HeadImage;
public Image ImageMask;
public Text Text_Hp;
public Text Text_Attack;
public Text Text_Lv;
void Awake()
{
self = this;
}
public void UpdatepetItemData(int id)//预制体 加载数据
{
PetXmlVO = ConfigManager.Instance.GetPetData(id);
Text_Hp.text = PetXmlVO.hp.ToString();
Text_Attack.text = PetXmlVO.attack.ToString();
Text_Lv.text = PetXmlVO.level.ToString();
HeadImage.sprite = Resources.Load<Sprite>("Images/" + PetXmlVO.headImage);
}
}
三、数据管理DataManager:
using UnityEngine;
using Mono.Xml;
using System.Security;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Reflection;
using ZPStudio;
public class ConfigManager : MonoBehaviour
{
private static ConfigManager mInstance = null;
//宠物配置
public Dictionary<int, PetXmlData> petXmlDataDict = new Dictionary<int, PetXmlData>();
public static ConfigManager Instance
{
get
{
return mInstance;
}
}
public int GetpetXmlDictCount()//获取长度
{
return petXmlDataDict.Count;
}
public PetXmlData GetPetXMLInfo(int DictOrder)
{
return petXmlDataDict[DictOrder];
}
}
四、XML数据配表:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Pets>
<Pet id="1" name="蜗牛兽" level="1" hp="15" attack="3" catchcost="5" worth="3" probability="0.8" headImage="1boluo"/>
<Pet id="2" name="小黄鸡" level="1" hp="9" attack="5" catchcost="5" worth="3" probability="0.8" headImage="2xigua"/>
<Pet id="3" name="小乌贼" level="1" hp="11" attack="5" catchcost="5" worth="3" probability="0.8" headImage="3kefu"/>
<Pet id="4" name="蓝蛇怪" level="1" hp="10" attack="5" catchcost="5" worth="3" probability="0.8" headImage="4labbg"/>
<Pet id="5" name="小萝卜" level="1" hp="10" attack="5" catchcost="5" worth="3" probability="0.8" headImage="5star"/>
<Pet id="6" name="宝箱怪" level="1" hp="10" attack="1" catchcost="5" worth="3" probability="0.8" headImage="js2"/>
<Pet id="7" name="小苹果" level="1" hp="10" attack="1" catchcost="5" worth="3" probability="0.8" headImage="jsk_00"/>
<Pet id="8" name="巴努兽" level="1" hp="10" attack="4" catchcost="5" worth="3" probability="0.8" headImage="jump"/>
<Pet id="9" name="小章鱼" level="2" hp="20" attack="8" catchcost="20" worth="8" probability="0.7" headImage="jxjt"/>
<Pet id="10" name="僵尸熊" level="2" hp="20" attack="8" catchcost="20" worth="8" probability="0.7" headImage="kongdiandian"/>
<Pet id="11" name="小羊兽" level="2" hp="10" attack="10" catchcost="20" worth="8" probability="0.7" headImage="lansebuffzhuagntai"/>
<Pet id="12" name="胡萝卜拳手" level="2" hp="15" attack="9" catchcost="20" worth="8" probability="0.7" headImage="libao"/>
<Pet id="13" name="长牙怪" level="2" hp="30" attack="3" catchcost="20" worth="8" probability="0.7" headImage="shengjituan"/>
<Pet id="14" name="千足蛙" level="2" hp="30" attack="5" catchcost="20" worth="8" probability="0.7" headImage="shezhi"/>
<Pet id="15" name="混种蜗牛" level="2" hp="40" attack="10" catchcost="20" worth="8" probability="0.7" headImage="shop"/>
<Pet id="16" name="卡布拉怪" level="3" hp="35" attack="10" catchcost="40" worth="10" probability="0.6" headImage="skill_huoqiu"/>
<Pet id="17" name="洋葱头" level="3" hp="30" attack="8" catchcost="40" worth="10" probability="0.6" headImage="skill_zhiliao"/>
<Pet id="18" name="香蕉侠" level="3" hp="45" attack="15" catchcost="40" worth="10" probability="0.6" headImage="suo"/>
<Pet id="19" name="章鱼王" level="3" hp="50" attack="10" catchcost="40" worth="10" probability="0.6" headImage="tequan"/>
<Pet id="20" name="派拉斯" level="3" hp="35" attack="10" catchcost="40" worth="10" probability="0.6" headImage="tuichu"/>
<Pet id="21" name="胡萝卜船长" level="4" hp="65" attack="30" catchcost="60" worth="12" probability="0.5" headImage="tx_01"/>
<Pet id="22" name="肥蜥蜴" level="4" hp="45" attack="20" catchcost="60" worth="12" probability="0.5" headImage="tx_02"/>
<Pet id="23" name="紫茄兽" level="4" hp="55" attack="15" catchcost="60" worth="12" probability="0.5" headImage="tx_03"/>
<Pet id="24" name="盲眼鸟" level="4" hp="50" attack="25" catchcost="60" worth="12" probability="0.5" headImage="tx_04"/>
<Pet id="25" name="花椰菜" level="4" hp="75" attack="20" catchcost="60" worth="12" probability="0.5" headImage="tx_05"/>
<Pet id="26" name="嘎啦" level="4" hp="55" attack="25" catchcost="60" worth="12" probability="0.5" headImage="tx_06"/>
<Pet id="27" name="独眼布欧" level="4" hp="85" attack="15" catchcost="60" worth="12" probability="0.5" headImage="tx_07"/>
<Pet id="28" name="臭臭泥" level="5" hp="115" attack="24" catchcost="80" worth="15" probability="0.4" headImage="zuanshi"/>
<Pet id="29" name="香蕉先生" level="5" hp="75" attack="35" catchcost="80" worth="15" probability="0.4" headImage="zuanshi-icon"/>
<Pet id="30" name="海王星" level="5" hp="90" attack="30" catchcost="80" worth="15" probability="0.4" headImage="zuanshi-icon1"/>
<Pet id="31" name="羊头怪" level="5" hp="80" attack="35" catchcost="80" worth="15" probability="0.4" headImage="zuanshi-icon2"/>
<Pet id="32" name="独眼恶魔" level="5" hp="85" attack="30" catchcost="80" worth="15" probability="0.4" headImage="zuanshi-icon3"/>
<Pet id="33" name="图图犬" level="5" hp="115" attack="25" catchcost="80" worth="15" probability="0.4" headImage="suger2"/>
<Pet id="34" name="哥达鸭" level="5" hp="75" attack="40" catchcost="80" worth="15" probability="0.4" headImage="sailout"/>
<Pet id="35" name="铁甲蟹" level="6" hp="100" attack="55" catchcost="120" worth="18" probability="0.3" headImage="skill_huoqiu"/>
<Pet id="36" name="镰刀盔" level="6" hp="125" attack="65" catchcost="120" worth="18" probability="0.3" headImage="skill_huoqiu"/>
<Pet id="37" name="蓝皮鼠" level="6" hp="130" attack="55" catchcost="120" worth="18" probability="0.3" headImage="skill_huoqiu"/>
<Pet id="38" name="兔宝宝" level="6" hp="125" attack="55" catchcost="120" worth="18" probability="0.3" headImage="skill_huoqiu"/>
<Pet id="39" name="遁甲蜗牛" level="6" hp="145" attack="45" catchcost="120" worth="18" probability="0.3" headImage="skill_huoqiu"/>
<Pet id="40" name="铁犀牛" level="6" hp="145" attack="50" catchcost="120" worth="18" probability="0.3" headImage="skill_huoqiu"/>
<Pet id="41" name="眼魔" level="7" hp="110" attack="65" catchcost="150" worth="25" probability="0.2" headImage="skill_huoqiu"/>
<Pet id="42" name="大嘴鳄鱼" level="7" hp="115" attack="75" catchcost="150" worth="25" probability="0.2" headImage="skill_huoqiu"/>
<Pet id="43" name="蓝色魔鬼" level="7" hp="165" attack="65" catchcost="150" worth="25" probability="0.2" headImage="skill_huoqiu"/>
<Pet id="44" name="僵尸兽" level="7" hp="180" attack="90" catchcost="150" worth="25" probability="0.2" headImage="skill_huoqiu"/>
<Pet id="45" name="杯子猫" level="7" hp="145" attack="80" catchcost="150" worth="25" probability="0.2" headImage="skill_huoqiu"/>
<Pet id="46" name="暴龙" level="7" hp="200" attack="80" catchcost="150" worth="25" probability="0.2" headImage="skill_huoqiu"/>
<Pet id="47" name="兔米花" level="8" hp="200" attack="160" catchcost="200" worth="40" probability="0.1" headImage="skill_huoqiu"/>
<Pet id="48" name="饼干人" level="8" hp="165" attack="125" catchcost="200" worth="40" probability="0.1" headImage="skill_huoqiu"/>
<Pet id="49" name="糖果猫" level="8" hp="220" attack="165" catchcost="200" worth="40" probability="0.1" headImage="skill_huoqiu"/>
<Pet id="50" name="宇宙大帝" level="8" hp="220" attack="200" catchcost="200" worth="40" probability="0.1" headImage="1boluo"/>
</Pets>
五、结果显示: