最近在做一个rogue类游戏,游戏的数据比较多

像这样的角色还有好几个,还有装备,物品等的数据,所以我选择用Xml的方式进行批量读取,也方便策划随时进行数据填充与修改

比方说我需要在游戏开始时根据玩家id从xml中读取对应的数据

XML文件

假设我的xml配置文件如下

<?xml version="1.0" encoding="utf-8"?>
<characters>
    <character id="0" name="武师">
        <lv values="0">
            <attack>2</attack>
            <attackSpeed>1.2</attackSpeed>
            <moveSpeed>1</moveSpeed>
            <maxHp>20</maxHp>
            <hpRecoverySpeed>0</hpRecoverySpeed>
            <shield>0</shield>
            <maxShield>20</maxShield>
            <shieldRecoverySpeed>0</shieldRecoverySpeed>
            <maxMp>0</maxMp>
            <mpRecoverSpeed>0</mpRecoverSpeed>
            <maxSp>15</maxSp>
            <spRecoverySpeed>1</spRecoverySpeed>
            <attackRange>1</attackRange>
            <armor>0</armor>
            <armorIgnore>0</armorIgnore>
            <magicArmor>5</magicArmor>
            <blitz>0.1</blitz>
            <blitzDamage>1.5</blitzDamage>
            <skillCoolDown>1</skillCoolDown>
            <skillCost>1</skillCost>
            <exp>0</exp>
            <getDamage>0</getDamage>
        </lv>
    </character>
</characters>

角色类

首先定义一个类,存有角色的各种属性

public class c_property {
    public float attack;
    public float attackSpeed;
    public float moveSpeed;
    public float maxHp;
    public float hpRecoverySpeed;
    public float shield;
    public float maxShield;
    public float shieldRecoverySpeed;
    public float maxMp;
    public float mpRecoverSpeed;
    public float maxSp;
    public float spRecoverySpeed;
    public float attackRange;
    public float armor;
    public float armorIgnore;
    public float magicArmor;
    public float blitz;
    public float blitzDamage;
    public float skillCoolDown;
    public float skillCost;
    public float exp;
    public float getDamage;
}

然后给定角色类的属性

public class Character : MonoBehaviour {
    public int id;
    public c_property prop = new c_property ();
}

Xml文件读取

关于xml的操作可以看这一篇文章https://blog.csdn.net/NCZ9_/article/details/85230187

之后再初始化角色数据

public void PropInit (int id) {
    //首先加载Xml文件
    XmlDocument charctersXml = new XmlDocument ();
    charctersXml.Load ("Assets/Resources/Xml/Character.xml");
    //获取到根节点
    XmlElement charcters = charctersXml.DocumentElement;
    //获取 类 的属性值
    FieldInfo[] props = prop.GetType ().GetFields ();
    //遍历根节点的子节点
    foreach (XmlNode character in charcters) {
        //转换为可操作的元素
        XmlElement xe = (XmlElement) character;
        //获取节点的id属性
        if (int.Parse (xe.GetAttribute ("id")) == id) {
            Debug.Log (props.Length);
            for (int i = 0; i < props.Length; i++) {
                //给类的每个属性赋值
                if (props[i].Name == "name") {
                    props[i].SetValue (prop, xe.GetAttribute ("name"));
                }
                else {
                    props[i].SetValue (prop, float.Parse (xe.ChildNodes[0].SelectSingleNode (props[i].Name).InnerText));
                }
            }
        }
    }
    Debug.Log ("初始化玩家数据");
    foreach (var field in props) {
        Debug.Log (field.Name + ":" + field.GetValue (prop));
    }
}

最后在Start中调用属性初始化函数即可,运行后就会自动根据xml内数据初始化角色

Last modification:March 11th, 2021 at 10:43 pm