Search Unity

Question Parameters in Custom Inspector Not Animating with Timeline

Discussion in 'Scripting' started by UlfvonEschlauer, Feb 18, 2022.

  1. UlfvonEschlauer

    UlfvonEschlauer

    Joined:
    Dec 3, 2014
    Posts:
    127
    Hello!
    I wrote a custom inspector for one of my scripts
    The purpose of my little project is to have cinematic rendering from within Unity. So of course, I want to animate those parameters using the Timeline and then record it using the Recorder. Because of that, the whole thing is supposed to run in both editor and in play mode.

    Generally everything is working fine in the Editor. Undos work, saving works, etc.
    While I record in the timeline, my animated parameters "turn red" and they animate correctly while I am scrubbing the timeline. But as soon as I switch to a different GameObject (e.g. back to the timeline object), or when I stop recording and scrub the timeline, the parameters do not animate anymore.
    Same also goes when I enter playmode to use the Recorder to record the animation to an image sequence or movie.
    I suppose that this is because the action happens in OnInspectorGUI() and that is of course only updated when the custom inspector is visible.
    Now I am sure that there is some sort of workaround for this and I did quite an extensive Google search but none of the solutions that I found seem to work.
    Any help is highly appreciated!

    Scripts (simplified):

    Base Class:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. using System;
    4. using System.Collections;
    5.  
    6. namespace Mycustomnamespace
    7. {
    8.     [ExecuteInEditMode]
    9.     public class MyClass : MonoBehaviour
    10.     {
    11.        [SerializeField]
    12.        public float myProperty = 10.0f;
    13.      
    14.        public void UpdateProperties()
    15.         {
    16.            meshRenderer.sharedMaterial.SetFloat("myShaderProperty", myProperty );
    17.        }
    18.    }
    19. }
    Custom Inspector:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections.Generic;
    4. using System;
    5. using System.Collections;
    6.  
    7. namespace Mycustomnamespace
    8. {
    9.  
    10.     [CustomEditor(typeof(MyClass)), CanEditMultipleObjects, System.Serializable]
    11.     public class MyClassInspector : Editor
    12.     {
    13.      
    14.        private SerializedProperty myPropertyCtrl;
    15.      
    16.        void OnEnable()
    17.        {
    18.            InitializeProperties();
    19.        }
    20.      
    21.        void InitializeProperties()
    22.        {
    23.            MyClass myClass = (myClass)target;
    24.            myPropertyCtrl   = serializedObject.FindProperty("myProperty");
    25.        }
    26.      
    27.        public override void OnInspectorGUI()
    28.         {
    29.            serializedObject.UpdateIfDirtyOrScript();
    30.            MyClass myClass = (myClass)target;
    31.            EditorGUILayout.Slider(myPropertyCtrl, 1, 10);
    32.            myClass.UpdateProperties();
    33.            serializedObject.ApplyModifiedProperties();
    34.        }
    35.    }
    36. }
     
    Last edited: Feb 18, 2022
  2. UlfvonEschlauer

    UlfvonEschlauer

    Joined:
    Dec 3, 2014
    Posts:
    127
    Bump. Is there really no one who can help me with this?