- Home /
Access dynamic values from the custom PropertyDrawer of a ScriptableObject
Hi there,
I have a ScriptableObject A that intends to capture dynamically a list of parameter values from the inspector. Here is an illustration of what I whant to do : I have one instance of this ScriptableObject A, that represents a set of three parameters : a float (speed), a boolean (isActive) and a Vector3 (initPosition). Each element of the array is an instance of a Parameter, which consists in a variable Name (string) and a variable Type (another ScriptableObject).
Then, in another class B, I declare a public variable "customParams" of type A, and drag and drop this ScriptableObject to obtain the following in the inspector :
To do that, I made a custom PropertDrawer on my class A, that loops over the parameters, and displays the appropriate field to fill. Here is some code :
public class ADrawer : PropertyDrawer
{
private Parameter[] parameters = null;
private object[] values;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
float height = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
A myParams = property.objectReferenceValue as System.Object as A;
if (myParams != null) {
parameters = myParams.parameters;
height += parameters.Length * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing);
}
return height;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
EditorGUI.BeginProperty(position, label, property);
EditorGUI.PropertyField(position, property, label, true);
if (parameters != null) {
int prevIndent = EditorGUI.indentLevel;
Rect indentedPosition = EditorGUI.IndentedRect(position);
EditorGUI.indentLevel += 1;
values = new object[parameters.Length];
int i = 0;
foreach (Parameter p in parameters) {
//Parameter label
Rect paramLabelPosition = new Rect(indentedPosition.x, position.y + ((i + 1) * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing)), EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight);
EditorGUI.LabelField(paramLabelPosition, p.GetInspectorName());
//Parameter value
Rect valueRect = new Rect(paramLabelPosition.x + paramLabelPosition.width, paramLabelPosition.y, indentedPosition.width - paramLabelPosition.width, EditorGUIUtility.singleLineHeight);
Type type = p.type.FindType();
values[i] = p.type.DrawPropertyField(valueRect);
i++;
}
behaviour.SetParameters(parameters, values);
EditorGUI.indentLevel = prevIndent;
}
EditorGUI.EndProperty();
}
My question is simple : how can I access all the array values from, let's say, a custom PropertyDrawer of B, or even any other class?
Thanks!