How do check which enum value was selected?
I have this enum in one script:
public enum TypeOfRotation
{
SingleRotation,
LoopedRotation,
}
public TypeOfRotation RotationType;
And then in another script I want set the value of elementNameProperty (a string) to be whatever was selected in the enum (SingleRotation or LoopedRotation). The following code doesn't work and I don't quite know how to achieve this.
RotationTimeline.elementNameProperty = RotationTimeline.GetItem(0).FindPropertyRelative("TypeOfRotation").enumNames;
$$anonymous$$erry Christmas to you @alexanderameye. Could you please show me what the script/structure RotationTimeline
looks like?
Sure!
I'll give you the 2 scripts I used:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using $$anonymous$$alee;
public class Rotations : $$anonymous$$onoBehaviour {
public List<RotationsChild> RotationTimeline;
[System.Serializable]
public class RotationsChild
{
public string SingleRotation = "Single";
[HideInInspector]
public string LoopedRotation;
public enum TypeOfRotation
{
SingleRotation,
LoopedRotation,
}
public TypeOfRotation RotationType;
public float InitialAngle = 0f;
public float FinalAngle = 90f;
public float Speed = 4f;
}
class Container {
[SerializeField]
private RotationsChild[] RotationTimeline;
}
[System.Serializable]
public class RotationsChildList : ReorderableArray<RotationsChild> {
}
}
and then script number 2
using UnityEngine;
using UnityEditor;
using System.Collections;
using $$anonymous$$alee.Editor;
[CanEdit$$anonymous$$ultipleObjects]
[CustomEditor(typeof(Rotations))]
public class RotationsEditor : Editor
{
private ReorderableList RotationTimeline;
void OnEnable()
{
RotationTimeline = new ReorderableList(serializedObject.FindProperty("RotationTimeline"));
}
public override void OnInspectorGUI()
{
//Ins$$anonymous$$d of checking the value of 'SingleRotation' here, I want it to check what element of the RotationType enum was selected
RotationTimeline.elementNameProperty = RotationTimeline.GetItem(0).FindPropertyRelative("SingleRotation").stringValue;
serializedObject.Update();
RotationTimeline.DoLayoutList();
serializedObject.Apply$$anonymous$$odifiedProperties();
}
}
Answer by TBruce · Dec 26, 2016 at 12:04 AM
Hi @alexanderameye, If I understand you correctly then you are looking for something like this
public static T ParseEnum<T>(string value)
{
return (T) System.Enum.Parse(typeof(T), value, true);
}
public override void OnInspectorGUI()
{
SerializedProperty property = RotationTimeline.serializedProperty.GetArrayElementAtIndex(0).FindPropertyRelative("RotationType");
Rotations.RotationsChild.TypeOfRotation rotationType = ParseEnum<Rotations.RotationsChild.TypeOfRotation>(property.stringValue);
if (rotationType == Rotations.RotationsChild.TypeOfRotation.SingleRotation)
{
}
//Instead of checking the value of 'SingleRotation' here, I want it to check what element of the RotationType enum was selected
// RotationTimeline.elementNameProperty = RotationTimeline.GetItem(0).FindPropertyRelative("SingleRotation").stringValue;
serializedObject.Update();
RotationTimeline.DoLayoutList();
serializedObject.ApplyModifiedProperties();
}
Lines were getting too long to read, I added the function ParseEnum<>()
which returns an enum value from a string. I also broke everything down into multiple lines and added the if
comparison statement.
I do mot know what Malee
and Malee.Editor
are as I did not have the associated scripts, so for testing purposes I had to comment out the uses statements and any code associated. I also had to add the uses statement
using UnityEditorInternal;
to use ReorderableList
.
$$anonymous$$alee is a class someone wrote and posted on the forums.
'It's an attempt to mimic functionality of the ReorderableList within Unity while adding some extended functionality.'
The thing is, $$anonymous$$alee.Editor kind of conflicts with UnityEditorInternal, and doesn't contain definitions for GetArrayElementAtIndex etc. So it's kind of hard for you guys here to answer my question, I'll try to contact the original author of the $$anonymous$$alee scripts ins$$anonymous$$d, thank you for your effort though!
$$anonymous$$
I figured that the $$anonymous$$alee/$$anonymous$$alee.Editor
had something to do with a custom ReorderableList
. Barring that, did this not work for you? It is not specific to any library.
It did not :/ First it gave me the following error:
Assets/ReorderableList/Example/Editor/RotationsEditor.cs(16,10): error CS0104: `ReorderableList' is an ambiguous reference between `$$anonymous$$alee.Editor.ReorderableList' and `UnityEditorInternal.ReorderableList'
What lead me too remove the UnityEditorInternal, but that gave me the next error
Assets/ReorderableList/Example/Editor/RotationsEditor.cs(32,50): error CS1061: Type `$$anonymous$$alee.Editor.ReorderableList' does not contain a definition for `serializedProperty' and no extension method `serializedProperty' of type `$$anonymous$$alee.Editor.ReorderableList' could be found. Are you missing an assembly reference?
Hi @alexanderameye,
Sorry for the long silence (holidays). I seem to have misunderstood what you were ai$$anonymous$$g for and am still not 100% sure what you are looking to do. But I have created the following three Unity packages
Uses the $$anonymous$$alee framework (I found $$anonymous$$alee in this discussion).
Uses the $$anonymous$$alee framework - This $$anonymous$$alee version has a custom editor (The only way I could set the default values with this)
Note: I did not know what fields that you wanted to show so I have added the following items to be displayed (you can add or remove items easily from either of the versions)
SingleRotation
RotationType
InitialAngle
FinalAngle
Speed