ClickObjectSelectionUpdateOperation.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. namespace O3DWB
  4. {
  5. public class ClickObjectSelectionUpdateOperation : ObjectSelectionUpdateOperation
  6. {
  7. #region Public Methods
  8. public override void Perform()
  9. {
  10. if (Event.current.InvolvesLeftMouseButton())
  11. {
  12. Event.current.DisableInSceneView();
  13. MouseCursorRayHit cursorRayHit = ObjectSelection.Get().GetObjectPickedByCursor();
  14. if (cursorRayHit.WasAnObjectHit)
  15. {
  16. GameObject closestHitObject = cursorRayHit.SortedObjectRayHits[0].HitObject;
  17. if (cursorRayHit.WasLightObjectHit || cursorRayHit.WasParticleSystemHit) closestHitObject = cursorRayHit.GetClosestHitParticleSystemOrLightObject();
  18. // Note: If append mode is enabled, we will toggle the selected state of the picked object. Otherwise,
  19. // we will clear the current selection and select only the picked game object.
  20. if (AllShortcutCombos.Instance.EnableAppendObjectsToSelection.IsActive()) ToggleObjectSelectedState(closestHitObject);
  21. else ClearSelectionAndSelectObject(closestHitObject);
  22. if(Event.current.clickCount == 2)
  23. {
  24. UndoEx.RecordForToolAction(ObjectSelection.Get());
  25. ObjectSelectionActions.SelectAllObjectsWithSamePrefabAsObject(closestHitObject);
  26. ObjectSelection.Get().ObjectSelectionGizmos.OnObjectSelectionUpdatedUsingMouseClick();
  27. }
  28. }
  29. else ClearSelectionWhenNoObjectPicked();
  30. }
  31. }
  32. #endregion
  33. #region Private Methods
  34. private void ToggleObjectSelectedState(GameObject gameObject)
  35. {
  36. ObjectSelection objectSelection = ObjectSelection.Get();
  37. ObjectSelectionUpdateMode selectionUpdateMode = ObjectSelectionSettings.Get().SelectionUpdateMode;
  38. UndoEx.RecordForToolAction(objectSelection);
  39. if(selectionUpdateMode == ObjectSelectionUpdateMode.EntireHierarchy)
  40. {
  41. if (objectSelection.IsGameObjectSelected(gameObject)) objectSelection.RemoveEntireGameObjectHierarchyFromSelection(gameObject);
  42. else objectSelection.AddEntireGameObjectHierarchyToSelection(gameObject);
  43. }
  44. else
  45. {
  46. if (objectSelection.IsGameObjectSelected(gameObject)) objectSelection.RemoveGameObjectFromSelection(gameObject);
  47. else objectSelection.AddGameObjectToSelection(gameObject);
  48. }
  49. objectSelection.ObjectSelectionGizmos.OnObjectSelectionUpdatedUsingMouseClick();
  50. }
  51. private void ClearSelectionAndSelectObject(GameObject gameObject)
  52. {
  53. ObjectSelection objectSelection = ObjectSelection.Get();
  54. ObjectSelectionUpdateMode selectionUpdateMode = ObjectSelectionSettings.Get().SelectionUpdateMode;
  55. if (selectionUpdateMode == ObjectSelectionUpdateMode.EntireHierarchy)
  56. {
  57. UndoEx.RecordForToolAction(objectSelection);
  58. objectSelection.Clear();
  59. objectSelection.AddEntireGameObjectHierarchyToSelection(gameObject);
  60. objectSelection.ObjectSelectionGizmos.OnObjectSelectionUpdatedUsingMouseClick();
  61. }
  62. else
  63. {
  64. // Note: We only continue if the picked object is not the only currently selected object. If it is, it means
  65. // we would be registering an Undo operation for nothing.
  66. if (!ObjectSelection.Get().IsSameAs(gameObject))
  67. {
  68. UndoEx.RecordForToolAction(objectSelection);
  69. objectSelection.Clear();
  70. objectSelection.AddGameObjectToSelection(gameObject);
  71. objectSelection.ObjectSelectionGizmos.OnObjectSelectionUpdatedUsingMouseClick();
  72. }
  73. }
  74. }
  75. private void ClearSelectionWhenNoObjectPicked()
  76. {
  77. ObjectSelection objectSelection = ObjectSelection.Get();
  78. // When no object is picked, we will clear the selection.
  79. // Note: We only clear the selection if:
  80. // -the current number of selected objects is not 0. This allows us to avoid registering
  81. // an Undo operation for nothing;
  82. // -append mode is not active. If it is, the user may have clicked an empty area in order
  83. // to start selecting multiple objects with the selection shape. In this case we don't
  84. // want to clear the selection because otherwise, there would be no way for the user to
  85. // append objects to the selection using the selection shape;
  86. // -deselect objects with selection shape mode is not active. If it is, clearing the object
  87. // selection would not allow the user to deselect only the objects they desire because when
  88. // they click in an empty space all objects would already be deselected.
  89. if (objectSelection.NumberOfSelectedObjects != 0 &&
  90. !AllShortcutCombos.Instance.EnableAppendObjectsToSelection.IsActive() &&
  91. !AllShortcutCombos.Instance.EnableDeselectObjectsWithSelectionShape.IsActive())
  92. {
  93. UndoEx.RecordForToolAction(objectSelection);
  94. objectSelection.Clear();
  95. }
  96. }
  97. #endregion
  98. }
  99. }
  100. #endif