Skip to content

MonoBehaviours

Rudolf Kolbe edited this page Jul 23, 2022 · 3 revisions

Background

The MonoBehaviour object type in Unity assets can be seen as a serialized instance of a script class, which can be pretty much everything, going from scene definitions to localisations or game settings.

By definition "[the] MonoBehaviour class is the base class from which every Unity script derives, by default"¹. So nearly all game developer-made script classes are children of the MonoBehaviour class, which allows an easy serialization of all of them via the MonoBehaviour class.

The MonoBehaviour class serves as a springboard, as seen by its signature.

class MonoBehaviour(Behaviour):
    m_GameObject: PPtr[GameObject] = None
    m_Enabled: int = None
    m_Script: PPtr[MonoScript] = None
    m_Name: str = None

m_GameObject points to the GameObject that uses the script. m_Enabled tells the engine if the script is enabled/turned on by default. m_Name is the name of the instance. m_Script points to a MonoScript object.

What is this MonoScript?

class MonoScript(TextAsset):
    m_Name: str = None
    m_ClassName: str = None
    m_Namespace: str = None
    m_AssemblyName: str = None
    m_IsEditorScript: Optional[bool] = None
    m_ExecutionOrder: Optional[int] = None
    m_PropertiesHash: Optional[Union[int, Hash128]] = None

As can be guessed by just reading the property names of the MonoScript class, it simply points to a specific class within an Assembly and is; therefore, used to find the class used by a MonoBehaviour object.

m_AssemblyName is the name of the assembly, e.g. Assembly-CSharp.dll. m_Namespace is the name of the namespace the target class resides in. m_ClassName is the name of the target class.

And that's already everything about the MonoBehaviour object type.

You may now ask, but what about the derived classes I want to parse? Let's talk about that next.

Reading

As explained in Background, all MonoBehaviour objects refer to a MonoScript object, which in turn refers to a class within an assembly. This class is the class type of the object stored with the MonoBehaviour object.

Editing

Clone this wiki locally