GridSelection.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Scripting.APIUpdating;
  4. using Object = UnityEngine.Object;
  5. namespace UnityEditor.Tilemaps
  6. {
  7. /// <summary>Stores the selection made on a GridLayout.</summary>
  8. [MovedFrom(true, "UnityEditor", "UnityEditor")]
  9. public class GridSelection : ScriptableObject
  10. {
  11. /// <summary>Callback for when the active GridSelection has changed.</summary>
  12. public static event Action gridSelectionChanged;
  13. private BoundsInt m_Position;
  14. private GameObject m_Target;
  15. [SerializeField] private Object m_PreviousSelection;
  16. /// <summary>Whether there is an active GridSelection made on a GridLayout.</summary>
  17. public static bool active { get { return Selection.activeObject is GridSelection && selection.m_Target != null; } }
  18. private static GridSelection selection { get { return Selection.activeObject as GridSelection; } }
  19. /// <summary>The cell coordinates of the active GridSelection made on the GridLayout.</summary>
  20. public static BoundsInt position
  21. {
  22. get { return selection != null ? selection.m_Position : new BoundsInt(); }
  23. set
  24. {
  25. if (selection != null && selection.m_Position != value)
  26. {
  27. selection.m_Position = value;
  28. if (gridSelectionChanged != null)
  29. gridSelectionChanged();
  30. }
  31. }
  32. }
  33. /// <summary>The GameObject of the GridLayout where the active GridSelection was made.</summary>
  34. public static GameObject target { get { return selection != null ? selection.m_Target : null; } }
  35. /// <summary>The Grid of the target of the active GridSelection.</summary>
  36. public static Grid grid { get { return selection != null && selection.m_Target != null ? selection.m_Target.GetComponentInParent<Grid>() : null; } }
  37. /// <summary>Creates a new GridSelection and sets it as the active GridSelection.</summary>
  38. /// <param name="target">The target GameObject for the GridSelection.</param>
  39. /// <param name="bounds">The cell coordinates of selection made.</param>
  40. public static void Select(Object target, BoundsInt bounds)
  41. {
  42. GridSelection newSelection = CreateInstance<GridSelection>();
  43. newSelection.m_PreviousSelection = Selection.activeObject;
  44. newSelection.m_Target = target as GameObject;
  45. newSelection.m_Position = bounds;
  46. Selection.activeObject = newSelection;
  47. if (gridSelectionChanged != null)
  48. gridSelectionChanged();
  49. }
  50. /// <summary>Clears the active GridSelection.</summary>
  51. public static void Clear()
  52. {
  53. if (active)
  54. {
  55. selection.m_Position = new BoundsInt();
  56. Selection.activeObject = selection.m_PreviousSelection;
  57. if (gridSelectionChanged != null)
  58. gridSelectionChanged();
  59. }
  60. }
  61. }
  62. }