Create clips in a track from editor script

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:

public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        serializedObject.Update();

        if (m_beginKeyIndex != -1 && m_endKeyIndex != -1 && m_beginKeyIndex >= m_endKeyIndex)
        {
            EditorGUILayout.HelpBox("Your begin key is above your end key.", MessageType.Error);
        }

        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(m_languageProp);
        bool languageChanged = EditorGUI.EndChangeCheck();

        if (m_beginKeyIndex != -1 && m_endKeyIndex != -1)
        {
            int newBeginIndex = EditorGUILayout.Popup(m_beginKeyIndex, m_locKeysValues);
            int newEndIndex = EditorGUILayout.Popup(m_endKeyIndex, m_locKeysValues);

            if (newBeginIndex != m_beginKeyIndex || newEndIndex != m_endKeyIndex)
            {
                m_beginKeyStringValue.stringValue = m_locData.OrderedKeys[newBeginIndex];
                m_endKeyStringValue.stringValue = m_locData.OrderedKeys[newEndIndex];
                m_beginKeyIndex = newBeginIndex;
                m_endKeyIndex = newEndIndex;
                if (m_beginKeyIndex < m_endKeyIndex)
                {
                    //Here is the function inside my custom track I called, wich simply do CreateDefaultClip();
                    (serializedObject.targetObject as DialogAndAudioPlayableTrack).CreateClipsFromKeys();
                    //All the following is apparantly not enough to actually save the created clip...
                    EditorUtility.SetDirty(serializedObject.targetObject);
                    EditorUtility.SetDirty((serializedObject.targetObject as DialogAndAudioPlayableTrack).timelineAsset);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                }
            }
        }

        if (languageChanged)
        {
            LoadLocData();
        }

        serializedObject.ApplyModifiedProperties();
    }

Here is the code in my CustomTrack:

    public void CreateClipsFromKeys()
    {
        ClearClips();

        AddClipToTrack();
    }

    public TimelineClip AddClipToTrack()
    {
        /*DialogAndAudioPlayableClip clip = */
        TimelineClip clip = CreateDefaultClip();
        clip.start = 0.0f;
        clip.duration = 2.0f;
        return clip;
    }

    #region Private

    private void ClearClips()
    {
        m_Clips.Clear();
    }

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…

Solved it by using a button rather than check if the enum index changed…

For those who are curious:

if (GUILayout.Button("Generate clips"))
        {
            if (m_beginKeyIndex < m_endKeyIndex)
            {
                (serializedObject.targetObject as DialogAndAudioPlayableTrack).CreateClipsFromKeys();
                EditorUtility.SetDirty(target);
                EditorUtility.SetDirty(serializedObject.targetObject);
                EditorUtility.SetDirty((serializedObject.targetObject as DialogAndAudioPlayableTrack).timelineAsset);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }
        }

This is working (probably some useless lines)

1 Like