ConfigReader(四十)—— ReadNpcConfig

本文介绍了一个用于读取NPC配置文件的C#类ReadNpcConfig,该类从XML文件中加载NPC的各种属性,并将这些信息存储在NpcConfigInfo实例中。文章详细展示了如何通过遍历XML节点来获取NPC的ID、名称、类型等参数。
摘要由CSDN通过智能技术生成

目录为:Assets/Scripts/ConfigReader/目录下
ReadNpcConfig.cs

对应配置文件:
Assets/Resources/Config/NpcCfg.xml
这个配置文件很大,部分如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<NPCCfg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <info un32ID="20001">
        <szNOStr>Beinuyideyuanren_5</szNOStr>
        <eNpcType>1</eNpcType>
        <eNpcChildType>3</eNpcChildType>
        <XuetiaoHeight>4</XuetiaoHeight>
        <EmitPos>0,1000,0</EmitPos>
        <HitPos>0,1300,0</HitPos>
        <eRace>1</eRace>
        <eAttendantCate>1</eAttendantCate>
        <eMagicCate>2</eMagicCate>
        <eAttackWay>1</eAttackWay>
        <n32AttackDist>230</n32AttackDist>
        <eAICate>2</eAICate>
        <un32AITarID>0</un32AITarID>
        <n32PursueDist>1400</n32PursueDist>
        <n32GuardDist>1000</n32GuardDist>
        <eAttackMode>0</eAttackMode>
        <n32AttackPower>45</n32AttackPower>
        <n32MagAttackPower>0</n32MagAttackPower>
        <n32DefenseAbility>0</n32DefenseAbility>
        <n32MagDefenseAbility>150</n32MagDefenseAbility>
        <n32MoveSpeed>560</n32MoveSpeed>
        <n32BaseMoveSpeedScaling>1200</n32BaseMoveSpeedScaling>
        <n32AttackSpeed>1333</n32AttackSpeed>
        <n32MaxHP>400</n32MaxHP>
        <n32MaxMP>0</n32MaxMP>
        <n32GotExp>60</n32GotExp>
        <n32ConsumeCP>500</n32ConsumeCP>
        <n32KillCP>85</n32KillCP>
        <bIfCanControl>1</bIfCanControl>
        <eConsumeAtt>13</eConsumeAtt>
        <n32ConsumeValue>6000</n32ConsumeValue>
        <n32ConsumePer>0</n32ConsumePer>
        <n32HPRecover>0</n32HPRecover>
        <n32MPRecover>0</n32MPRecover>
        <n32GroupID>1</n32GroupID>
        <n32CollideRadius>70</n32CollideRadius>
        <n32IsLocked>1</n32IsLocked>
        <n32LockRadius>1.3</n32LockRadius>
        <n32RandomAttack>attack</n32RandomAttack>
        <un32DeathSould>Beinuyideyuanren5_Dead</un32DeathSould>
        <un32FreeSound>0</un32FreeSound>
        <un32SkillType1>1160201</un32SkillType1>
        <un32SkillType2>1500201</un32SkillType2>
        <un32SkillType3>1500201</un32SkillType3>
        <un32SkillType4>1500201</un32SkillType4>
        <un32SkillType5>0</un32SkillType5>
        <un32PassSkillID>11200201</un32PassSkillID>
        <HeadPhoto>11</HeadPhoto>
        <eCamp>0</eCamp>
        <un32ShopID>0</un32ShopID>
        <EffectID>heal_once</EffectID>
        <un32Script1>Beinuyideyuanren_line_01,Beinuyideyuanren_line_02</un32Script1>
        <n32Script1Rate>90050,90050</n32Script1Rate>
    </info>

ReadNpcConfig.cs

using System;
using UnityEngine;
using System.Xml;
using System.Collections.Generic;

//这里和ReadHeroConfig那里有一些是相似的

//对应配置文件:Assets/Resources/Config/NPCCfg.xml
//这个配置文件很大
public class ReadNpcConfig
{
    XmlDocument xmlDoc = null;

