Scriptable Variables is package to implement variables in Unity using Scriptable Objects, taking advantage of Unity 2020.1 ability to serialize generic fields.
This package is considered to be a preview-package. This means:
- Core features may be incomplete.
- Functionality has not yet been fully tested and definitely contains bugs.
- Documentation is incomplete.
- Code may be uncommented and not following standards
Unity 2020.1.0 or later versions.
The package is available on the openupm registry at com.jreason.scriptablevariables
.
More information can be found at the Installation page.
There are two main classes in the package: Variable<>
and Reference<>
Variables are assets created in the Unity Editor which store data. A new Variable Asset can be created from the 'Assets/Create/Variable' menu or from the '+' Dropdown in the ProjectView. A Variable's type can be changed in the 'Type' Dropdown.
Variable Types
Before being able to create a Variable Asset of a certain Type, that type needs to be created as a Class.
The nessary code to create a Variable type can generate from the 'Type' Dropdown by clicking 'Create new', this only needs to be done once per type.
Some Types are pre-setup such as primitives and some more common Unity classes.
References are how variables are accessed in Code.
(e.g. Use Reference<float>
to reference a Float
Variable.)
//Reference to Vector3 Variable
public Reference<Vector3> m_direction;
private void Update()
{
//How to set the value of the variable
m_direction.Value = transform.forward;
//Can implicitly cast when getting the value of the variable
transform.position += m_direction;
//Sometimes you'll need to do this though
transform.position += m_direction.Value * Time.deltaTime;
transform.position += (Vector3)m_direction * Time.deltaTime;
}
More information can be found in the Variable page and Reference page
- The theory for this package is based off Ryan Hipple's Unite Talk.
- The code is based off Wolar-Games Implementation and has been re-written for Unity 2020.1.