1
0

GridSelection.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. [Serializable]
  10. public class GridSelection : ScriptableObject
  11. {
  12. public static string kUpdateGridSelection = L10n.Tr("Update Grid Selection");
  13. /// <summary>Callback for when the active GridSelection has changed.</summary>
  14. public static event Action gridSelectionChanged;
  15. [SerializeField]
  16. private BoundsInt m_Position;
  17. private GameObject m_Target;
  18. [SerializeField]
  19. private Object m_PreviousSelection;
  20. /// <summary>Whether there is an active GridSelection made on a GridLayout.</summary>
  21. public static bool active { get { return Selection.activeObject is GridSelection && selection.m_Target != null; } }
  22. private static GridSelection selection { get { return Selection.activeObject as GridSelection; } }
  23. /// <summary>The cell coordinates of the active GridSelection made on the GridLayout.</summary>
  24. public static BoundsInt position
  25. {
  26. get { return selection != null ? selection.m_Position : new BoundsInt(); }
  27. set
  28. {
  29. if (selection != null && selection.m_Position != value)
  30. {
  31. RegisterUndo();
  32. selection.m_Position = value;
  33. if (gridSelectionChanged != null)
  34. gridSelectionChanged();
  35. }
  36. }
  37. }
  38. /// <summary>The GameObject of the GridLayout where the active GridSelection was made.</summary>
  39. public static GameObject target { get { return selection != null ? selection.m_Target : null; } }
  40. /// <summary>The Grid of the target of the active GridSelection.</summary>
  41. public static Grid grid { get { return selection != null && selection.m_Target != null ? selection.m_Target.GetComponentInParent<Grid>() : null; } }
  42. /// <summary>Creates a new GridSelection and sets it as the active GridSelection.</summary>
  43. /// <param name="target">The target GameObject for the GridSelection.</param>
  44. /// <param name="bounds">The cell coordinates of selection made.</param>
  45. public static void Select(Object target, BoundsInt bounds)
  46. {
  47. GridSelection newSelection = CreateInstance<GridSelection>();
  48. newSelection.m_PreviousSelection = Selection.activeObject;
  49. newSelection.m_Target = target as GameObject;
  50. newSelection.m_Position = bounds;
  51. Selection.activeObject = newSelection;
  52. if (gridSelectionChanged != null)
  53. gridSelectionChanged();
  54. }
  55. /// <summary>Clears the active GridSelection.</summary>
  56. public static void Clear()
  57. {
  58. if (active)
  59. {
  60. RegisterUndo();
  61. selection.m_Position = new BoundsInt();
  62. Selection.activeObject = selection.m_PreviousSelection;
  63. if (gridSelectionChanged != null)
  64. gridSelectionChanged();
  65. }
  66. }
  67. internal static void RegisterUndo()
  68. {
  69. if (selection != null)
  70. Undo.RegisterCompleteObjectUndo(selection, kUpdateGridSelection);
  71. }
  72. }
  73. }