I am working on an Editor Extension and been wondering how to allow user to create his own variables like parameters and access their values like Mecanim does.
public struct Parameter
{
public System.Type type;
public string name;
public byte value;
public Parameter(System.Type t, string n, byte v)
{
type = t;
name = n;
value = v;
}
}
Well… you can’t actually create new variables in a class/script without actually modifying the text file for the code. This could be done through the inspector, let me know if this is what you mean.
…
Unless you mean you want to create dynamically added variables to a script.
I do this with dictionary of my class VariantReference:
The dictionary is in VariantCollection:
As you can see used here:
This is NOT like how other variables work though… really its a lookup table of variables by name stored as a VariantReference.
The VariantReference being a special type that allows storing a value of any of the common serializable unity types.
Thing is… it’s not trivial. And of course needs its corresponding editors:
This of course is a lot of work, but is usable at runtime. It even supports operating arithmetic operations at runtime.
This VariableStore is often used by my designer to add arbitrary variables to a scene he is making with out needing to writing any code (he can’t program).
I actually use this in tandem with my ‘Trigger’ system (similar to UnityEvent) to allow calling functions on scripts and pass in parameters evaluated at runtime.