暫無描述
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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using UnityEditor.SceneManagement;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.Scripting.APIUpdating;
  6. using Object = UnityEngine.Object;
  7. namespace UnityEditor.Tilemaps
  8. {
  9. /// <summary>Stores the selection made on a GridLayout.</summary>
  10. [MovedFrom(true, "UnityEditor", "UnityEditor")]
  11. [HelpURL("https://docs.unity3d.com/Manual/TilemapPainting-SelectionTool.html#GridSelect")]
  12. [Serializable]
  13. public class GridSelection : ScriptableObject
  14. {
  15. private static string kUpdateGridSelection = L10n.Tr("Update Grid Selection");
  16. /// <summary>Callback for when the active GridSelection has changed.</summary>
  17. public static event Action gridSelectionChanged;
  18. [SerializeField]
  19. private BoundsInt m_Position;
  20. private GameObject m_Target;
  21. [SerializeField]
  22. private Object m_PreviousSelection;
  23. [SerializeField]
  24. private Scene m_Scene;
  25. [SerializeField]
  26. private GameObject m_OriginalPalette;
  27. /// <summary>Whether there is an active GridSelection made on a GridLayout.</summary>
  28. public static bool active { get { return Selection.activeObject is GridSelection && selection.m_Target != null; } }
  29. private static GridSelection selection { get { return Selection.activeObject as GridSelection; } }
  30. /// <summary>The cell coordinates of the active GridSelection made on the GridLayout.</summary>
  31. public static BoundsInt position
  32. {
  33. get { return selection != null ? selection.m_Position : new BoundsInt(); }
  34. set
  35. {
  36. if (selection != null && selection.m_Position != value)
  37. {
  38. RegisterUndo();
  39. selection.m_Position = value;
  40. if (gridSelectionChanged != null)
  41. gridSelectionChanged();
  42. }
  43. }
  44. }
  45. /// <summary>The GameObject of the GridLayout where the active GridSelection was made.</summary>
  46. public static GameObject target { get { return selection != null ? selection.m_Target : null; } }
  47. /// <summary>The Grid of the target of the active GridSelection.</summary>
  48. public static Grid grid { get { return selection != null && selection.m_Target != null ? selection.m_Target.GetComponentInParent<Grid>() : null; } }
  49. /// <summary>Creates a new GridSelection and sets it as the active GridSelection.</summary>
  50. /// <param name="target">The target GameObject for the GridSelection.</param>
  51. /// <param name="bounds">The cell coordinates of selection made.</param>
  52. public static void Select(Object target, BoundsInt bounds)
  53. {
  54. GridSelection newSelection = CreateInstance<GridSelection>();
  55. newSelection.m_PreviousSelection = Selection.activeObject;
  56. newSelection.m_Target = target as GameObject;
  57. newSelection.m_Position = bounds;
  58. newSelection.m_OriginalPalette = null;
  59. Selection.activeObject = newSelection;
  60. if (gridSelectionChanged != null)
  61. gridSelectionChanged();
  62. }
  63. /// <summary>Clears the active GridSelection.</summary>
  64. public static void Clear()
  65. {
  66. if (active)
  67. {
  68. RegisterUndo();
  69. selection.m_Position = new BoundsInt();
  70. if (selection.m_Scene.IsValid())
  71. {
  72. DestroyImmediate(selection.m_Target);
  73. selection.m_Target = null;
  74. selection.m_OriginalPalette = null;
  75. EditorSceneManager.ClosePreviewScene(selection.m_Scene);
  76. }
  77. Selection.activeObject = selection.m_PreviousSelection;
  78. if (gridSelectionChanged != null)
  79. gridSelectionChanged();
  80. }
  81. }
  82. internal static void SaveStandalone()
  83. {
  84. if (!selection.m_Scene.IsValid()
  85. || selection.m_OriginalPalette == null
  86. || selection.m_Target == null)
  87. return;
  88. TilePaletteSaveUtility.SaveTilePalette(selection.m_OriginalPalette, selection.m_Target.transform.root.gameObject);
  89. }
  90. internal static void TransferToStandalone(GameObject palette)
  91. {
  92. if (!active)
  93. return;
  94. if (!selection.m_Scene.IsValid())
  95. {
  96. selection.m_Scene = EditorSceneManager.NewPreviewScene();
  97. if (!selection.m_Scene.IsValid())
  98. throw new InvalidOperationException("Preview scene could not be created");
  99. }
  100. SceneManager.MoveGameObjectToScene(selection.m_Target.transform.root.gameObject, selection.m_Scene);
  101. selection.m_OriginalPalette = palette;
  102. }
  103. internal static void RegisterUndo()
  104. {
  105. if (selection != null)
  106. Undo.RegisterCompleteObjectUndo(selection, kUpdateGridSelection);
  107. }
  108. private void OnDisable()
  109. {
  110. Clear();
  111. }
  112. }
  113. }