[Unity]在Webgl 读取XML文件

   public void LoadXMLDocument(string fileName)
        {
            string filePath = Application.streamingAssetsPath + "/" + fileName;
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
            url = "file://" + filePath; // 在PC端时,需要在文件路径前加上file://协议
#elif UNITY_WEBGL
    url = filePath; // 在WebGL端直接使用相对路径即可
#endif
            // 构建xml文件的路径

            // 创建XmlDocument对象
            XmlDocument xmlDoc = new XmlDocument();

            // 加载xml文件
            UnityWebRequest request = UnityWebRequest.Get(filePath);
            request.SendWebRequest();

            while (!request.isDone) ;

            if (request.isNetworkError || request.isHttpError)
            {
                Debug.LogError("加载xml文件失败:" + request.error);
                return;
            }
            else
            {
                xmlDoc.LoadXml(request.downloadHandler.text);
            }

            // 处理xml文件
            // ...

            Debug.Log("加载xml文件成功!");
        }

猜你喜欢

转载自blog.csdn.net/weixin_46472622/article/details/130077571
今日推荐