Search Unity

Question How to get rid of "unused bindings"?

Discussion in 'Timeline' started by Peter77, May 23, 2023.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    I'm using Unity 2022.2.18 and Timeline 1.7.4.

    How can I delete the stuff listed under "Unused Bindings" in the Playable Director Component?

    I don't need a perfect solution, just the quickest and dirtiest method that will do the trick.
     
  2. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    127
    There should be a [-] button below the binding list. you can delete unused bindings by clicking it.
     
    Peter77 likes this.
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Thanks, that definitely works. But I have like thousands of unused bindings and the Inspector is super unresponsive. It takes forever to delete just one. Is there any way to get rid of all of them in one go?
     
  4. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    127
    You can use this script to remove them. Just put this script in your project, open scene and attach the script to any arbitrary GameObject, put all PlayableDirectors you want to clear unused bindings into Directors, and hit [Remove Unused Bindings]. You can then remove this script from your project.
    (I have no warranty to the script)

    remove_unused_bindings.png

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using UnityEditor;
    5. using UnityEngine;
    6. using UnityEngine.Playables;
    7.  
    8. public class RemoveUnusedBindings : MonoBehaviour
    9. {
    10.     [SerializeField] private PlayableDirector[] directors;
    11.  
    12.  
    13. #if UNITY_EDITOR
    14.  
    15.     [CustomEditor(typeof(RemoveUnusedBindings))]
    16.     public class RemoveUnusedBindingsEditor : Editor
    17.     {
    18.         public override void OnInspectorGUI()
    19.         {
    20.             base.OnInspectorGUI();
    21.             if (GUILayout.Button("Remove Unused Bindings")) {
    22.                 RemoveUnusedBindings();
    23.             }
    24.         }
    25.  
    26.         private void RemoveUnusedBindings()
    27.         {
    28.             var directors = (target as RemoveUnusedBindings).directors;
    29.             if (directors == null) return;
    30.  
    31.             foreach (var director in directors)
    32.             {
    33.                 if (!director) continue;
    34.  
    35.                 var directorSO = new SerializedObject(director);
    36.                 var sceneBindings = directorSO.FindProperty("m_SceneBindings");
    37.                 var assetBindings = director.playableAsset.outputs.ToArray();
    38.  
    39.                 for (var i = sceneBindings.arraySize - 1; i >= 0; i--) {
    40.                     var key = sceneBindings.GetArrayElementAtIndex(i).FindPropertyRelative("key").objectReferenceValue;
    41.                     bool isUnused = assetBindings.All(binding => binding.sourceObject != key);
    42.                     if (isUnused) {
    43.                         sceneBindings.DeleteArrayElementAtIndex(i);
    44.                     }
    45.                 }
    46.  
    47.                 directorSO.ApplyModifiedProperties();
    48.             }
    49.         }
    50.     }
    51.  
    52. #endif
    53. }
    54.  
    ... or, just reset your PlayableDirector Component, and reattach those bindings which are in use.
    (if there is little PlayableDirector to work)