TimelineWindow_Selection.cs 2.9 KB

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