Saturday, 8 February 2014

Unity Reflection

I'm still learning about Unity and how to code for it.  Today I have found out that the OnGUI() method is deliberately called twice every frame.

For the most part this is not noticed but I was putting some home made mouse over methods within the OnGUI() method and getting double key press results.

That led me on to find out how Unity calls the MonoBehaviour methods, like Awake(), Start(), Update() and OnGUI().  Apparently Unity finds them using reflection rather than any form of interface or inheritance.

That behaviour leads to an unexpected coding style compared to vanilla C# coding.

You can define those methods any way you like.  They can be public, private or protected and virtual or not.  Unity will still behave as intended.

It feels odd but just works.

I'm getting the hang of creating components rather than joining everything together in one big mass of a project.

I am surprised how much work is involved in getting a menu system with mouse, keyboard and gamepad input working but I am hopeful I will make up this time when it comes to creating the game levels.