TimelineWindow_Selection.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using UnityEngine;
  2. using UnityEngine.Playables;
  3. using UnityEngine.Timeline;
  4. namespace UnityEditor.Timeline
  5. {
  6. partial class TimelineWindow
  7. {
  8. [SerializeField]
  9. SequencePath m_SequencePath;
  10. void OnSelectionChange()
  11. {
  12. RefreshSelection(false);
  13. }
  14. void RefreshSelection(bool forceRebuild)
  15. {
  16. // if we're in Locked mode, keep current selection - don't use locked property because the
  17. // sequence hierarchy may need to be rebuilt and it assumes no asset == unlocked
  18. if (m_LockTracker.isLocked || (state != null && state.recording))
  19. {
  20. RestoreLastSelection(forceRebuild);
  21. return;
  22. }
  23. // selection is a TimelineAsset
  24. Object selectedObject = Selection.activeObject as TimelineAsset;
  25. if (selectedObject != null)
  26. {
  27. SetCurrentSelection(Selection.activeObject);
  28. return;
  29. }
  30. // selection is a GameObject, or a prefab with a director
  31. var selectedGO = Selection.activeGameObject;
  32. if (selectedGO != null)
  33. {
  34. bool isSceneObject = !PrefabUtility.IsPartOfPrefabAsset(selectedGO);
  35. bool hasDirector = selectedGO.GetComponent<PlayableDirector>() != null;
  36. if (isSceneObject || hasDirector)
  37. {
  38. SetCurrentSelection(selectedGO);
  39. return;
  40. }
  41. }
  42. // otherwise, keep the same selection.
  43. RestoreLastSelection(forceRebuild);
  44. }
  45. void RestoreLastSelection(bool forceRebuild)
  46. {
  47. state.SetCurrentSequencePath(m_SequencePath, forceRebuild);
  48. }
  49. void SetCurrentSelection(Object obj)
  50. {
  51. var selectedGameObject = obj as GameObject;
  52. if (selectedGameObject != null)
  53. {
  54. PlayableDirector director = TimelineUtility.GetDirectorComponentForGameObject(selectedGameObject);
  55. SetCurrentTimeline(director);
  56. }
  57. else
  58. {
  59. var selectedSequenceAsset = obj as TimelineAsset;
  60. if (selectedSequenceAsset != null)
  61. {
  62. SetCurrentTimeline(selectedSequenceAsset);
  63. }
  64. }
  65. Repaint();
  66. }
  67. }
  68. }