Skip to content

Variables Extending

Richard Kopelow edited this page Aug 5, 2018 · 2 revisions

Variables - Extending

SO Variable

SO Variables wrap data so references to it can easily setup in the editor.

  1. Create a new class file. A new file is necessary for Unity to properly link the code and the SO's asset file.
  2. Inherit from BaseVariable<T> with T being the data type.
  3. Give it a CreateAssetMenu attribute.
[CreateAssetMenu(fileName = "NewByteVariable", menuName = "Scriptable Objects/Variables/Byte")]
public class ByteVariable : BaseVariable<byte> { } 

Value Fields

The Value class acts as an abstraction layer that makes it easy to switch between using SO Variables and hardcoded values.

  1. Inherit from BaseValue<T, Y> with T being the data type and Y being the Variable type.
  2. Give it the Serializable attribute.
[Serializable]
public class ByteValue : BaseValue<byte, ByteVariable> { }

Custom Property Drawer

Custom property drawers are used to clean up the look of Value Fields in the editor while maintaining the flexibility gained by using a Value rather than simply referencing the SO Variable.

  1. Define a class that inherits from ValueDrawer. (This need only be done once, it can be used for all custom variables)
  2. Add a CustomPropertyDrawer attribute to the ValueDrawer class.
[CustomPropertyDrawer(typeof(ByteValue))]

Clone this wiki locally