Custom Types
While this library provides many of the common Unity types you will use, you may want to extend this library with your own custom types specific to your application. Fortunately, this is rather easy and typically only involves creating a derived class from one of the base classes. You may also add any additional helper properties and methods as you need.
Variables
- Create a new class that derives from
RuntimeVariable<T>
, whereT
should be the type you wish to wrap. - Ensure the new type is marked with the
CreateAssetMenu
attribute, so you can create instances in the Editor. - Create any instances of the new variable in your
Assets
folder. - Configure the
isReadOnly
andinitialValue
as needed. - Now you can use them throughout your application.
NOTE: While most people create ScriptableObject
instances in the Editor, you can create them at runtime as well!
Observables
- Create a new class that derives from
ObservableObject
. - Add
private
backing fields andpublic
accessor properties. - Use the
TrySetProperty()
method in the propertyset
block. - Optionally raise events via
RaisePropertyChanged()
method for any computed properties. - Ensure the new type is marked with the
CreateAssetMenu
attribute, so you can create instances in the Editor. - Create any instances of the new variable in your
Assets
folder. - Now you can use them throughout your application.
References
- Create a new class that derives from
RuntimeReference<T, TVar>
whereT
should be the underlying type andTVar
is the variable type. - Ensure the new type is marked with the
Serializable
attribute, so it appears in the Inspector. - Now you can use the reference in your components.
Editor
For the custom Inspector GUI to work you will need to create a new custom property drawer class.
You can reuse the RuntimeReferencePropertyDrawer
class.
- Create a new class that derives from
RuntimeReferenceProperyDrawer
. - Ensure the new class is marked with the
CustomPropertyDrawer
attribute for your custom reference type.
NOTE: You can mark this custom property drawer as the CustomPropertyDrawer
for multiple reference types.
No need to create multiple variants unless you explicitly need them to behave differently.
Watchers
- Create a new class that derives from
RuntimeVariableWatcher<T, TVar>
, whereT
should be the underlying type andTVar
is the variable type. - You can now use the watcher component in your scenes.
Editor
For the custom Inspector GUI to work you will need to create a new custom editor class.
You can reuse the RuntimeVariableWatcherEditor<T, TVar>
class.
- Create a new class that derives from
RuntimeVariableWatcherEditor<T, TVar>
, whereT
should be the underlying type andTVar
is the variable type. - Ensure the new type is marked with the
CustomEditor
attribute for your custom watcher type.
NOTE: Unlike the property drawer, a CustomEditor
can only reference a single type.
You will need to make a custom editor for each custom watcher you make.