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. Dismiss Notice

Unity Editor Extension IEnumerator?

Discussion in 'Scripting' started by vipvex, Jun 26, 2014.

  1. vipvex

    vipvex

    Joined:
    Dec 6, 2011
    Posts:
    72
    Hey guys! I'm working on a unity editor extension. In this plugin I need run a specific function in the a editor window every 2 seconds. Does anyone know how to do this?
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Bind yourself on this event;

    Code (csharp):
    1. EditorApplication.update
    And run a counter for the number of update received. I'm not sure if Time.deltaTime works in editor mode, so you may not have a fixed 2 sec counter.
     
  3. vipvex

    vipvex

    Joined:
    Dec 6, 2011
    Posts:
    72
    Got it working! Thanks.

    Code (CSharp):
    1.    
    2. void OnEnable ()
    3.     {
    4.  
    5.         EditorApplication.update = CondtionUpdate;
    6. }
    7.  
    8.     int updates = 0;
    9.     void CondtionUpdate() // Called around 100 times per sec
    10.     {
    11.         updates++;
    12.         if (updates > 200)
    13.         {
    14.             Debug.Log("Updating");
    15.             updates = 0;
    16.         }
    17.     }
    18.  
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Doesn't standard .NET have some standard time functions that could be used?
     
  5. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Sure thing.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Timers;
    4.  
    5. public class TimersDemo : MonoBehaviour {
    6.     Timer timer;
    7.     int counter = 0;
    8.  
    9.     void Start() {
    10.         timer = new Timer(1000);
    11.         timer.Elapsed += new ElapsedEventHandler(TimerEvent);
    12.         timer.Enabled = true;
    13.     }
    14.  
    15.     void Update() {
    16.         if(counter >= 5) {
    17.             timer.Enabled = false;
    18.         }
    19.     }
    20.  
    21.     void OnApplicationQuit() {
    22.         timer.Close();
    23.     }
    24.    
    25.     void TimerEvent(object source, ElapsedEventArgs e) {
    26.         counter++;
    27.         Debug.Log("TimerEvent() at " + System.DateTime.Now.ToLongTimeString());
    28.     }
    29. }
    30.