TimelineWindow_Selection.cs 2.6 KB

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