Nehmen wir an, wir haben die folgenden Aufzählungen
public enum Section
{
A,
B
}
public enum SectionA
{
A1,
A2,
A3
}
public enum SectionB
{
B1,
B2,
B3
}
Ich möchte ein Skript mit zwei öffentlichen Enum-Feldern mit auswählbaren Werten als Dropdown-Liste aus dem Unity-Editor erstellen. Der erste dient zur Auswahl des Abschnitts (A oder B), während der zweite eine Aufzählung vom Typ Abschnitt A oder Abschnitt B sein sollte, je nachdem, welcher Wert im ersten Feld ausgewählt wurde.
Ich habe dafür folgende Skripte erstellt:
public class Item : MonoBehaviour
{
[HideInInspector]
public Section Section;
[HideInInspector]
public System.Enum Value;
}
[CustomEditor(typeof(Item))]
public class ItemEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
Item item = target as Item;
item.Section = (Section)EditorGUILayout.EnumPopup(item.Section);
switch (item.Section)
{
case Section.A:
item.Value = (SectionA)EditorGUILayout.EnumPopup(item.Value);
break;
case Section.B:
item.Value = (SectionB)EditorGUILayout.EnumPopup(item.Value);
break;
}
}
}
Es wird jedoch eine Ausnahme ausgelöst, die besagt, dass sie item.Value
null ist.
Ich habe auch versucht zu ersetzen
item.Value = (SectionA)EditorGUILayout.EnumPopup(item.Value);
mit
item.Value = (SectionA)EditorGUILayout.EnumPopup(item.Value ?? SectionA.A1);
um zu versuchen, ihm einen "Anfangswert" zu geben, aber dann A1
überschreibt der Wert den ausgewählten, wenn ich die Wiedergabetaste drücke. Irgendwelche Ideen?
Ich habe es behoben, indem ich ein Platzhalterfeld hinzugefügt habe, um den Aufzählungswert zu speichern.
public class Item : MonoBehaviour
{
[HideInInspector]
public Section Section;
[HideInInspector]
public System.Enum Value;
[HideInInspector]
public SectionA a;
[HideInInspector]
public SectionB b;
}
switch (item.Section)
{
case Section.A:
item.Value = EditorGUILayout.EnumPopup(item.a = (SectionA)(item.Value ?? item.a));
break;
case Section.B:
item.Value = EditorGUILayout.EnumPopup(item.b = (SectionB)(item.Value ?? item.b));
break;
}
Die vorherige Lösung kann beim Wechseln von Szenen häufig keine Werte speichern. Das funktioniert richtig.
public class Item : MonoBehaviour
{
public Section section;
public SectionA sectionA;
public SectionB sectionB;
public System.Enum Value
{
get
{
switch(section)
{
case Section.A:
return sectionA;
case Section.B:
return sectionB;
default:
return null;
}
}
}
}
[CustomEditor(typeof(Item))]
public class ItemEditor : Editor
{
SerializedProperty section;
SerializedProperty sectionA;
SerializedProperty sectionB;
void OnEnable()
{
section = serializedObject.FindProperty("section");
sectionA = serializedObject.FindProperty("sectionA");
sectionB = serializedObject.FindProperty("sectionB");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(section);
switch ((Section)section.enumValueIndex)
{
case Section.A:
EditorGUILayout.PropertyField(sectionA);
break;
case Section.B:
EditorGUILayout.PropertyField(sectionB);
break;
}
serializedObject.ApplyModifiedProperties();
}
}
Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.
Bei Verstößen wenden Sie sich bitte [email protected] Löschen.
Lass mich ein paar Worte sagen