Skip to content

Instantly share code, notes, and snippets.

@rutcreate
Last active October 28, 2022 14:30
Show Gist options
  • Save rutcreate/d550aa1ae4052e0a0b37 to your computer and use it in GitHub Desktop.
Save rutcreate/d550aa1ae4052e0a0b37 to your computer and use it in GitHub Desktop.
Unity3D: How to iterate properties through inspector
using UnityEngine;
using UnityEditor;
using System.Collections;
namespace Opendream {
[CustomEditor(typeof(MovingPlatform))]
public class InspectorEditor : Editor {
public override void OnInspectorGUI() {
serializedObject.Update();
// SerializedProperty movingPlatform = serializedObject.FindProperty("movingPlatform");
// Show movePoints property if movingPlatform is checked.
// if (movingPlatform.boolValue) {
// SerializedProperty movePoints = serializedObject.FindProperty("movePoints");
// Atleast 1 element.
// if (movePoints.arraySize == 0) {
// movePoints.InsertArrayElementAtIndex(0);
// movePoints.GetArrayElementAtIndex(0).vector3Value = handleTransform.position;
// }
// }
SerializedProperty prop = serializedObject.GetIterator();
if (prop.NextVisible(true)) {
do {
// Draw movePoints property manually.
if (prop.name == "movePoints") {
if (movingPlatform.boolValue) {
EditorGUI.indentLevel++;
DrawMovePointsElement(prop);
EditorGUI.indentLevel--;
}
}
// Draw default property field.
else {
EditorGUILayout.PropertyField(serializedObject.FindProperty(prop.name), true);
}
}
while (prop.NextVisible(false));
}
serializedObject.ApplyModifiedProperties();
}
private void DrawMovePointsElement(SerializedProperty property) {
// Draw your stuff.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment