本帖最后由 巫人 于 2021-8-14 11:29 编辑
请问await是要等待所有主线程的代码执行完毕后再执行么?
[C#] 纯文本查看 复制代码 static SearchOperation()
{
Initialize();
}
internal static async void Initialize()
{
var dictionary = await ReadLocalJson<Dictionary<string, List<Dictionary<string, string>>>>(
RuntimePlatform.WindowsPlayer, Application.streamingAssetsPath + "/Config/Path.json");
ListConfig = dictionary["AllConfigPath"];
}
internal static async Task<T> ReadLocalJson<T>(RuntimePlatform platform, string url) where T : ICollection
{
UnityWebRequest unityWebRequest = UnityWebRequest.Get(GetLocalUrl(platform, url));
await unityWebRequest.SendWebRequest();
if (unityWebRequest.result == UnityWebRequest.Result.ProtocolError ||unityWebRequest.result == UnityWebRequest.Result.ConnectionError)
{
Debug.Log("error=" + unityWebRequest.error);
throw new Exception("读取JSON文件出错");
}
var dictionary = JsonMapper.ToObject<T>(unityWebRequest.downloadHandler.text);
return dictionary;
}
//使用代码
//ReadUIJson=ReadLocalJson
UIModelCollection.Instance.ReadUIJson(RuntimePlatform.WindowsPlayer,
Application.streamingAssetsPath + "/Config/UIModelConfig/UIPrefabs.json");
GameObject go = Instantiate(UIModelCollection.Instance.LoadGameObject("Button.prefab"));
go.name = "1111";
我这样使用的话他会先遍历ListConfig导致报空错误,就是ReadLocalJson还没执行完就执行了LoadGameObject这个函数,LoadGameObject里做遍历ListConfig操作,结果就是报空错误,请问怎么解决呢?好烦搞了好几天了
如果我后面的遍历ListConfig操作稍等下再做,就能读取完毕,但是现在就得要求立即遍历ListConfig |