Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UnityWebRequest editor script

Discussion in 'Scripting' started by r3dwolf, Jul 15, 2016.

  1. r3dwolf

    r3dwolf

    Joined:
    Feb 16, 2012
    Posts:
    39
    Hello everybody,
    I tried to use the new UnityWebRequest class in an editor script.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4. using UnityEngine.Experimental.Networking;
    5.  
    6. public class UnityWebRequestWindow:EditorWindow{
    7.  
    8.     [MenuItem ("Window/UnityWebRequest")]
    9.     public static void  ShowWindow () {
    10.         EditorWindow.GetWindow(typeof(UnityWebRequestWindow));
    11.     }
    12.     void OnGUI () {
    13.         if (GUILayout.Button ("New Web request")) {
    14.             UnityWebRequest uwr = UnityWebRequest.Get("http://www.unity3d.com");
    15.             uwr.Send ();
    16.             while (!uwr.isDone) {
    17.             }
    18.             if (uwr.isError) {
    19.                 Debug.Log ("Error");
    20.             }
    21.             Debug.Log ("uwr.downloadHandler.text");
    22.             uwr.Dispose ();
    23.         }
    24.     }
    25. }
    26.  
    I found that CPU usage raises incrementally at every request!!
    if i switch to play mode, this problem disappear.
    There is something wrong in my script or it is a bug?

    Thanks in advance
     
  2. r3dwolf

    r3dwolf

    Joined:
    Feb 16, 2012
    Posts:
    39
  3. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    Could be the busy wait consume all the cpu time? Try this instead,

    while (!request.isDone)
    Thread.Sleep(50);
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,294
    That will block the main thread as well.

    In theory you should start a coroutine and use yields instead.
    But coroutines in editor should be updated manually via something like Editor.update callback as they do not work properly as in runtime.

    Also, do not necro ancient threads.