Search Unity

Editor function to run every second ?

Discussion in 'Immediate Mode GUI (IMGUI)' started by EMOTION-THEORY, Apr 2, 2016.

  1. EMOTION-THEORY

    EMOTION-THEORY

    Joined:
    Jul 16, 2013
    Posts:
    68
    I have an editor extension (inherits from Editor) where I'm running stuff in OnSceneGUI ( ).

    The problem is, the code only runs when the scene view is being updated. I'd like my code to run every second rather than contain it in OnSceneGUI.

    Any ideas on how to do that?

    Much appreciated :)

    EDIT: Rather than make a new thread - does anybody also happen to know how to detect sleep / idle in an Editor script? This would be in the same script as above!
     
  2. seldom

    seldom

    Joined:
    Dec 4, 2013
    Posts:
    109
    Write an update method and subscribe it to EditorApplication.update (and unsubscribe in OnDestroy). In it, use EditorApplication.timeSinceStartup to run your code once per second.
     
  3. EMOTION-THEORY

    EMOTION-THEORY

    Joined:
    Jul 16, 2013
    Posts:
    68
    Thanks, this did the trick :)

    I also found that when the computer is sleeping (closed my Macbook) it did not continue to run update. It did, however, continue running update even when I switched to other apps, and even when I had minimised it to the dock.

    All in all, exactly what I was after!

    I couldn't get OnDestroy to run though, because my script isn't technically an Editor Window, but a SerializedObject. Is there any danger for it not to be Destroyed when the Unity Editor is shut down?
     
  4. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    525
    There is also extension called EditorCoroutines, can also be used for that.