- Home /
CustomEditor: Inspector icon rapidly switching
I made a simple CustomEditor-script just to hide and show inspector parameters based on the current state of the MonoBehaviour.
The Problem is the moment I add that script to a GameObject the inspector icon starts switching between the attached sprite and the default Prefab-icon.
I don't set the icon in the CustomEditor-script, so it baffles me why this happens only when the script with the CustomEditor is attached.
Google didn't help so I'm hoping to find help here, of which any is greatly appreciated.
Edit: As requested, here is the code (if it is too long I will gladly shorten it)
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(PlayerMovement)), CanEditMultipleObjects]
public class PlayerMovementEditor : Editor
{
public SerializedProperty
mode_p,
physics_p,
accelerationRate_p,
breakRate_p,
maxSpeed_p,
accelerationForce_p,
turnSpeed_p;
private void OnEnable() {
// Setup SerializedProperties
mode_p = serializedObject.FindProperty("mode");
physics_p = serializedObject.FindProperty("physics");
accelerationRate_p = serializedObject.FindProperty("accelerationRate");
breakRate_p = serializedObject.FindProperty("breakRate");
maxSpeed_p = serializedObject.FindProperty("maxSpeed");
accelerationForce_p = serializedObject.FindProperty("accelerationForce");
turnSpeed_p = serializedObject.FindProperty("turnSpeed");
}
public override void OnInspectorGUI() {
serializedObject.Update();
// enums can't be changed when moving during runtime
GUI.enabled = !Application.isPlaying || !(target as PlayerMovement).IsMoving;
EditorGUILayout.PropertyField(mode_p);
EditorGUILayout.PropertyField(physics_p);
GUI.enabled = true;
PlayerMovement.Mode mode = (PlayerMovement.Mode)mode_p.enumValueIndex;
PlayerMovement.PhysicsLevel physics = (PlayerMovement.PhysicsLevel)physics_p.enumValueIndex;
switch (physics)
{
case PlayerMovement.PhysicsLevel.None:
EditorGUILayout.PropertyField(maxSpeed_p, new GUIContent("Max Speed"));
break;
case PlayerMovement.PhysicsLevel.RateBased:
EditorGUILayout.PropertyField(maxSpeed_p, new GUIContent("Max Speed"));
EditorGUILayout.Slider(accelerationRate_p, 0f, 1f, new GUIContent("Acceleration Rate"));
EditorGUILayout.Slider(breakRate_p, 0f, 1f, new GUIContent("Break Rate"));
break;
case PlayerMovement.PhysicsLevel.ForceBased:
EditorGUILayout.PropertyField(accelerationForce_p, new GUIContent("Acceleration Force"));
break;
}
if (mode == PlayerMovement.Mode.Ship)
EditorGUILayout.PropertyField(turnSpeed_p, new GUIContent("Turn Speed"));
serializedObject.ApplyModifiedProperties();
}
}
You might do something which just breaks the GUI flow of the inspector. We can not know that since we have absolutely no idea what you do in your custom editor.
I'm sorry for this oversight.. I added the code, hoping it isn't too long
Too long? No, it's actually quite concise. I don't see any major issues, though there are some $$anonymous$$or ones. First of all you used "enumValueIndex" wrong. It does not return the actual enum value so casting it to your enum type might fail. The "enumValueIndex" represents the index in the enumNames / enumDisplayNames array. To get the actual enum value you should use the intValue. If the enum names have the same order and the values start at 0 without any gaps the index would be the same so it might work in your case. But in general your usage is wrong.
Apart from that I don't see any other issue, I have never seen this problem. Though I haven't done much lately since Unity has nested prefab support / prefab variant support. $$anonymous$$aybe it has something to do with them.