    //构造函数
    public ReadNpcConfig (string xmlFilePath)
    {
        ResourceUnit xmlfileUnit = ResourcesManager.Instance.loadImmediate (xmlFilePath, ResourceType.ASSET);
        TextAsset xmlfile = xmlfileUnit.Asset as TextAsset;

        if (!xmlfile)
        {
            Debug.LogError(" error infos: 没有找到指定的xml文件:" + xmlFilePath);
        }

        xmlDoc = new XmlDocument ();
        xmlDoc.LoadXml (xmlfile.text);

        XmlNodeList infoNodeList = xmlDoc.SelectSingleNode ("NPCCfg").ChildNodes;

        for (int i = 0; i < infoNodeList.Count; i++)
        {
            if ((infoNodeList[i] as XmlElement).GetAttributeNode("un32ID") == null)
            {
                continue;
            }

            string typeName = (infoNodeList [i] as XmlElement).GetAttributeNode ("un32ID").InnerText;

            NpcConfigInfo npcSelectInfo = new NpcConfigInfo ();
            npcSelectInfo.NpcId = Convert.ToInt32 (typeName);

            foreach (XmlElement xEle in infoNodeList[i].ChildNodes)
            {
                switch (xEle.Name)
                {
                case "szNOStr":
                    npcSelectInfo.NpcName = Convert.ToString (xEle.InnerText);
                    break;

                case "eNpcType":
                    npcSelectInfo.NpcType = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "eNpcChildType":
                    npcSelectInfo.ENPCCateChild = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "XuetiaoHeight":
                    npcSelectInfo.NpcXueTiaoHeight = Convert.ToSingle (xEle.InnerText);
                    break;

                case "eRace":
                    npcSelectInfo.NpcRace = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "eAttendantCate":
                    npcSelectInfo.NpcAttendantCate = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "eMagicCate":
                    npcSelectInfo.NpcMagicCate = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "n32AttackDist":
                    npcSelectInfo.NpcAtkDis = Convert.ToSingle (xEle.InnerText);
                    break;

                case "eAICate":
                    npcSelectInfo.NpcAiCate = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "un32AITarID":
                    npcSelectInfo.NpcAiTarId = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "un32PursueDist":
                    npcSelectInfo.NpcPursueDis = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32GuardDist":
                    npcSelectInfo.NpcGuardDis = Convert.ToSingle (xEle.InnerText);
                    break;

                case "eAttackMode":
                    npcSelectInfo.NpcAtkMode = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "n32AttackPower":
                    npcSelectInfo.NpcAtkPower = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32MagAttackPower":
                    npcSelectInfo.NpcMagAtkPower = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32DefenseAbility":
                    npcSelectInfo.NpcDef = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32MagDefenseAbility":
                    npcSelectInfo.NpcMagDef = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32MoveSpeed":
                    npcSelectInfo.NpcMoveSpeed = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32BaseMoveSpeedScaling":
                    npcSelectInfo.n32BaseMoveSpeedScaling = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32AttackCD":
                    npcSelectInfo.NpcAtkCd = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32MaxHP":
                    npcSelectInfo.NpcMaxHp = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32MaxMP":
                    npcSelectInfo.NpcMaxMp = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32GotExp":
                    npcSelectInfo.NpcGotExp = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32ConsumeCP":
                    npcSelectInfo.NpcConsumeCp = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32KillCP":
                    npcSelectInfo.NpcKillCp = Convert.ToSingle (xEle.InnerText);
                    break;

                case "bIfCanControl":
                    npcSelectInfo.NpcCanControl = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "n32HPRecover":
                    npcSelectInfo.NpcHpRecover = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32MPRecover":
                    npcSelectInfo.NpcMpRecover = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32CollideRadius":
                    npcSelectInfo.NpcCollideRadius = Convert.ToSingle (xEle.InnerText);
                    break;

                case "un32WalkSound":
                    npcSelectInfo.NpcWalkSound = Convert.ToString (xEle.InnerText);
                    break;

                case "un32DeathSould":
                    npcSelectInfo.NpcDeathSould = Convert.ToString (xEle.InnerText);
                    break;

                case "un32SkillType1":
                    npcSelectInfo.NpcSkillType1 = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "un32SkillType2":
                    npcSelectInfo.NpcSkillType2 = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "un32SkillType3":
                    npcSelectInfo.NpcSkillType3 = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "un32SkillType4":
                    npcSelectInfo.NpcSkillType4 = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "un32SkillType5":
                    npcSelectInfo.NpcSkillType5 = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "HeadPhoto":
                    npcSelectInfo.HeadPhoto = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "eCamp":
                    npcSelectInfo.NpcCamp = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "un32ShopID":
                    npcSelectInfo.un32ShopID = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "BuildsDeathEffID":
                    npcSelectInfo.NpcJianTaDeath = Convert.ToString (xEle.InnerText);
                    break;

                case "n32LockRadius":
                    npcSelectInfo.n32LockRadius = Convert.ToSingle (xEle.InnerText);
                    break;

                case "n32Script1Rate":
                    npcSelectInfo.n32Script1Rate = Convert.ToString (xEle.InnerText);
                    break;

                case "n32Script1":
                    npcSelectInfo.un32Script1 = Convert.ToString (xEle.InnerText);
                    break;

                case "n32AttackSpeed":
                    npcSelectInfo.NpcAttackSpeed = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "n32IsLocked":
                    npcSelectInfo.CanLock = (Convert.ToInt32 (xEle.InnerText) == 1) ? true : false;
                    break;
                }
            }

            ConfigReader.npcXmlInfoDict.Add (npcSelectInfo.NpcId, npcSelectInfo);
        }
    }
}

