ConfigReader(十六)—— ReadGuideKillNpcTaskConfig

本文介绍了一个Unity3D游戏中新手教程杀怪任务的配置读取方法。通过C#脚本ReadGuideKillNpcTaskConfig.cs从XML配置文件中加载任务详情,包括NPC ID、击杀次数及死亡原因等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

对应配置文件:
Assets/Resources/Config/killnpc.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<killnpc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <info id="3001">
        <deadnpcid>21019</deadnpcid>
        <times>1</times>
        <deadreason>0,1,2</deadreason>
    </info>
    <info id="3002">
        <deadnpcid>21023</deadnpcid>
        <times>1</times>
        <deadreason>-1</deadreason>
    </info>
    <info id="3003">
        <deadnpcid>21021</deadnpcid>
        <times>1</times>
        <deadreason>-1</deadreason>
    </info>
</killnpc>

ReadGuideKillNpcTaskConfig.cs

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

//新手教程相关
//对应的配置文件:Assets/Resources/Config/killnpc.xml
public class ReadGuideKillNpcTaskConfig
{
    XmlDocument xmlDoc = null;

    //构造函数
    public ReadGuideKillNpcTaskConfig(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 ("killnpc").ChildNodes;

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

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

            //在Guide那部分定义
            CKillTask killInfo = new CkillTask ();
            killInfo.TaskId = Convert.ToInt32 (typeName);
            killInfo.TaskType = GuideTaskType.KillType;

            foreach(XmlElement xEle in infoNodeList[i].ChildNodes)
            {
                switch (xEle.Name)
                {
                case "deadtype":
                    killInfo.DeadType = (DeadType)Convert.ToInt32 (xEle.InnerText);
                    break;

                case "deadnpcid":
                    killInfo.DeadId = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "times":
                    killInfo.DeadTimes = Convert.ToInt32 (xEle.InnerText);
                    break;

                case "deadreason":
                    string reason = Convert.ToString (xEle.InnerText);
                    if (reason.IndexOf(',') != -1)
                    {
                        killInfo.KillReason = GameMethod.ResolveToIntList (reason);
                    }
                    else
                    {
                        killInfo.KillReason.Add (Convert.ToInt32 (reason));
                    }
                }
            }

            CTaskBase.killTaskDic.Add (killInfo.TaskId, killInfo);
        }
    }
}

/*
格式还是写在这里供参考
<info id="3001">
    <deadnpcid>21019</deadnpcid>
    <times>1</times>
    <deadreason>0,1,2</deadreason>
</info>
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值