ID
Nothing fancier than a string regarding the data it carries, but it has a more specific semantic meaning: it represents a unique identifier specific for a given instance of an object or piece of data.
It gives added functionalities to a field of this type in the Inspector and can be decorated with attributes to modify those functionalities. It's also used in some Types of collections used as dictionaries with an Id as a key (see Identifiables, IdentifiablesCollection.)
Creating an ID
To create a new ID instance there are two possible ways. If the ID has a specific value that has to be assigned, it can be created with
Id id = Id.OfValue("MyID");
If the new ID's actual value is irrelevant and it's just required to be unique, it can be created with
Id id = Id.Generate();
string and Id are interchangeable types and can be implicitly cast one into the other and vice-versa. Carefully consider in what cases this property can be used without causing confusion before using it.
Id id = "MyID"; // implicit cast string → Id
string idString = id; // implicit cast Id → string
ID in the Inspector
In the inspector, by default an ID field looks like this:
Semantically speaking, an empty ID field is not necessarily an invalid ID, but it's usually just an oversight, this is why an empty ID is always indicated by a red label and an alert icon on its side. The icon disappears and the label turns to the default aspect as soon as the field is given a non-empty value, unless the field isn't decorated with an Optional attribute.
The 'refresh' button can be used to quickly and automatically re-generate a valid ID using a GUID string.
Customization Attributes
The ID inspector can be further customized by decorating the field with attributes.
Optional
An ID decorated with the Optional attribute won't show the alert icon and won't color the label red if it doesn't have a value.
[SerializeField, Optional]
private Id _idField;
Id Inspector Type
An ID field decorated with the IdInspectorType attribute changes how it looks in the inspector and what operations are possible to change its value.
[SerializeField, IdInspectorType(IdType)]
private Id _idField;
It can also be used to decorate class definitions. In that case, it changes the appearence of all fields of type Id defined in the decorated type (see Identifiables.)
The IdInspectorType attribute allows to specify whether or not the ID is editable from the inspector and if it can be regenerated by pressing the button.
The possible parameters are:
-
IdType.ManualRegen. The default, equivalent to not specifying this attribute. The ID field can be edited and thebutton is enabled.
-
IdType.ManualOnly. The ID field can be edited and thebutton is disabled.
IdType.AutoRegen. The ID field can't be edited and thebutton is enabled. The initial value is automatically generated and since it can't be edited manually, it cannot be empty.
IdType.AutoOnly. The ID field can't be edited and thebutton is disabled. The initial value is automatically generated and since it can't be edited manually or re-generated, it cannot be empty and it cannot be changed.
Optional and IdInspectorType can be combined to decorate a single Id.
ID Reference
To reduce errors in trying to reference objects through their IDs by hand (they can be reported with typos or be forgotten), it's possible to serialize a field of type IdReference<T> instead of plain Id in the Inspector. It looks similar to a classic UnityEngine.Object reference field, but it only serializes its ID.
The generic type T must be a Type implementing IIdentifiable (see the identifiables page for more information.)
T can by any IIdentifiable, but only Monobehaviors/ScriptableObjects implementing the interface will show up correctly (IdentifiableObject, IdentifiableBehaviour.) Non-UnityEngine.Object Identifiables will appear as a default id field.
The Inspector field will explicit (between parenthesis in fluorescent blue) the ID of the referenced object to show what has been serialized.
If the colored ID is missing after a value has been assigned in the Inspector, it means the referenced object has an Empty ID.
In Editor, a change of an Identifiable ID won't break the reference to it in an ID Reference field, but it will give a Warning at runtime because it has to be refreshed. It can be done semi-automatically by opening the REFERENCING asset/component in the Inspector.
ID Reference Filter Attribute
An IdReference field can be decorated with the [IdReferenceFilter] attribute, specifying as parameter the name of a local method that defines the filtering logic. The method is expected to accept an generic object and to return a boolean value.
[SerializeField, IdReferenceFilter(nameof(HasFunnyName))]
private IdReference<ScriptableObject> _scriptableReference;
private bool HasFunnyName(object newValue) => ((ScriptableObject)newValue).name.Contains("blipbloop");
Whenever a new object is being assigned to the field, the given method is run and if the result is false, the new value is rejected.