- Home /
Custom Inspector Enum
Right now im having problems with my Editor code. Essentially, im trying to make a custom editor which displays multiple different variables in the inspector based off of which enum i have selected. My problem comes in when i try to have 2 different enums. for some reason, the custom editor takes the wrong index value.
So for example, if i select Normal in the top enum, all the normal elements are displayed for both enums. If i select Charge in the top enum (number 2 in list) it also displays every element for the second enums number 2 in list.
To simplify, only the upper one works, and is also determining to display the elements for the lower enum too.
Any Help would be very appreciated! Cheers
//Gaffa
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Bullet)), CanEditMultipleObjects]
public class BulletEditor : Editor {
public SerializedProperty
bulletType_Prop, //enum 1
bulletWaitTime_Prop, //elements of enum1
bulletDamageChargeMultiplier_Prop, //elements of enum1
bulletInpact_Prop, //enum 2
explosionRange_Prop, //elements of enum2
explosionDamage_Prop, //elements of enum2
bounceSpeedFallof_Prop; //elements of enum2
void OnEnable()
{
bulletType_Prop = serializedObject.FindProperty ("bulletType");
bulletWaitTime_Prop = serializedObject.FindProperty ("bulletWaitTime");
bulletDamageChargeMultiplier_Prop = serializedObject.FindProperty ("bulletDamageChargeMultiplier");
bulletInpact_Prop = serializedObject.FindProperty ("bulletInpact");
explosionRange_Prop = serializedObject.FindProperty ("explosionRange");
explosionDamage_Prop = serializedObject.FindProperty ("explosionDamage");
bounceSpeedFallof_Prop = serializedObject.FindProperty ("bounceSpeedFallof");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField (bulletType_Prop);
Bullet.BulletTypes bTypes = (Bullet.BulletTypes)bulletType_Prop.enumValueIndex;
switch(bTypes)
{
case Bullet.BulletTypes.normal:
break;
case Bullet.BulletTypes.charge:
//om typen är charge visa bullet charge multiplier
EditorGUILayout.Slider(bulletDamageChargeMultiplier_Prop, 0.1f, 10f, new GUIContent("bulletDamageChargeMultiplier"));
break;
case Bullet.BulletTypes.wait:
//om typen är wait, visa bullet wait time
EditorGUILayout.Slider(bulletWaitTime_Prop, 0f, 10f, new GUIContent("bulletWaitTime"));
break;
}
serializedObject.ApplyModifiedProperties ();
EditorGUILayout.PropertyField (bulletInpact_Prop);
Bullet.BulletInpact bInpact = (Bullet.BulletInpact)bulletType_Prop.enumValueIndex;
switch(bInpact)
{
case Bullet.BulletInpact.normal:
break;
case Bullet.BulletInpact.explosion:
EditorGUILayout.Slider(explosionRange_Prop, 0f, 100f, new GUIContent("explosionRange"));
EditorGUILayout.Slider(explosionDamage_Prop, 0f, 100f, new GUIContent("explosionDamage"));
break;
case Bullet.BulletInpact.bounce:
EditorGUILayout.Slider(bounceSpeedFallof_Prop, 0f, 2f, new GUIContent("bounceSpeedFallof"));
break;
}
serializedObject.ApplyModifiedProperties ();
DrawDefaultInspector();
}
}
Answer by wachief · Jan 09, 2017 at 05:57 PM
both bTypes and bInpact get the same value.
Bullet.BulletTypes bTypes = (Bullet.BulletTypes)bulletType_Prop.enumValueIndex;
Bullet.BulletInpact bInpact = (Bullet.BulletInpact)bulletType_Prop.enumValueIndex;
so there is no reason for them to be different.
looks like you wanted this
Bullet.BulletInpact bInpact = (Bullet.BulletInpact)bulletInpact_Prop .enumValueIndex;
Haha, thanks :) sorry for making you read all the code do find my small error :p