- Home /
Array of classes on SerializedProperty
So, I have a custom class, with a custom inspector, and I need to alter a array with the custom inspector. The problem is, I can't use the default array inspector, because I need to change 2 values of each class in the array. I have the class: #InteractiveObject.js
#pragma strict
class InteractiveObject extends MonoBehaviour {
public static var name : String = "Unnamed Object";
public static var version : int = 0x001;
public var propagate : InteractionPacket[];
final function Start() {
//Reserved
Init();
}
public function Interact(type : InteractionType, sender : InteractiveObject) : InteractionResults {
return InteractionResults.NotImplemented;
}
private function Init() {}
static public function Interact(sender : InteractiveObject, p :InteractionPacket) : InteractionResults {
return p.target.Interact(p.type, sender);
}
}
public enum InteractionType {
Activate,
Toggle,
Deactivate,
TurnOn,
TurnOff
}
public enum InteractionResults {
Sucess,
Failed,
InvalidInput,
NotImplemented,
Exception
}
public class InteractionPacket {
public var type : InteractionType;
public var target : InteractiveObject;
}
And its editor class: #Editor/InteractiveObjectEditor.js
#pragma strict
@CustomEditor(InteractiveObject)
@CanEditMultipleObjects
class InteractiveObjectEditor extends Editor {
var propagate : SerializedProperty;
//EDITOR SCRIPT VARIABLES
private var propagateControls : boolean[] = new boolean[4];
//END EDITOR SCRIPT VARIABLES
function OnEnable() {
propagate = serializedObject.FindProperty("propagate");
}
function OnInspectorGUI() {
serializedObject.Update();
EditorGUILayout.BeginHorizontal();
propagateControls[0] = EditorGUILayout.Foldout(propagateControls[0], "Propagate");
propagateControls[1] = GUILayout.Button("+", EditorStyles.miniButtonLeft, GUILayout.MaxWidth(30));
propagateControls[2] = GUILayout.Button("-", EditorStyles.miniButtonMid, GUILayout.MaxWidth(30));
propagateControls[3] = GUILayout.Button("Clear", EditorStyles.miniButtonRight);
EditorGUILayout.EndHorizontal();
if (propagateControls[0]) {
}
}
}
I want to create a editor that displays each entry in the propagate
variable like this:
A selector for a InteractiveObject class, a enum dropdown for InteractionType and a button to remove the entry. It would be simple, if I could access the variable contents. I can't seem to find a way to modify a array as a SerializedProperty...
You can use these to edit an array serialized property:
public void InsertArrayElementAtIndex( int index );
public bool $$anonymous$$oveArrayElement( int srcIndex, int dstIndex );
public SerializedProperty GetArrayElementAtIndex( int index );
So you would use GetArrayElementAtIndex to get the SerializedProperty of one of the InteractionPacket objects in the array (InteractionPacket needs to be System.Serializable).
Answer by AndyP · Jun 23, 2012 at 09:03 AM
I'm having the same problem. I have a class TrackData which contains a list of TrackDataPoint objects. Both classes are marked as serializeable and the TrackDataPoint only contains standard serializeable types (Vector3 and float). Whilst I can loop through the list of TrackDataPoints in my editor class, I can't figure out how to extract the properties of each point. Either they are not serialized because TrackDataPoint is not a standard class, or there is some syntactic acrobatics I need to perform in order to extract the data from each point. I tried using an IEnumerator via GetEnumerator, but its value was null.
Would be nice to have some more in depth Editor tutorials on the Unity site actually. In addition to documentation and examples lacking for c# users, it would be great to see a few more complicated examples.
I want to know this also, anyone can provide a good answer ?
Answer by SmallCar · Nov 08, 2012 at 03:39 AM
http://catlikecoding.com/unity/tutorials/star/ this is a tutorial of the Unity Inspector.