using System.Collections; using System.Xml; using UnityEngine; using UnityEngine.Events; using UnityEngine.Networking; public class UnityToWebMono : MonoBehaviour { public void Navigator(string value) { StartCoroutine(TextReader(value, (str) => { UnityToWeb.ToJSSendMsgByMQTT(str); })); } /// /// 读取StreamingAsset中的配置文件 /// IEnumerator TextReader(string configName, UnityAction action = null) { string path = Application.streamingAssetsPath + "/UnityToWebConfig.xml"; using UnityWebRequest unityWebRequest = UnityWebRequest.Get(path); yield return unityWebRequest.SendWebRequest(); if (unityWebRequest.error != null) Debug.Log(unityWebRequest.error); else { string content = unityWebRequest.downloadHandler.text; XmlDocument doc = new(); doc.LoadXml(content); XmlNodeList nodeList = doc.SelectSingleNode("root").ChildNodes; foreach (XmlElement element in nodeList) { if (element.Name == configName) { action?.Invoke(element.InnerText); } } } } }