Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by Aurelinator · Feb 28, 2012 at 01:19 AM · editorcoroutinewwweditorwindowyield

Yielding with WWW in Editor

Hello!

I'm working on an editor tool that needs some SQL connectivity. It is an internal bug tracker. I've written a php script that will return the data I need formatted correctly, and it works, but, I have to force the application to wait using the disgusting while(!www.isDone) approach.

In my EditorWindow, I have a function:

 public void RefreshData() {
     SQL.query("SELECT * FROM users");         
 }

Whereas, the query method in question is somewhat like this.

 public static void query(string q) {
     WWW www = new WWW("...?query="+WWW.EscapeURL(q));
     while(!www.isDone);
     if(www.error != null) 
         Debug.LogError(www.error);     
     else 
         Debug.Log(www.text);
 }

This works beautifully... obviously, I'll get it to return the necessary data when I write the parsing functionality, but my question is: Is there any way I can used the yield return www; to get it to not hang the Unity Editor when I have more queries to parse?

Since this is a static method built into a static editor class, I cannot attach a MonoBehavior to anything... thus, I cannot use the StartCoroutine method. Is there some C# interface I can implement on my EditorWindow that requires a Coroutine Scheduler?

Is there any hope for me, or will my tool forever hang?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sebas77 · Apr 10, 2012 at 01:21 PM 0
Share

why do you say obviously? I would say that this works only if www is multithread. I did not know it was multithread.

avatar image Aurelinator · Apr 10, 2012 at 02:49 PM 0
Share

It is not. That is the problem.

avatar image sebas77 · Apr 10, 2012 at 02:52 PM 0
Share

weird because I did the while like you did and it works

avatar image marijnz · Jan 09, 2015 at 01:25 PM 0
Share

Hey, so even though this question is a couple of years old, I made a plugin that that makes it possible to use coroutines in Editor code: http://forum.unity3d.com/threads/released-editor-coroutines.289703/

9 Replies

· Add your reply
  • Sort: 
avatar image
10
Wiki

Answer by kjems · Apr 22, 2012 at 03:47 PM

I made a ContinuationManager to handle the cases where I want to wait for a condition and then do something with an object.

The snippet below is an example of WWW using the ContinuationManager where the condition to trigger the continuation is www.isDone. The lambda closure captures the www object so it can be used when the www is done. The code is non-blocking.

 var www = new WWW("someURL");
 ContinuationManager.Add(() => www.isDone, () =>
 {
     if (!string.IsNullOrEmpty(www.error)) Debug.Log("WWW failed: " + www.error);
     Debug.Log("WWW result : " + www.text);
 });



The ContinuationManager

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEditor;
 
 internal static class ContinuationManager
 {
     private class Job
     {
         public Job(Func<bool> completed, Action continueWith)
         {
             Completed = completed;
             ContinueWith = continueWith;
         }
         public Func<bool> Completed { get; private set; }
         public Action ContinueWith { get; private set; }
     }
 
     private static readonly List<Job> jobs = new List<Job>();
 
     public static void Add(Func<bool> completed, Action continueWith)
     {
         if (!jobs.Any()) EditorApplication.update += Update;
         jobs.Add(new Job(completed, continueWith));
     }
 
     private static void Update()
     {
         for (int i = 0; i >= 0; --i)
         {
             var jobIt = jobs[i];
             if (jobIt.Completed())
             {
                 jobIt.ContinueWith();
                 jobs.RemoveAt(i);
             }
         }
         if (!jobs.Any()) EditorApplication.update -= Update;
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image createdbyx · Nov 24, 2015 at 09:33 AM 1
Share

$$anonymous$$ade gist with a few more features and xml documentation comments https://gist.github.com/createdbyx/2c43c0518f622a652442

avatar image peteradvr · Jan 02, 2017 at 08:27 PM 0
Share

Up until Unity 5.5 this was working great for us. Starting at Unity 5.5 it stopped working. Any ideas what might have changed from Unity to break this?

avatar image
2

Answer by Gru · Aug 03, 2016 at 06:09 PM

My solution: http://www.ennoble-studios.com/tuts/making-unitys-www-class-work-in-editor-scripts.html

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Durston88 · Mar 19, 2018 at 11:14 PM 0
Share

Hah, didn't see there was a 2nd page before posting my own. #4 on this link actually looks promising.

avatar image
1

Answer by DaveA · Feb 28, 2012 at 02:00 AM

Does it have to be static? I've had pretty good luck using MonoBehaviour and ExecuteInEditMode, use Update to check isDone.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aurelinator · Feb 29, 2012 at 07:32 PM 0
Share

As great as that might be, this is supposed to work for every scene. A $$anonymous$$onoBehavior object needs to be attached to something in the Game for it to even be instantiated - I need something that can exist in any scene and be a permanent fixture in the entire project.

avatar image by0log1c · Apr 10, 2012 at 01:32 PM 0
Share

How about...wait for it... a singleton with DontDestroyOnLoad()! Another day, another singleton post from me :D - Oops, this question is 2 months old x(

avatar image
1

Answer by Skjalg · Sep 28, 2012 at 06:24 AM

In order to yield in an editor method you need to use a regular c# Thread.

http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image markgrossnickle · Oct 04, 2017 at 09:06 PM 0
Share

Does not work. WWW needs to be on the main thread.

avatar image
0

Answer by Aurelinator · Apr 10, 2012 at 03:39 PM

So. Im saying that this class needs to exist independent of what's loaded in the scene. It is a bug tracker that connects to a database to retrieve bugs. If I create a brand new scene - I don't want to have to go ahead and create an object that for some magical reason uses game time to do something that ought to be simple.

I had just wondered if anyone had found out how to avoid the lag associated with do a WWW request in editor only code.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sebas77 · Apr 10, 2012 at 04:05 PM 0
Share

Honestly I had the same problem, but since the lag is not an issue for me, for the time being I use www synchronously. I watch this question in case you find a solution.

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

yield return request never returns 2 Answers

yield on a www never completes 10 Answers

WWW in Editor getting request with null attributes 1 Answer

Loading files via WWW class and Unity hangs for a few seconds if I use coroutines. If I don't, it works perfectly. What is going on? 1 Answer

How to imitate yield functionality like WWW class 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges