OnApplicationQuit C# technical details?

Hi,

I’m just curious how OnApplicationQuit gets called from a MonoBehavior subclass in C#. I see that it’s getting called, but according to the metadata, OnApplicationQuit is not a member of MonoBehavior or any of its parents.

I considered that maybe it was getting called through Reflection, but in viewing the Environment.StackTrace inside my OnApplicationQuit implementation, the bottom of the call stack is my class which implements it (i.e. there are no callers beyond that).

What is going on under the hood?

1 Answer

1

Unity does a lot post-processing after your scripts are compiled. Unity searches via reflection if you’ve implemented an Update or Start method and how it’s defined. As you might know Start can be a coroutine or a normal function. If a script doesn’t implement a certain method the class is flagged internally so Unity can call those function very optimised.

I can’t tell what’s really going on behind the scenes since most of the engine is written in C++ and only the scripting interface is using Mono.