using System; using System.Collections.Generic; using System.Linq; using UnityEngine.Playables; using Object = UnityEngine.Object; namespace UnityEditor.Timeline { readonly struct PreviewedBindings where T : Object { readonly IEnumerable m_UniqueBindings; readonly IReadOnlyDictionary> m_BindingsPerObject; PreviewedBindings(IEnumerable uniqueBindings, IReadOnlyDictionary> bindingsPerObject) { m_UniqueBindings = uniqueBindings; m_BindingsPerObject = bindingsPerObject; } public IEnumerable GetUniqueBindings() => m_UniqueBindings ?? Enumerable.Empty(); public IEnumerable GetBindingsForObject(Object track) { if (m_BindingsPerObject != null && m_BindingsPerObject.TryGetValue(track, out HashSet bindings)) return bindings; return Enumerable.Empty(); } public static PreviewedBindings GetPreviewedBindings(IEnumerable directors) { var uniqueBindings = new HashSet(); var bindingsPerTrack = new Dictionary>(); foreach (PlayableDirector director in directors) { if (director.playableAsset == null) continue; foreach (PlayableBinding output in director.playableAsset.outputs) { var binding = director.GetGenericBinding(output.sourceObject) as T; Add(output.sourceObject, binding, uniqueBindings, bindingsPerTrack); } } return new PreviewedBindings(uniqueBindings, bindingsPerTrack); } static void Add(Object obj, T binding, HashSet bindings, Dictionary> bindingsPerObject) { if (binding == null) return; bindings.Add(binding); if (bindingsPerObject.TryGetValue(obj, out HashSet objectBindings)) objectBindings.Add(binding); else bindingsPerObject.Add(obj, new HashSet { binding }); } } }