|  |  |  |  | using UnityEditor; | 
					
						
							|  |  |  |  | using UnityEditor.Timeline; | 
					
						
							|  |  |  |  | using UnityEngine; | 
					
						
							|  |  |  |  | using UnityEngine.Playables; | 
					
						
							|  |  |  |  | using UnityEngine.Timeline; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | [ExecuteInEditMode] | 
					
						
							|  |  |  |  | public class TimelineTool : MonoBehaviour | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     private PlayableDirector PlayableDirector; | 
					
						
							|  |  |  |  |     public GameObject self; | 
					
						
							|  |  |  |  |     public GameObject target; | 
					
						
							|  |  |  |  |     public Animator self_animator; | 
					
						
							|  |  |  |  |     public Animator target_animator; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     private void OnEnable() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         PlayableDirector = GetComponent<PlayableDirector>(); | 
					
						
							|  |  |  |  |         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 AttackTrack) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 PlayableDirector.SetGenericBinding(track, self); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             if (track is AnimationTrack) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 PlayableDirector.SetGenericBinding(track, self_animator); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             if (track is ThrowPositionTrack) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 PlayableDirector.SetGenericBinding(track, target); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             if (track is ThrowAnimationTrack) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 PlayableDirector.SetGenericBinding(track, target_animator); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |              | 
					
						
							|  |  |  |  |             if (track is ThrowTurnTrack) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 PlayableDirector.SetGenericBinding(track, target); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         EditorApplication.delayCall += () => { Selection.activeGameObject = gameObject; }; | 
					
						
							|  |  |  |  |         TimelineEditor.GetOrCreateWindow().ShowTab(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } |