Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question How to use Keyframe Recording on Custom Inspector fields

Discussion in 'Animation' started by thunder725, Jun 21, 2022.

  1. thunder725


    Jan 17, 2022
    Hello everybody,

    First of all sorry if this should've been in the Scripting Forum, I don't know which one fits the best.

    I am currently working on a project where, for a hitbox creator script, I am using a Custom Inspector (or is it considered a Custom Editor?) for the first time.
    In this project I'll also animate a lot of this Script's values using Animations... and here is a problem.

    While the Animations are only able to place keyframes on the Script's variables and not the Custom Inspector's variables, I figured that the Keyframe Recording Mode would work since all the Custom Inspector does is to allow for a better handling of the behind-the-scenes variables. It still modifies their values as if it was a regular inspector, but in a more intuitive way overall.
    So I thought that the Keyframe Recording Mode would detect that the values change and record them, even if through a Custom Inspector.

    But when using the Keyframe Recording Mode, none of what I did to the Custom Inspector ever placed a keyframe, or modified an existing keyframe's values for that matter.
    The only way to do so was to edit the values in the Default Inspector (which should be hidden, that's the purpose of my custom one) or in the Animation Window directly (which defeats the purpose of the Keyframe Recording Mode).

    Even weirder: when I changed the values in the Custom Editor and waited a bit (1-2 seconds), the numerical values within the Animation Window changed. But when changing frames and returning to the one which should have changed, it returned to the previous value, as if it was a visual change which was not saved in the animation.

    I tried searching for similar problems but I don't have any idea of the exact terminology I should use so I didn't find anything related.

    Is there any way to allow for some CustomInspector-KeyframeRecording interaction or am I forced to rethink my workflow?

    Thank you in advance for any answer!

    Some code extracts & screenshots for context:

    My animated GameObject, viewed while in Keyframe Recording Mode.
    I made visible both the Default Inspector and my Custom One just below.

    And here is the Custom Inspector part of this script's code (probably not clean sorry in advance)
    Code (CSharp):
    1. #if UNITY_EDITOR
    2. [CustomEditor(typeof(LocalAttackHitboxCreator))]
    3. public class LocalAttackHitboxCreatorEditor : Editor
    4. {
    5.     public override void OnInspectorGUI()
    6.     {
    7.         // Apparently this allows to get a reference to the script's instance
    8.         LocalAttackHitboxCreator _script = target as LocalAttackHitboxCreator;
    10.         EditorGUILayout.LabelField("This is the Default Inspector", EditorStyles.boldLabel);
    11.         EditorGUILayout.Space();
    13.         DrawDefaultInspector();
    15.         EditorGUILayout.Space();
    16.         EditorGUILayout.LabelField("This is the Custom Inspector", EditorStyles.boldLabel);
    19.         _script.HitboxType = (LocalAttackHitboxCreator.HitboxTypeEnum)EditorGUILayout.EnumPopup("Hitbox Type", _script.HitboxType);
    21.         if (_script.HitboxType == LocalAttackHitboxCreator.HitboxTypeEnum.Capsule)
    22.         {
    23.             EditorGUILayout.LabelField("The position of the end of the capsule relative to the start", EditorStyles.boldLabel);
    24.             _script.capsuleSecondaryHitboxOffset = EditorGUILayout.Vector3Field("Secondary Center Offset", _script.capsuleSecondaryHitboxOffset);
    25.         }
    27.         EditorGUILayout.Space();
    28.         EditorGUILayout.Space();
    30.         _script.HitboxID =  EditorGUILayout.IntSlider("Hitbox ID", _script.HitboxID, 0, 15);
    31.         _script.editorAttackID = EditorGUILayout.IntField("Hit ID", _script.editorAttackID);
    33.         EditorGUILayout.Space();
    34.         EditorGUILayout.Space();
    36.         _script.HitboxRadius = EditorGUILayout.FloatField("Hitbox Radius",_script.HitboxRadius);
    37.         _script.HitboxDamage = EditorGUILayout.FloatField("Hitbox Damage",_script.HitboxDamage);
    39.         EditorGUILayout.Space();
    41.         _script.specialLaunchingAngle = EditorGUILayout.Toggle("Use a Special Launching Angle", _script.specialLaunchingAngle);
    43.         if (!_script.specialLaunchingAngle)
    44.         {
    45.             _script.HitboxLaunchingAngle = EditorGUILayout.Slider("Hitbox Launching Angle", _script.HitboxLaunchingAngle, 0, 360);
    46.             EditorGUILayout.LabelField("0 = horizontal direction the character is facing. Increases counter-clockwise", EditorStyles.miniLabel);
    47.         }
    48.         else
    49.         {
    50.             _script.angleType = (LocalAttackHitboxCreator.AngleType)EditorGUILayout.EnumPopup("Special Launching Angle Type", _script.angleType);
    52.             if (_script.angleType == LocalAttackHitboxCreator.AngleType.SakuraiAngle)
    53.             {
    54.                 _script.HitboxLaunchingAngle = 361;
    55.                 EditorGUILayout.LabelField("Standard increasing horizontal-to-diagonal angle", EditorStyles.miniLabel);
    56.             }
    57.             else
    58.             {
    59.                 _script.HitboxLaunchingAngle = 367;
    60.                 EditorGUILayout.LabelField("Angle specialized for multi-hit attacks", EditorStyles.miniLabel);
    61.                 if (_script.HitboxType == LocalAttackHitboxCreator.HitboxTypeEnum.Capsule)
    62.                 {
    63.                     EditorGUILayout.LabelField("DO NOT USE AUTOLINK ANGLE WITH A CAPSULE", EditorStyles.boldLabel);
    64.                 }
    66.             }
    67.         }
    68.         EditorGUILayout.Space();
    69.         EditorGUILayout.Space();
    71.         _script.hitboxKnockbackValue = EditorGUILayout.FloatField("Hitbox Knockback Value", _script.hitboxKnockbackValue);
    72.         _script.hitboxKnockbackScaling = EditorGUILayout.FloatField("Hitbox Knockback Scaling",_script.hitboxKnockbackScaling);
    73.         EditorGUILayout.LabelField("Default Knockback Scaling is 100", EditorStyles.miniLabel);
    75.         // Disable when not used
    76.         //_script.hitboxVisualizationColorAlpha = EditorGUILayout.Slider("Hitbox pre-visualization alpha", _script.hitboxVisualizationColorAlpha, 0, 1);
    77.     }
    78. }
    79. #endif