using UnityEditor; using UnityEditor.Timeline; using UnityEngine; using UnityEngine.Playables; using UnityEngine.Timeline; [ExecuteInEditMode] public class TimelineTool : MonoBehaviour { private PlayableDirector PlayableDirector; private GameObject animator; private void OnEnable() { PlayableDirector = GetComponent(); animator = GetComponentInChildren().gameObject; Selection.selectionChanged += OnSelectionChanged; } private void OnDisable() { Selection.selectionChanged -= OnSelectionChanged; } private void OnSelectionChanged() { var selectedObject = UnityEditor.Selection.activeObject; if (selectedObject == null) return; if (selectedObject.GetType() != typeof(TimelineAsset)) return; // 打印选定对象的名称 var timeline = (TimelineAsset)selectedObject; PlayableDirector.playableAsset = timeline; foreach (var track in timeline.GetOutputTracks()) { if (track is AnimationTrack) { PlayableDirector.SetGenericBinding(track, animator); } if (track is AttackTrack) { PlayableDirector.SetGenericBinding(track, transform); } } EditorApplication.delayCall += () => { Selection.activeGameObject = gameObject; }; TimelineEditor.GetOrCreateWindow().ShowTab(); } }