Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animate state dropdown in inspector (e.g. enum), at least indirectly with editor script

Discussion in 'Immediate Mode GUI (IMGUI)' started by WiedemannD, Mar 23, 2015.

  1. WiedemannD

    WiedemannD

    Joined:
    Mar 4, 2015
    Posts:
    19
    Hi there,

    I've got a CharFace Monobehaviour. With a dropdown in its inspector I like to select, which state the characters eyes (for example) are in. Getting this to work is not a problem, BUT being able to animate this IS.

    I created an enum for the eyesState to use as the base for the different possible states (open, closed, etc), which can be selected via a dropdown. As I've understood, it is not possible to create keyframes for the change of an enum, which is sad, but hey. So I added this intermediate float value which gets set (during editor time) accordingly to the current state of the dropdown. Again all this works fine, BUT setting the float field via script sadly doesn't trigger the creation of a keyframe. To create a keyframe I manually have to change the value of the float field which makes the dropdown useless.

    I looked into AnimationUtility, but wasn't very successful with that.

    My questions are:

    1) In general, is there a workaround to get an editor dropdown somehow to create a keyframe in the currently open animation clip, at the current time etc pp, like float fields do for example?

    2) Is there a method I've missed, to "simply" set an animatable field to "changed" or whatever, to trigger the creation of a keyframe?

    3) AnimationUtility maybe usable for this cause somehow, but is there a way to actually get the currently in the animation window opened animation clip, the current time where the animation window time indicator is placed and then setting a keyframe via script for the float field?

    Cheers,
    Daniel

    EDIT:
    Maybe with SerializedObject/SerializedProperty there is a way to achieve this somehow?
     
    Last edited: Mar 23, 2015
  2. WiedemannD

    WiedemannD

    Joined:
    Mar 4, 2015
    Posts:
    19
    Well I don't seem to be getting somewhere with changing an editor float field via script while also creating keyframes when in animation mode.

    My current solution is not using a dropdown, but instead a slider which changes the corresponding float directly. With this workaround, animation people are constrained to specific values and keyframes can still be created in the regular way. I added the current state to the label in front, to be more clear/descriptive and additionally a simple label below describing all possible values. Furthermore a tooltip states that created keyframe tangents should be set to Constant, to avoid interpolating between them.

    Bildschirmfoto 2015-03-24 um 11.14.05.png

    As one might have guessed, I still coupled these sliders with enums. So I can still make use of their flexibility and comfort. Luckily, the slider automatically uses whole integers instead of float numbers for some reason.

    Code (CSharp):
    1.  
    2. public enum FT2DCharFaceMouth
    3. {
    4.     OPEN_WIDE,
    5.     OPEN_NORMAL,
    6.     OPEN_OH,
    7.     CLOSED
    8. }
    9.  
    10. ...
    11.  
    12. SerializedProperty eyesStatusFloatProp = serializedObject.FindProperty("eyesStatusFloat");
    13.  
    14. ...
    15.  
    16. // eyes stati slider
    17. FT2DCharFaceEyes eyesStatus = (FT2DCharFaceEyes) eyesStatusFloatProp.floatValue;
    18.  
    19. EditorGUILayout.Slider(eyesStatusFloatProp, 0, System.Enum.GetValues(typeof(FT2DCharFaceEyes)).Length - 1, new GUIContent("Eyes are " + eyesStatus, "When animating this, keyframe tangents should be set to Constant, to avoid interpolating between states."));
    20.  
    21. // extra description for eyes stati
    22. string[] eyesStati = System.Enum.GetNames(typeof(FT2DCharFaceEyes));
    23. string eyesStatiDescription = "";
    24. for(int i = 0; i < eyesStati.Length; i++)
    25. {
    26.     eyesStatiDescription += i + ": " + eyesStati[i] + ", ";
    27. }
    28. GUILayout.Label(eyesStatiDescription);
    29.