动态抽取姓名, 思路 用xml 先编写好一些名字,然后把这些名字加载到 list 用random 取里的名名字
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Load : MonoBehaviour
{
public GameObject PanelIntro;
public GameObject ShuoMing;
public InputField name;
private string filepath;
private List ListName = new List();
// Use this for initialization
void Start()
{
filepath = Application.streamingAssetsPath + @"/name.xml";
LoadXml();
}
// Update is called once per frame
void Update()
{
CStaticValue.Name = name.text;
Debug.Log(CStaticValue.Name);
}
public void ChangeName()
{
int ran = Random.Range(0, ListName.Count);
name.text = ListName[ran];
}
void LoadXml()
{
if (File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("transforms").ChildNodes;
foreach (XmlElement xe in nodeList)
{
foreach (XmlElement x1 in xe.ChildNodes)
{
if (x1.Name == "name")
{
ListName.Add(x1.InnerText);
}
}
}
}
}
public void ShowShuoMing()
{
ShuoMing.SetActive(true);
}
public void HideShuoMing()
{
ShuoMing.SetActive(false);
}
public void StartGame()
{
PanelIntro.SetActive(false);
}
public void StartToMain()
{
SceneManager.LoadScene("Main");
}
public void PaiHang()
{
CStaticValue.Agagin = false;
SceneManager.LoadScene("PaiHang");
}
}