1. 使用 WaitUntil ,如下图所示:
protected virtual IEnumerator UpdateConfig() { configJsonStr = ""; using UnityWebRequest request = UnityWebRequest.Get(configJsonUri); UnityWebRequestAsyncOperation operation = request.SendWebRequest(); yield return new WaitUntil(() => { return operation.isDone; }); try { switch (request.result) { case UnityWebRequest.Result.Success: configJsonStr = request.downloadHandler.text; Debug.LogFormat("【获取配置文件】成功:fileUri={0} fileStr=\r\n{1}", configJsonUri, configJsonStr); break; case UnityWebRequest.Result.InProgress: break; default: Debug.LogErrorFormat("【获取配置文件】失败:fileUri={0} errMsg={1}", configJsonUri, request.error); break; } } catch (Exception ex) { Debug.LogException(ex); } }
2. 参考文档
https://docs.unity.cn/cn/current/ScriptReference/WaitUntil.html