Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Does Unity have any engine callbacks that a script can hook into (like Update.)

Discussion in 'Scripting' started by gtzpower, Jun 10, 2013.

Thread Status:
Not open for further replies.
  1. gtzpower

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    I am looking for a way to get a non-MonoBehavior script instance to run every frame. Currently I am handling this by calling a function in the script from a monobehaviors Update() function, but I'd like to get rid of the need for a script on a game object in the scene. Does Unity have any engine callbacks that my script can hook into or some method of getting my function to execute every frame?
     
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Unfortunately appels is correct. MonoBehaviour is an absolute requirement to get any Update() callbacks.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601

    You can have a single 'callback manager' or 'scene graph manager' which offers delegate lists and fires them.
    that way you can use a single monobehaviour to handle all non-monobehaviours.

    Why one would want to do that though is beyond me, as it cuts the Active and Hierarchy handling as well as the whole Component based, iterative development focused workflow, which is basically the very core of the engine and I personally would recheck if the engine is suitable for your needs any longer if you want to forcefully get rid of it.

    The only callback to surely get rid off from multiple monobehaviours at least is OnGUI due to its overhead.
     
  5. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    You could do something like this in a monobehaviour, then all you need to do is register to it

    Code (csharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4.  
    5. public sealed class UpdateRunner : MonoBehaviour {
    6. private static event Action UpdateTarget;
    7.  
    8. public static void Register(Action method){
    9.     UpdateTarget += method;
    10. }
    11.  
    12. public static void Unregister(Action method){
    13.     UpdateTarget -= method;
    14. }
    15.  
    16. private void Update(){
    17.     if (UpdateTarget != null)
    18.     {
    19.         UpdateTarget();
    20.     }
    21. }
    22.  
    Then you simply register your non monobehaviour update like so:

    UpdateRunner.Register(DoUpdate);

    Not sure why you would do this, but this code works.
     
  6. phobossion

    phobossion

    Joined:
    May 30, 2013
    Posts:
    2
    Well it's actually very handy for global game logic "managers", basically singletons. We use one GameManager game object that manages all these classes. This is required to keep some global game state intact when you have your levels changing (or at least the only one I've found to actually work nicely).
     
  7. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    Actually...why wouldn't this be a good idea?

    You could have many event subscribers that don't require inheriting monobehavior, that all get fired from a single Update. Maybe they would use frame or DeltaTime values from the original update function for calculations, I dunno. But it seems like using plain .net classes to do the work, instead of a Monobehavior object, would be better.
     
  8. gtzpower

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    Wow, lot of replys! Thanks folks.

    It sounds like some others have nailed the purpose on the head. I want to write singleton managers that don't need to be on a game object in the scene. In this particular case, I have coded an Invoke() functionality that is not affected by timescale, which means I need to loop through a list of delegates, and check their associated fire time against the unity's Time.realTimeSinceStartup. This must be checked on a constant basis, and an OnUpdate callback would be ideal.

    Right now, I am calling the managers Update() function from a monobehavior, but it just feels dirty :)
     
  9. MattRix

    MattRix

    Joined:
    Aug 23, 2011
    Posts:
    121
    gaborkb, Ziflin and user012345 like this.
Thread Status:
Not open for further replies.