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

Create clips in a track from editor script

Discussion in 'Timeline' started by Yannick_Montes, Apr 27, 2018.

  1. Yannick_Montes

    Yannick_Montes

    Joined:
    Apr 4, 2018
    Posts:
    2
    Hi there!

    I've done few scripts around timeline this past weeks, a bit struggling with everything x)

    But today I can't solve my issue whatever I try.

    I have a CustomEditor for my track, and I call a function which is inside my Track script in order to create clips.
    I debugged it, and the clip is correctly instantiated, but it's not saved between two frames...

    Here is the code:

    Code (CSharp):
    1. public override void OnInspectorGUI()
    2.     {
    3.         base.OnInspectorGUI();
    4.  
    5.         serializedObject.Update();
    6.  
    7.         if (m_beginKeyIndex != -1 && m_endKeyIndex != -1 && m_beginKeyIndex >= m_endKeyIndex)
    8.         {
    9.             EditorGUILayout.HelpBox("Your begin key is above your end key.", MessageType.Error);
    10.         }
    11.  
    12.         EditorGUI.BeginChangeCheck();
    13.         EditorGUILayout.PropertyField(m_languageProp);
    14.         bool languageChanged = EditorGUI.EndChangeCheck();
    15.  
    16.         if (m_beginKeyIndex != -1 && m_endKeyIndex != -1)
    17.         {
    18.             int newBeginIndex = EditorGUILayout.Popup(m_beginKeyIndex, m_locKeysValues);
    19.             int newEndIndex = EditorGUILayout.Popup(m_endKeyIndex, m_locKeysValues);
    20.  
    21.             if (newBeginIndex != m_beginKeyIndex || newEndIndex != m_endKeyIndex)
    22.             {
    23.                 m_beginKeyStringValue.stringValue = m_locData.OrderedKeys[newBeginIndex];
    24.                 m_endKeyStringValue.stringValue = m_locData.OrderedKeys[newEndIndex];
    25.                 m_beginKeyIndex = newBeginIndex;
    26.                 m_endKeyIndex = newEndIndex;
    27.                 if (m_beginKeyIndex < m_endKeyIndex)
    28.                 {
    29.                     //Here is the function inside my custom track I called, wich simply do CreateDefaultClip();
    30.                     (serializedObject.targetObject as DialogAndAudioPlayableTrack).CreateClipsFromKeys();
    31.                     //All the following is apparantly not enough to actually save the created clip...
    32.                     EditorUtility.SetDirty(serializedObject.targetObject);
    33.                     EditorUtility.SetDirty((serializedObject.targetObject as DialogAndAudioPlayableTrack).timelineAsset);
    34.                     AssetDatabase.SaveAssets();
    35.                     AssetDatabase.Refresh();
    36.                 }
    37.             }
    38.         }
    39.  
    40.         if (languageChanged)
    41.         {
    42.             LoadLocData();
    43.         }
    44.  
    45.         serializedObject.ApplyModifiedProperties();
    46.     }
    Here is the code in my CustomTrack:

    Code (CSharp):
    1.     public void CreateClipsFromKeys()
    2.     {
    3.         ClearClips();
    4.  
    5.         AddClipToTrack();
    6.     }
    7.  
    8.     public TimelineClip AddClipToTrack()
    9.     {
    10.         /*DialogAndAudioPlayableClip clip = */
    11.         TimelineClip clip = CreateDefaultClip();
    12.         clip.start = 0.0f;
    13.         clip.duration = 2.0f;
    14.         return clip;
    15.     }
    16.  
    17.     #region Private
    18.  
    19.     private void ClearClips()
    20.     {
    21.         m_Clips.Clear();
    22.     }
    TO sum up: the problem is that the clip is correctly added to the track, but next frame of OnInspectorGUI, it go back to older state, where it was not created...
     
  2. Yannick_Montes

    Yannick_Montes

    Joined:
    Apr 4, 2018
    Posts:
    2
    Solved it by using a button rather than check if the enum index changed...

    For those who are curious:

    Code (CSharp):
    1. if (GUILayout.Button("Generate clips"))
    2.         {
    3.             if (m_beginKeyIndex < m_endKeyIndex)
    4.             {
    5.                 (serializedObject.targetObject as DialogAndAudioPlayableTrack).CreateClipsFromKeys();
    6.                 EditorUtility.SetDirty(target);
    7.                 EditorUtility.SetDirty(serializedObject.targetObject);
    8.                 EditorUtility.SetDirty((serializedObject.targetObject as DialogAndAudioPlayableTrack).timelineAsset);
    9.                 AssetDatabase.SaveAssets();
    10.                 AssetDatabase.Refresh();
    11.             }
    12.         }
    This is working (probably some useless lines)
     
    senkal_ likes this.