Properties¶
Game Creator properties are a special type of class that allows to dynamically specify the source of a field value using a dropdown menu. The menu's options are dynamic and can be added without the need of overwriting Game Creator core code, allowing to write maintainable and decoupled code.
Polymorphic Serialization
Properties take advantage of Unity's polymorphic serialzation, which means that the dropdown menu options are decoupled from the core code. Anyone can plug in their own menu options without overwriting any scripts.
There are different types of Properties, each with its own set of options. All of them have in common that, when retrieving them, an instance of Args
parameter is passed, which contains two fields:
- Target: A reference to the
Game Object
responsible for calling the property - Self: A reference to the
Game Object
containing the property reference.
Args Parameter
There are some cases where the Target
and Self
fields will reference the same game object.
Property Get types allow to retrieve a value and Property Set types allow to set a value. Game Creator comes with a collection of both types, but each module increases the amount available. You can even create your own property types to extend the existing ones.
Property Get Types¶
There are a few default property types available:
PropertyGetBool
: A boolean value typePropertyGetColor
: A representation of a ColorPropertyGetDecimal
: A decimal valuePropertyGetDirection
: A Vector3 representing a directionPropertyGetGameObject
: References a game objectPropertyGetInstantiate
: Allows to reference an instancePropertyGetInteger
: An integer valuePropertyGetLocation
: A position and/or rotationPropertyGetOffset
: A Vector3 that offsets from a positionPropertyGetPosition
: A Vector3 representing a point in spacePropertyGetRotation
: A Quaternion representing a rotationPropertyGetScale
: A Vector3 representing a scalar valuePropertyGetScene
: Allows to select scene objectsPropertyGetSprite
: Returns Sprite assetsPropertyGetString
: Returns textsPropertyGetTexture
: To retrieve Texture assets
Property Set Types¶
PropertySetBool
: Sets a boolean valuePropertySetColor
: Sets a Color type valuePropertySetGameObject
: Sets a game object referencePropertySetNumber
: Sets a numeric valuePropertySetSprite
: Sets a Sprite referencePropertySetString
: Sets a text-based valuePropertySetTexture
: Sets a Texture asset referencePropertySetVector3
: Sets a Vector3 type value
Using Properties¶
UI Toolkit
Using properties requires the Editor scripts to be written using Unity's UI Toolkit. IMGUI is not supported.
To use a property it's very simple. You just need to declare them as you would with a primitive type, but instead of getting the value directly, call the Get(args)
method to retrieve its value.
For example, let's say that in a component, you want to get a string
value. Instead of declaring a value like this:
public string myValue = "This is my string";
You could use a property so the source of that string value isn't hard-coded, but set from the Inspector. Like this:
public PropertyGetString myValue = new PropertyGetString();
This will display a dropdown menu on the Inspector with the current option selected. By default it's a constant string, but the value can be chosen to come from the name of a game object, a local or global variable, etc.
To get the value you simply call the Get(args)
method:
string value = this.myValue.Get(args);
Args
The Args
(arguments) class is a two-field struct that contains the game object considered as the source of the call as well as the targeted game object. This class is necessary in order to use properties that reference the "Self" or "Target" values. If you are not sure what the self and target objects are, simply pass in the current MonoBehaviour's game object:
Args args = new Args(this.gameObject);