/*
XML格式:
<info un32ID="20006">
        <szNOStr>Kulouqishi_5</szNOStr>
        <eNpcType>1</eNpcType>
        <eNpcChildType>3</eNpcChildType>
        <XuetiaoHeight>4</XuetiaoHeight>
        <EmitPos>0,1200,0</EmitPos>
        <HitPos>0,1200,0</HitPos>
        <eRace>4</eRace>
        <eAttendantCate>1</eAttendantCate>
        <eMagicCate>2</eMagicCate>
        <eAttackWay>1</eAttackWay>
        <n32AttackDist>230</n32AttackDist>
        <eAICate>2</eAICate>
        <un32AITarID>0</un32AITarID>
        <n32PursueDist>1400</n32PursueDist>
        <n32GuardDist>1000</n32GuardDist>
        <eAttackMode>0</eAttackMode>
        <n32AttackPower>90</n32AttackPower>
        <n32MagAttackPower>0</n32MagAttackPower>
        <n32DefenseAbility>0</n32DefenseAbility>
        <n32MagDefenseAbility>0</n32MagDefenseAbility>
        <n32MoveSpeed>570</n32MoveSpeed>
        <n32BaseMoveSpeedScaling>1200</n32BaseMoveSpeedScaling>
        <n32AttackSpeed>1500</n32AttackSpeed>
        <n32MaxHP>380</n32MaxHP>
        <n32MaxMP>0</n32MaxMP>
        <n32GotExp>65</n32GotExp>
        <n32ConsumeCP>600</n32ConsumeCP>
        <n32KillCP>93</n32KillCP>
        <bIfCanControl>1</bIfCanControl>
        <eConsumeAtt>5</eConsumeAtt>
        <n32ConsumeValue>35</n32ConsumeValue>
        <n32ConsumePer>0</n32ConsumePer>
        <n32HPRecover>0</n32HPRecover>
        <n32MPRecover>0</n32MPRecover>
        <n32GroupID>0</n32GroupID>
        <n32CollideRadius>70</n32CollideRadius>
        <n32IsLocked>1</n32IsLocked>
        <n32LockRadius>0.9</n32LockRadius>
        <n32RandomAttack>attack</n32RandomAttack>
        <un32DeathSould>Jinglingnan5_Dead</un32DeathSould>
        <un32FreeSound>0</un32FreeSound>
        <un32SkillType1>1160501</un32SkillType1>
        <un32SkillType2>1500501</un32SkillType2>
        <un32SkillType3>1500501</un32SkillType3>
        <un32SkillType4>1500501</un32SkillType4>
        <un32SkillType5>0</un32SkillType5>
        <un32PassSkillID>11200101</un32PassSkillID>
        <HeadPhoto>20</HeadPhoto>
        <eCamp>0</eCamp>
        <un32ShopID>0</un32ShopID>
        <un32Script1>Kulouqishi_line_01,Kulouqishi_line_02</un32Script1>
        <n32Script1Rate>90050,90050</n32Script1Rate>
    </info>
*/
public class NpcConfigInfo: System.Object
{
    #region NPC信息
    public string NpcName;
    public int NpcId;
    public int NpcType;
    public int ENPCCateChild;
    public float NpcXueTiaoHeight;
    public int NpcRace;
    public int NpcAttendantCate;
    public int NpcMagicCate;
    public float NpcAtkDis;
    public int NpcAiCate;
    public int NpcAiTarId;
    public float NpcPursueDis;
    public float NpcGuardDis;
    public int NpcAtkMode;
    public float NpcAtkPower;
    public float NpcMagAtkPower;
    public float NpcDef;
    public float NpcMagDef;
    public float NpcMoveSpeed;
    public float n32BaseMoveSpeed;
    public float n32BaseMoveSpeedScaling;
    public float NpcAtkCd;
    public float NpcMaxHp;
    public float NpcMaxMp;
    public float NpcGotExp;
    public float NpcConsumeCp;
    public float NpcKillCp;
    public int NpcCanControl;
    public float NpcHpRecover;
    public float NpcMpRecover;
    public float NpcCollideRadius;
    public string NpcWalkSound;
    public string NpcDeathSould;
    public int NpcSkillType1;
    public int NpcSkillType2;
    public int NpcSkillType3;
    public int NpcSkillType4;
    public int NpcSkillType5;
    public int HeadPhoto;
    public int NpcCamp;
    public int un32ShopID;
    public string NpcJianTaDeath;
    public float n32LockRadius;
    public string n32Script1Rate;
    public string un32Script1;
    public int NpcAttackSpeed;
    public bool CanLock;
    #region
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值