随机生成游戏角色昵称(在Unity中解析XML配置文件)下

上一篇文章我们说到了,配置文件的生成,接下来继续,也就是
1、Unity中,使用资源加载系统去 加载配置文件(客户端)
2、在C# 文件 IO 接口加载文件(服务器)
所以这里直接就上源代码了:上代码之前看把 XML 贴出来吧
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<item ID="1">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="2">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="3">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="4">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="5">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="6">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="7">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="8">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="9">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="10">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="11">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="12">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="13">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="14">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="15">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="16">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="17">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="18">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="19">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="20">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="21">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="22">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="23">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="24">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="25">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="26">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="27">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="28">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="29">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="30">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="31">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="32">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="33">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="34">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="35">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="36">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="37">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="38">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="39">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="40">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="41">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="42">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="43">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="44">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="45">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="46">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="47">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="48">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="49">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="50">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="51">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="52">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="53">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="54">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="55">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="56">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="57">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="58">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="59">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="60">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="61">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="62">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="63">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="64">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="65">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="66">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="67">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="68">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="69">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="70">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="71">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="72">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="73">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="74">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="75">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="76">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="77">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="78">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="79">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="80">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="81">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="82">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="83">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="84">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="85">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="86">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="87">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="88">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="89">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="90">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="91">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="92">
		<surname></surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="93">
		<surname>司马</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="94">
		<surname>上官</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="95">
		<surname>欧阳</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="96">
		<surname>夏侯</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="97">
		<surname>诸葛</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="98">
		<surname>闻人</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="99">
		<surname>东方</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="100">
		<surname>赫连</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="101">
		<surname>皇甫</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="102">
		<surname>尉迟</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="103">
		<surname>澹台</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="104">
		<surname>公冶</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="105">
		<surname>公输</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="106">
		<surname>淳于</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="107">
		<surname>单于</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="108">
		<surname>申屠</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="109">
		<surname>公孙</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="110">
		<surname>仲孙</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="111">
		<surname>轩辕</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="112">
		<surname>令狐</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="113">
		<surname>钟离</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="114">
		<surname>宇文</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="115">
		<surname>长孙</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="116">
		<surname>慕容</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="117">
		<surname>鲜于</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="118">
		<surname>闾丘</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="119">
		<surname>司徒</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="120">
		<surname>司空</surname>
		<man></man>
		<woman></woman>
	</item>
	<item ID="121">
		<surname>屠苏</surname>
		<man></man>
	</item>
	<item ID="122">
		<surname>端木</surname>
		<man></man>
	</item>
	<item ID="123">
		<surname>乐正</surname>
		<man></man>
	</item>
	<item ID="124">
		<surname>拓跋</surname>
		<man></man>
	</item>
	<item ID="125">
		<surname>夹谷</surname>
		<man></man>
	</item>
	<item ID="126">
		<surname>谷梁</surname>
		<man></man>
	</item>
	<item ID="127">
		<surname></surname>
		<man></man>
	</item>
	<item ID="128">
		<surname>百里</surname>
		<man></man>
	</item>
	<item ID="129">
		<surname>南门</surname>
		<man></man>
	</item>
	<item ID="130">
		<surname>呼延</surname>
	</item>
	<item ID="131">
		<surname>归海</surname>
	</item>
	<item ID="132">
		<surname></surname>
	</item>
	<item ID="133">
		<surname></surname>
	</item>
	<item ID="134">
		<surname></surname>
	</item>
	<item ID="135">
		<surname>梁丘</surname>
	</item>
	<item ID="136">
		<surname>左丘</surname>
	</item>
	<item ID="137">
		<surname>西门</surname>
	</item>
	<item ID="138">
		<surname></surname>
	</item>
	<item ID="139">
		<surname>南宫</surname>
	</item>
	<item ID="140">
		<surname>第五</surname>
	</item>
</root>

紧接着就是随机数工具类(无需挂载)以及 C#源码,直接挂在 相机身上即可

//==========================
// - FileName:      PETools.cs         
// - Created:       true.	
// - CreateTime:    2020/7/28 22:58:53	
// - Email:         1670328571@qq.com		
// - Region:        China WUHAN	
// - Description:   随机工具类
//==========================
using System;

public class PETools 
{
    public static int RDInt(int min, int max,Random rd = null) 
    {
        if (rd == null) 
        {
            rd = new Random();
        }
        //包含前面一个数,不包含后面一个数
        int val = rd.Next(min, max + 1);
        return val;
    }
}

//==========================
// - FileName:      XmlAnalysis.cs         
// - Created:       true.	
// - CreateTime:    2020/7/29 1:14:28	
// - Email:         1670328571@qq.com		
// - Region:        China WUHAN	
// - Description:   Xml读取
//==========================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;

public class XmlAnalysis : MonoBehaviour
{
    //姓氏列表
    public List<string> surnameList = new List<string>();
    //男性名字
    public List<string> manList = new List<string>();
    //女性名字
    public List<string> womanList = new List<string>();

    public const string RdNameConfig = "ResCfgs/rdname";
    void Start()
    {
        InitRdConfigName();
        InvokeRepeating("Test", 1f, 1f);
    }

    private void Test()
    {
        Debug.Log(GetRdNameData(false));
        Debug.Log(GetRdNameData());
    }    

    private void InitRdConfigName()
    {
        //读取
        TextAsset xml = Resources.Load<TextAsset>(RdNameConfig);
        //找到子节点
        if (!xml)
        {
            Debug.LogError("路径错误" + RdNameConfig);
        }
        else
        {
            //需要引入 using System.Xml; 命名空间,
            XmlDocument doc = new XmlDocument();
            //加载文本
            doc.LoadXml(xml.text);
            //加载完成以后使用、root 为xml 根节点,不要写错了哦
            XmlNodeList nodeList = doc.SelectSingleNode("root").ChildNodes;
            //遍历子节点
            for (int i = 0; i < nodeList.Count; i++)
            {
                //拿到某一个
                XmlElement xmlElement = nodeList[i] as XmlElement;
                //ID 不存在的时候直接跳过当前
                if (xmlElement.GetAttributeNode("ID") == null)
                {
                    continue;
                }
                //将获取到的字符串转换为想要的数据类型
                int ID = Convert.ToInt32(xmlElement.GetAttributeNode("ID").InnerText);
                //拿到 节点数据以后遍历每一条属性,然后保存起来
                foreach (XmlElement e in nodeList[i].ChildNodes)
                {
                    switch (e.Name)
                    {
                        //存储到列表中 
                        case "surname":
                            surnameList.Add(e.InnerText);
                            break;
                        case "man":
                            manList.Add(e.InnerText);
                            break;
                        case "woman":
                            womanList.Add(e.InnerText);
                            break;
                    }
                }
            }
        }
    }

    /// <summary>
    /// 获取随机昵称
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    private string GetRdNameData(bool man = true)
    {
        //使用随机数工具类
        System.Random rd = new System.Random();
        //随即范围为 0 - 基数减1
        string rdName = surnameList[PETools.RDInt(0, surnameList.Count - 1)];
        //如果是男性的话
        if (man)
        {
            rdName += manList[PETools.RDInt(0, manList.Count - 1)];
        }
        else
        {
            //女性的时候
            rdName += manList[PETools.RDInt(0, womanList.Count - 1)];
        }
        return rdName;
    }
}

最后就是运行结果图了:
在这里插入图片描述
完美运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值