因为之前没做过web端,所以不清楚,以为做好window端即可多端发布,结果踩了大坑,unityWeb的数据持久化和读取配置文件,不能直接通过读取文件夹文件,而是通过协程进行读取制定文件夹,
比如:Application.persistentDataPath文件夹只能在window和移动端读取,发布为web端是读取不了,会报wasm错误,如果过需要读取则采用协程的方式进行读取,具体代码如下
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using Newtonsoft.Json;
public class AppConfig : MonoBehaviour
{
public Config config;
public static AppConfig Instance;
private void Awake()
{
Instance = this;
StartCoroutine(GetAppConfig());
}
IEnumerator GetAppConfig()
{
string url = Path.Combine(Application.streamingAssetsPath,"AppConfig.txt");
UnityWebRequest unityRequest=UnityWebRequest.Get(url);
yield return unityRequest.SendWebRequest();
if (unityRequest.result!=UnityWebRequest.Result.Success)
{
Debug.Log("Is error");
}
else
{
this.URL= unityRequest.downloadHandler.text;
config = JsonConvert.DeserializeObject<Config>(URL);
}
}
}
Appconfig的配置就是你需要读取的文件,支持json和txt,其他如ini等没测试,但是这已经满足一般的项目需求,简单的问题总结,方便今后复盘,希望给刚入门的同行提供思路!