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>, whereTshould be the type you wish to wrap. - Ensure the new type is marked with the
CreateAssetMenuattribute, so you can create instances in the Editor. - Create any instances of the new variable in your
Assetsfolder. - Configure the
isReadOnlyandinitialValueas 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
privatebacking fields andpublicaccessor properties. - Use the
TrySetProperty()method in the propertysetblock. - Optionally raise events via
RaisePropertyChanged()method for any computed properties. - Ensure the new type is marked with the
CreateAssetMenuattribute, so you can create instances in the Editor. - Create any instances of the new variable in your
Assetsfolder. - Now you can use them throughout your application.
References
- Create a new class that derives from
RuntimeReference<T, TVar>whereTshould be the underlying type andTVaris the variable type. - Ensure the new type is marked with the
Serializableattribute, 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
CustomPropertyDrawerattribute 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>, whereTshould be the underlying type andTVaris 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>, whereTshould be the underlying type andTVaris the variable type. - Ensure the new type is marked with the
CustomEditorattribute 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.