- Home /
How can Unity framework recognize the functions defined in child objects?
Hi,
I was trying to develop a C# framework for Unity and got a question which seems to be more relevant to OOP.
In Unity, with objects derived from MonoBehaviour, we have functions like: Update(), Start(), Awake(), OnTriggerEnter(Collider obj)... in which we can define logic implementation to relevant events. We also can leave those functions blank if we don't want to override.
Normally, for injecting logic implementation defined by in child objects, I mark the method as "virtual" or "abstract" and then "override" it later.
class ParentObject { protected void ALargeFunction() { // Do something
// An event fires here. Implement logic defined by child object.
ChildLogicImplemtation();
// Do other things
}
protected virtual void ChildLogicImplemtation()
{
// May put some Default implementations here.
}
}
class ChildObject { protected override void ChildLogicImplemtation() { // base.ChildLogicImplemtation(); // Extension... } }
Is there anyway that I can make my code like what we have in Unity? Something like this?
class ChildObject
{
// - Don't have to use 'override' keyword here
// - Can ignore this function if not necessary.
void ChildLogicImplemtation()
{
// Extension...
}
}
How can Unity framework recognize the functions defined in child objects?
Thanks in advance,
Viet.
Answer by Mike 3 · Oct 25, 2010 at 11:52 AM
They're found via reflection, and added to delegates (or something similar - either way it's storing a pointer to the function somewhere). Then, if they exist, they're called when necessary
It makes things a lot faster than using virtual/override functions, as it doesn't need to go from the native engine to the script code when it's not implemented
Your answer
![](https://koobas.hobune.stream/wayback/20220613080732im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Call CS from JS in one gameobject 1 Answer
Are there any Unity demo projects that have an OOP design? 3 Answers
Duplicating GameObject with variations of the same functionality. 0 Answers
Unity3D memory game 2 Answers