ExcelBase接口类
c# 代码配置数据对象的类(用于Editor)
用于转换成xml时,在Construction给对象赋值,转成xml或者excel 文件。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 这个类用来保存转换成基本数据的类.存放有角色对象等数据.
/// </summary>
[System.Serializable]
public class ExcelBase
{
/// <summary>
/// 编译器生成基本数据的方法
/// </summary>
public virtual void Construction() { }
/// <summary>
/// 初始化方法
/// </summary>
public virtual void Init() { }
}
MonsterData 继承接口类
Construction :editor下进行产生对象,用于序列化
Init:用于程序运行时,初始化对象
MonsterBase 此类用来生成怪物表对象,xml 序列化成怪物表。
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using UnityEngine;
[System.Serializable]
public class MonsterData : ExcelBase
{
/// <summary>
/// 编译器下xml 的调用
/// </summary>
public override void Construction()
{
AllMonster = new List<MonsterBase>();
for (int i = 0; i < 5; i++)
{
MonsterBase monster = new MonsterBase();
monster.Id = i + 1;
monster.Name = i + "sq";
monster.OutLook = "Assets/GameData/Prefabs/Attack.prefab";
monster.Rare = 2;
monster.Height = 2 + i;
AllMonster.Add(monster);
}
}
/// <summary>
/// 运行时 初始化
/// </summary>
public override void Init()
{
m_AllMonsterDic.Clear();
foreach (MonsterBase monster in AllMonster) //把列表里的数据保存到字典里
{
if (m_AllMonsterDic.ContainsKey(monster.Id))
{
Debug.LogError(monster.Name + "有重复ID");
}
else
{
m_AllMonsterDic.Add(monster.Id, monster);
}
}
}
/// <summary>
/// 运行时 查找字典里是否有这个对象
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public MonsterBase FinMonsterById(int id)
{
return m_AllMonsterDic[id];
}
//运行时存放着怪物类.
[XmlIgnore]
public Dictionary<int, MonsterBase> m_AllMonsterDic = new Dictionary<int, MonsterBase>();
//这里用来保存所有的怪物对象, 为了防止重复new 加载导致多份list 数据,这里使用get,set
[XmlElement("AllMonster")]
public List<MonsterBase> AllMonster { get; set; }
}
[System.Serializable]
public class MonsterBase
{
[XmlAttribute("Id")]
public int Id { get; set; }
[XmlAttribute("name")]
public string Name { get; set; }
//预置路径
[XmlAttribute("OutLook")]
public string OutLook { get; set; }
[XmlAttribute("Level")]
public int Level { get; set; }
[XmlAttribute("Rare")]
public int Rare { get; set; }
[XmlAttribute("Height")]
public float Height { get; set; }
}