41 lines
1.3 KiB
C#

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);
}));
}
/// <summary>
/// ¶ÁÈ¡StreamingAssetÖеÄÅäÖÃÎļþ
/// </summary>
IEnumerator TextReader(string configName, UnityAction<string> 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);
}
}
}
}
}