Unity3d package allowing the distribution of data between different GameObjects through ScriptableObjects.
This project is inspired on the presentation by Ryan Hipple. I added my own flair to it by adding simple property drawers and extendability for custom and complex variables.
Feedback is welcome.
- Open "Package Manager"
- Choose "Add package from git URL..."
- Use the HTTPS URL of this repository:
https://github.com/yanicksenn/unity-variables.git
- Click "Add"
This package provides you references and variables. References are used in the scripts to allow access to a constant or variable.
These are the build-in references and the corresponding variables:
Reference | Variable |
---|---|
FloatReference |
FloatVariable |
" |
ClampedFloatVariable |
IntReference |
IntVariable |
" |
ClampedIntVariable |
BoolReference |
BoolVariable |
StringReference |
StringVariable |
Vector2Reference |
Vector2Variable |
Vector3Reference |
Vector3Variable |
QuaternionReference |
QuaternionVariable |
Nobody hinders you from directly using variables inside of your scripts. Please keep in mind then the interchangeability of constant and variable is not possible.
You can use them inside of your scripts. You don't need to call the constructor but if you do you can provide a default value for your constant.
public class MyBehaviour : MonoBehaviour
{
public FloatReference myFloat;
// public FloatReference myFloat = new FloatReference(7.2f);
}
The button is drawn in front you your reference.
If you click on it, a popup with be displayed providing you with the options of using the variable or a the constant.
After choosing the variable the property will immediately be updated to allow referencing a variable. Vice versa if you choose to use the constant.
You can read and write to your references through the myFloat.Value
property.
Adding the Start
method to the behaviour ...
void Start() {
myFloat.Value = 99.876f;
Debug.Log(myFloat.Value);
}
... will result in the following console output.
Variables can be created through the asset menu > Create > Variables > ... .
After assigning the created variable to the behaviour you should see a preview with value of the variable.
It is possible to create you own reference and variable by extending from AbstractReference
and AbstractVariable
.
Mostly you will just have to create boilerplate code.
Start by defining a data class. It is important that this class
or struct
is annotated with Serializable
.
// Arbitrary data object
[Serializable]
public struct Character
{
public string name;
public int age;
public float height;
public BoolReference alive;
public Pet pet;
}
// Arbitrary example of a nested object
[Serializable]
public class Pet
{
public string name;
public int age;
public BoolReference alive;
}
Create an event with the type of the data class as payload. This is just boilerplate code.
[Serializable]
public class CharacterEvent : UnityEvent<Character> { }
Create a variable class by extending from AbstractVariable
(inherits from ScriptableObject
). This is just boilerplate code.
[CreateAssetMenu(
menuName = Variables.RootMenu + "/Create Character variable",
fileName = "Character")]
public class CharacterVariable : AbstractVariable<Character, CharacterEvent> { }
Create a reference class by extending from AbstractReference
. This is just boilerplate code. It is possible to provide a default constant value inside of the constructor.
[Serializable]
public class CharacterReference : AbstractReference<Character, CharacterVariable, CharacterEvent>
{
public CharacterReference(Character defaultConstantValue) : base(defaultConstantValue) { }
}
Create a drawer class by extending from AbstractSimpleReferenceDrawer
. This is just boilerplate code.
[CustomPropertyDrawer(typeof(CharacterReference))]
public class CharacterReferenceDrawer : AbstractSimpleReferenceDrawer { }
Use the new CharacterReference
inside of your MonoBehvaiour
or ScriptableObject
.
public class CharacterBehaviour : MonoBehaviour {
public CharacterReference character;
}
Using the CharacterBehaviour
on a GameObject
would then show the following editor in the inspector, allowing you to edit the underlying constant.
Or if you want to reference a variable.