설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GridSelection.cs 3.5KB

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