- Home /
How can I recreate the Array Inspector element for a custom Inspector GUI?
I've been running into troubles doing this and could use some extra thoughts. Here is the code I have so far.
I have an array stored in a script component named Enemy
. I access this script component, and display it in my custom Inspector, but I always receive errors that not all elements are initialized.
Essentially, I am getting hung up on the idea of how this works:
How can I get None (Transform)
to appear in my Inspector instead of an error?
private bool collapsed; private int arraySize; private Enemy e;
public override void OnInspectorGUI() { EditorGUIUtility.LookLikeControls();
e = target as Enemy;
collapsed = EditorGUILayout.Foldout(collapsed, "Array");
if (collapsed)
{
Rect r = EditorGUILayout.BeginVertical();
arraySize = EditorGUILayout.IntField
if (e.array.Length > 0)
{
Transform[] temp = new Transform[arraySize];
e.array.CopyTo(temp, 0);
e.array = new Transform[arraySize];
temp.CopyTo(e.array, 0);
}
else
e.array = new Transform[arraySize];
for (int i = 0; i < arraySize; i++)
{
Transform t = e.array[i];
t = EditorGUILayout.ObjectField(t.name, t, typeof(Transform)) as Transform;
}
EditorGUILayout.EndVertical();
}
}
your kinda vague, if your asking how to take a built in array with some array indexes empty, and resize it to not have those empty slots, i went through each index in your array(array A, looks like your using a built in array, idk i use JS), asked if its not empty (if !=null), then put it in another array (array B, i used a JS array), then make array A = array B(if JS array, A=B.ToBuiltin(var type(for you, Transform)).
please clarify your question if i have not already answered it.
Answer by Darclaw · Jul 15, 2013 at 03:31 PM
If anyone interested, here's another example for array in custom editor: Base class:
public class RoomItem : MonoBehaviour {
public Transform[] targetPoints;
// other data
}
Custom editor:
[CustomEditor(typeof(RoomItem))]
public class RoomItemEditor : Editor {
public override void OnInspectorGUI() {
serializedObject.Update();
var controller = target as RoomItem;
EditorGUIUtility.LookLikeInspector();
SerializedProperty tps = serializedObject.FindProperty ("targetPoints");
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(tps, true);
if(EditorGUI.EndChangeCheck())
serializedObject.ApplyModifiedProperties();
EditorGUIUtility.LookLikeControls();
// ...
}
}
Thank you! This was perfect and I thought much cleaner than the other solutions.
Note that if you want an array of some class (not Transform), you'll need to mark it as Serializable.
public class RoomItem : $$anonymous$$onoBehaviour {
[System.Serializable]
public class $$anonymous$$yClass {
public Transform transform;
public AnimationClip animClip;
}
public $$anonymous$$yClass[] targetPoints;
}
EditorGUIUtility.LookLikeInspector(); EditorGUIUtility.LookLikeControls();
are now obsolete. What's the alternative?
Answer by booferei · Jan 10, 2018 at 02:51 PM
If you want to mimic the default Array display, try this:
SerializedProperty arrayProp = serializedObject.FindProperty("array");
arrayProp.isExpanded = EditorGUILayout.Foldout(arrayProp.isExpanded, "Array");
if (arrayProp.isExpanded) {
arrayProp.arraySize = EditorGUILayout.IntField("Size", arrayProp.arraySize);
for (int i = 0; i < arrayProp.arraySize; ++i) {
SerializedProperty transformProp = arrayProp.GetArrayElementAtIndex(i);
EditorGUILayout.PropertyField(transformProp, new GUIContent("Element " + i));
}
}
Answer by steinbitglis · Feb 24, 2012 at 01:53 PM
This is roughly what DrawDefaultInspector()
does:
override def OnInspectorGUI():
serializedObject.Update()
EditorGUIUtility.LookLikeInspector()
myIterator = serializedObject.FindProperty("myArrayField")
while true:
myRect = GUILayoutUtility.GetRect(0f, 16f);
showChildren = EditorGUI.PropertyField(myRect, myIterator)
break unless myIterator.NextVisible(showChildren)
serializedObject.ApplyModifiedProperties()
It works like it should with bold font on prefab overrides etc. It also handles undo operations, 'delete' operations and 'shift+delete' operations as expected. It works with all the kinds of arrays that Unity originally supports. It also looks like the original.
Remember to call EditorGUIUtility.LookLikeControls()
after drawing the array.
Thanks man! Almost the same as the default inspector! You saved my day. Now I need to figure out what each method does...
Answer by Steven-Walker · Nov 16, 2010 at 02:22 AM
For anyone else who is searching for the solution to this, here is working code in Javascript. It doesn't look the same as the built-in array controls, but it works fine.
// MyObject.js
public var ElementsExpand : boolean = true;
public var ElementsSize : int = 1;
public var Elements : Transform[] = new Transform[ElementsSize];
// MyObjectEditor.js - OnInspectorGUI()
target.ElementsExpand = EditorGUILayout.Foldout(target.ElementsExpand, "Transforms");
if(target.ElementsExpand) {
var x : int = 0;
target.ElementsSize = EditorGUILayout.IntField("Size", target.ElementsSize);
if(target.Elements.length != target.ElementsSize) {
var newArray : Transform[] = new Transform[target.ElementsSize];
for(x = 0; x < target.ElementsSize; x++) {
if(target.Elements.length > x) {
newArray[x] = target.Elements[x];
}
}
target.Elements = newArray;
}
for(x = 0; x < target.Elements.length; x++) {
target.Elements[x] = EditorGUILayout.ObjectField("Element "+x, target.Elements[x], typeof(Transform));
}
}
Answer by zee_ola05 · Dec 14, 2017 at 11:16 PM
SerializedProperty property = serializedObject.FindProperty("myArray");
EditorGUILayout.PropertyField(property, new GUIContent("My Array"), true);
Since, EditorGUIUtility.LookLikeInspector() and EditorGUIUtility.LookLikeControls() are now obsolete, here is an alternative.
Best answer! This is super clean and effective. It also allows us to customize the string value as well, thank you! This should be the best answer I$$anonymous$$HO.
Your answer
Follow this Question
Related Questions
How can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers
Editor scripting: Object reference not set to an instance of an object 0 Answers
OnInspectorGUI changes reset when played in editor or building 2 Answers
Script to create instance from in inspector 2 Answers
Custom Inspector & Arrays 0 Answers