Last active
April 18, 2021 06:26
-
-
Save STARasGAMES/193b7a5a3b98c7731ac7c36b63d6ef8e to your computer and use it in GitHub Desktop.
Main classes to implement ScriptableObjects variables workflow. They are not functionaly working, but will give you an overview what should be implemented. Note: there are only generic classes, and you eventually will need to create variants for each variable such as float, int, bool, Transform, Color etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class ReadonlyVariable<TValue> : ScriptableObject | |
{ | |
public abstract TValue Value { get; } | |
} | |
public abstract class Variable<TValue> : ReadonlyVariable<TValue> | |
{ | |
protected TValue _runtimeValue; | |
public override TValue Value | |
{ | |
get => _runtimeValue; | |
set => _runtimeValue; | |
} | |
void Awake() | |
{ | |
_runtimeValue = Value; | |
} | |
protected abstract TValue InitialValue{ get; } | |
} | |
public abstract class ReadonlyVariableReference<TValue, TVariable> : ReadonlyVariable<TValue> where TVariable : ReadonlyVariable<TValue> | |
{ | |
[SerializeField] protected TVariable _variable; | |
public override TValue Value { get => _variable.Value; } | |
public virtual TVariable Variable { get => _variable; } | |
} | |
public abstract class VariableReference<TValue, TVariable> : ReadonlyVariableReference<TValue, TVariable> where TVariable : Variable<TValue> | |
{ | |
public override TValue Value { get => base.Value; set => _variable.Value = value; } | |
public override TVariable Variable { get => base.Variable; set => _variable = value; } | |
} | |
public class SerializedVariable<TVariable, TValue> where TVariable : Variable<TValue> | |
{ | |
[SerializeField] private TValue _plainValue; | |
[SerializeField] private TVariable _variable; | |
public TValue Value | |
{ | |
get | |
{ | |
// if variable reference is set in the inspector, then use value from it | |
if (_variable != null) | |
return _variable.Value; | |
// otherwise, use a plain value, that set from inspector | |
return _plainValue; | |
} | |
set | |
{ | |
// the same logic for setter | |
if (_variable != null) | |
_variable.Value = value; | |
_plainValue = value; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment