欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 国际 > 八、随机名字功能

八、随机名字功能

2024/10/23 15:20:39 来源:https://blog.csdn.net/akjhgi/article/details/142961528  浏览:    关键词:八、随机名字功能

摘要:

XML在C#与Unity3D中的实战运用 - PlaneZhong - 博客园 (cnblogs.com)

读取策划提供的配置文件。

策划提供一份execel文档,程序将它转化为一个配置文件(xml)

首先:

XML是一个可扩展标记的语言

一、转换方法

把excel文件转换为XML文件

1、制作xml文件方法

注:这只是一个模板,必须有两个数据项

2、Execel读取Xml模板的方法

右键单击 映射元素

在表中内容添加完毕后,把这个文档导出

打开即可看见

最后导出到unity的项目文件夹下

二、使用XML文件

有unity和c#两种区别

一般c#用于服务器上多

1、代码

代码如下

private List<string>surnameLst=new List<string>();//x姓
private List<string>manLst=new List<string>();//男人名字
private List<string>womanLst=new List<string>();//女人名字private void InitRDNameCfg()
{TextAsset xml = Resources.Load<TextAsset>(PathDefine.RDNameCfg);//传入路径if (!xml){Debug.LogError("xml file" + PathDefine.RDNameCfg + "not exist");}else{XmlDocument doc=new XmlDocument();doc.LoadXml(xml.text);XmlNodeList nodLst = doc.SelectSingleNode("root").ChildNodes;//查找子节点for(int i = 0;i<nodLst.Count;i++){XmlElement ele = nodLst[i] as XmlElement;if (ele.GetAttributeNode("ID") == null){continue;}int ID= Convert.ToInt32(ele.GetAttributeNode("ID").InnerText);foreach( XmlElement e in nodLst[i].ChildNodes){switch (e.Name){case "surname":surnameLst.Add(e.InnerText);break;case "man":manLst.Add(e.InnerText);break;case "woman":womanLst.Add(e.InnerText);break;}}}}}

三、完成随机名字功能

1、随机方法

定义三个List集合,存储xml文本中的姓、男人名、女人名

这里的名字组合是:每一列的组合随机选取一个,组成一个名字

解析代码

写一个初始化名字的方法

首先把XML文本的路径传给xml参数

判断一下是否加载完成

然后new一个xml文档

这个文档doc调用加载方法,加载这个路径中的文本

查找这个路径中的root根节点,然后所有的子节点都赋值给nodLst。

在for中遍历所有的子节点数量,然后把每一次遍历的结果赋值给ele这个xml文本变量

如果ID为空,那么跳出这次循环

再次遍历所有属性中的子节点

e.name就是这一个节点中的三个属性

对应的case内容分给列表里面

InnerText这个就是中间的内容

 private void InitRDNameCfg(){TextAsset xml = Resources.Load<TextAsset>(PathDefine.RDNameCfg);//传入路径if (!xml){Debug.LogError("xml file" + PathDefine.RDNameCfg + "not exist");}else{XmlDocument doc=new XmlDocument();doc.LoadXml(xml.text);XmlNodeList nodLst = doc.SelectSingleNode("root").ChildNodes;//查找子节点for(int i = 0;i<nodLst.Count;i++){XmlElement ele = nodLst[i] as XmlElement;if (ele.GetAttributeNode("ID") == null){continue;}int ID= Convert.ToInt32(ele.GetAttributeNode("ID").InnerText);foreach( XmlElement e in nodLst[i].ChildNodes){switch (e.Name){case "surname":surnameLst.Add(e.InnerText);break;case "man":manLst.Add(e.InnerText);break;case "woman":womanLst.Add(e.InnerText);break;}}}}}

2、得到随机名称

接受了一个参数,用来判断是男还是女

男女会决定是从男人名字还是女人名字列表里面取值

最终返回一个随即后的名字rdName

 public string GetRDNameData(bool man=true){System.Random rd=new System.Random();string rdName = surnameLst[PETools.RDInt(0, surnameLst.Count - 1)];if (man){rdName += manLst[PETools.RDInt(0, manLst.Count - 1)];}else{rdName += womanLst[PETools.RDInt(0, womanLst.Count - 1)];}return rdName;}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com