2 回答

TA貢獻1860條經驗 獲得超8個贊
請注意,接受答案的while循環會完全阻止編輯器,直到下載完成。對于簡單的文本可能沒問題,但對于較大的文件,這可能會成為一個問題。
但是EditorApplication.update,您可以訂閱以便在編輯器中的每一幀調用一個方法。所以對于 EditorWindow 你可以做類似的事情
private IEnumerator currentDownload;
private void ProcessDownload()
{
if(currentDownload!=null) currentDownload.MoveNext();
}
private IEnumerator UpdateVersion(string message)
{
string post_url = NetworkManager.baseUrl + "VersionUpdate.php";
WWWForm form = new WWWForm();
form.AddField("Message", message);
form.AddField("Version", Application.version);
UnityWebRequest www = UnityWebRequest.Post(post_url, form);
www.chunkedTransfer = false;
yield return www.SendWebRequest();
if(www.error == null){
Debug.Log(www.downloadHandler.text);
} else {
Debug.Log("error!: " + www.error);
}
}
并像使用它
// makes sure the callback is added only once
EditorApplication.update -= ProcessDownload;
EditorApplication.update += ProcessDownload;
currentDownload = UpdateVersion("whatever string");

TA貢獻1794條經驗 獲得超8個贊
我遇到了同樣的問題。我最終得到了以下代碼,它以非協程方法執行 webrequest。
private void UpdateVersion(string message)
{
string post_url = NetworkManager.baseUrl + "VersionUpdate.php";
WWWForm form = new WWWForm();
form.AddField("Message", message);
form.AddField("Version", Application.version);
UnityWebRequest www = UnityWebRequest.Post(post_url, form);
www.chunkedTransfer = false;
www.SendWebRequest();
while (!www.isDone)
{
// do nothing
}
if(www.error == null){
Debug.Log(www.downloadHandler.text);
} else {
Debug.Log("error!: " + www.error);
}
}
- 2 回答
- 0 關注
- 198 瀏覽
添加回答
舉報