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
0
Question by MSFX · Jul 30, 2014 at 10:55 PM · editor-scriptingcoroutinetimerinvoke

Editor script coroutine/timer/delay...?

I need the ability to create a delay within an Editor script to cleanup some instantiated prefabs X seconds later... the issue I've found is that Update() only appears to run when there is an interaction in the Editor and Invoke/Coroutines appear to not work at all...

Is there any way around this? I saw that someone mentioned increasing the transform position by Vector3.zero within OnGUI but again I found that only runs when there's an interaction :/

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
13
Best Answer

Answer by VesuvianPrime · Jul 30, 2014 at 11:09 PM

Hey MSFX

What you want to do is listen to the EditorApplication.update event:

 // This doesn't need to be a MonoBehaviour, you can just as easily implement
 // this in an editor script.
 public class Example : MonoBehaviour
 {
     private float m_LastEditorUpdateTime;
 
     protected virtual void OnEnable()
     {
         #if UNITY_EDITOR
         m_LastEditorUpdateTime = Time.realtimeSinceStartup;
         EditorApplication.update += OnEditorUpdate;
         #endif
     }
 
     protected virtual void OnDisable()
     {
         #if UNITY_EDITOR
         EditorApplication.update -= OnEditorUpdate;
         #endif
     }
 
     protected virtual void OnEditorUpdate()
     {
         // In here you can check the current realtime, see if a certain
         // amount of time has elapsed, and perform some task.
     }
 
 }

Comment
Add comment · 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
3

Answer by naetib · Dec 03, 2019 at 10:38 AM

Following answer for anyone need.

There are many ways to delay in editor mode


if you want to keep using low C# version (.Net 3.5 Equivalent) you can use Editor Coroutines Package: Menu Window > Package Manager > All

Simple code like this:

 static void useDelay()
 {
      object obj = new object();
      EditorCoroutineUtility.StartCoroutine(IEDelayEditor(), obj);
 }
 
 static IEnumerator IEDelayEditor()
 {
      Debug.Log("Wait 1 second");
      yield return new EditorWaitForSeconds(1f);
      Debug.Log("After 1 second");
 }
 


If you use high C# version (.Net 4.x Equivalent), you can you Async Await with many advantages

 [MenuItem("ExMenu/Use Delay")]
 static void UseDelayAsync()
 {
      DelayUseAsync();
 }
 
 async static void DelayUseAsync ()
 {
       Debug.Log("Waiting 1 second...");
       await Task.Delay(1000);
       Debug.Log("After 1 second...");
 }
 


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 awsapps · Sep 25, 2021 at 08:46 PM 0
Share

using System.Threading.Tasks;

avatar image
0

Answer by MSFX · Jul 31, 2014 at 06:38 AM

Ah, I hadn't noticed that event.... awesome, thanks! :)

Comment
Add comment · 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

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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set timer for WWW helper? 1 Answer

Problems with Color.Lerp in a Coroutine 3 Answers

Invoke isn't working for a function with yield? 2 Answers

How does Invoke cope with mapping on frames 1 Answer

invoke much faster than update timer? 1 Answer


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