4
\$\begingroup\$

How does Unity3D implement their system where if a class is derived from MonoBehaviour and has certain methods like Awake() or Update(), they are called accordingly? I want to do a similar system for classes inheriting from the class 'Ability' to store the classes in an array and call the callback method Execute() when they are being executed.

\$\endgroup\$
2
  • \$\begingroup\$ Note on vocabulary: what you're talking about isn't really a "callback" it's simply an inherited method. Callbacks are functions passed to unrelated objects, not inheriting objects. \$\endgroup\$
    – jhocking
    Apr 2, 2015 at 15:48
  • \$\begingroup\$ Oh, thanks! I'll make sure to use the correct word next time. \$\endgroup\$
    – JPtheK9
    Apr 2, 2015 at 20:57

3 Answers 3

4
\$\begingroup\$

We just have to declare Awake, Update, and any other function created by us, to be public and virtual on the parent. Override the function on the children.

public class AgentBehaviour : MonoBehaviour
{

    public virtual void Awake()
    {
        // your code here
    }

    public virtual Vector3 GetVector3 ()
    {
        return new Vector3();
    }
}




public class Seek : AgentBehaviour
{

    public override void Awake()
    {
        // in case you need to call the parent's Awake
        base.Awake();
        // and then add any other code
        // here
    }

    public override Vector3 GetVector3()
    {
        return Vector3.zero;
    }
}
\$\endgroup\$
0
3
\$\begingroup\$

As pctroll said, the right solution for you is to use virtual functions.

To answer the question "how does Unity do that" without virtuals (i.e.: you don't have to override Awake or Update in MonoBehavior derived classes) they use Reflection to call theese functions by their names (string).

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the insight! I'm going to use virtual/override because that just seems easier and more efficient for my uses but it's good to know more methods, especially those of major engines. \$\endgroup\$
    – JPtheK9
    Mar 27, 2015 at 0:59
1
\$\begingroup\$

Unity doesn't virtualize its methods

Firstly, I want to say that I don't think pctroll is wrong in the context of the question (overriding-wise) but if you look in the update API, then Unity would generate a code closer to what it's bellow:

public class AgentBehaviour : MonoBehaviour
{
    public void Awake()//note the lack of the virtual keyword
    {
        // your code here
    }

    public virtual Vector3 GetVector3 ()
    {
       return new Vector3();
   }
}

How Unity "virtualize" its methods

But I still feel that the question wasn't aswered properly

How does Unity3D implement their system where if a class is derived from MonoBehaviour and has certain methods like Awake() or Update(), they are called accordingly?

I mean, have you ever notice that MonoBehaviour is indeed calling a lot of methods, like Awake, without overriding them? This is done due to a feature of C#, the extern modifier which allows external programs to implement behaviours and somehow the DLL your game builds is passed to Unity.

I'm Sorry But...

Ok, I know I have seen it sowewhere in Unity documentation or code but I cannot prove it. So can you do me a solid? So try to enter in Unity's metadata (the DLL, kind of accessible through F12 if you code in VS) and see how it is implemented, then print and post it here.

If no one does it before I get on a computer with Unity, I'll try to get that image as a prove of what I've just written.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .