説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

GridSelection.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. [SerializeField]
  21. private GameObject m_Target;
  22. [SerializeField]
  23. private Object m_PreviousSelection;
  24. [SerializeField]
  25. private Scene m_Scene;
  26. [SerializeField]
  27. private GameObject m_OriginalPalette;
  28. /// <summary>Whether there is an active GridSelection made on a GridLayout.</summary>
  29. public static bool active { get { return Selection.activeObject is GridSelection && selection.m_Target != null; } }
  30. private static GridSelection selection { get { return Selection.activeObject as GridSelection; } }
  31. /// <summary>The cell coordinates of the active GridSelection made on the GridLayout.</summary>
  32. public static BoundsInt position
  33. {
  34. get { return selection != null ? selection.m_Position : new BoundsInt(); }
  35. set
  36. {
  37. if (selection != null && selection.m_Position != value)
  38. {
  39. RegisterUndo();
  40. selection.m_Position = value;
  41. if (gridSelectionChanged != null)
  42. gridSelectionChanged();
  43. }
  44. }
  45. }
  46. /// <summary>The GameObject of the GridLayout where the active GridSelection was made.</summary>
  47. public static GameObject target { get { return selection != null ? selection.m_Target : null; } }
  48. /// <summary>The Grid of the target of the active GridSelection.</summary>
  49. public static Grid grid { get { return selection != null && selection.m_Target != null ? selection.m_Target.GetComponentInParent<Grid>() : null; } }
  50. /// <summary>Creates a new GridSelection and sets it as the active GridSelection.</summary>
  51. /// <param name="target">The target GameObject for the GridSelection.</param>
  52. /// <param name="bounds">The cell coordinates of selection made.</param>
  53. public static void Select(Object target, BoundsInt bounds)
  54. {
  55. var newSelection = CreateInstance<GridSelection>();
  56. newSelection.m_PreviousSelection = Selection.activeObject;
  57. newSelection.m_Target = target as GameObject;
  58. newSelection.m_Position = bounds;
  59. newSelection.m_OriginalPalette = null;
  60. Undo.RegisterCreatedObjectUndo(newSelection, kUpdateGridSelection);
  61. var currentGroup = Undo.GetCurrentGroup();
  62. Selection.activeObject = newSelection;
  63. Undo.CollapseUndoOperations(currentGroup);
  64. if (gridSelectionChanged != null)
  65. gridSelectionChanged();
  66. }
  67. /// <summary>Clears the active GridSelection.</summary>
  68. public static void Clear()
  69. {
  70. if (active)
  71. {
  72. RegisterUndo();
  73. selection.m_Position = new BoundsInt();
  74. if (selection.m_Scene.IsValid())
  75. {
  76. DestroyImmediate(selection.m_Target);
  77. selection.m_Target = null;
  78. selection.m_OriginalPalette = null;
  79. EditorSceneManager.ClosePreviewScene(selection.m_Scene);
  80. }
  81. Selection.activeObject = selection.m_PreviousSelection;
  82. if (gridSelectionChanged != null)
  83. gridSelectionChanged();
  84. }
  85. }
  86. internal static void SaveStandalone()
  87. {
  88. if (!selection.m_Scene.IsValid()
  89. || selection.m_OriginalPalette == null
  90. || selection.m_Target == null)
  91. return;
  92. TilePaletteSaveUtility.SaveTilePalette(selection.m_OriginalPalette, selection.m_Target.transform.root.gameObject);
  93. }
  94. internal static void TransferToStandalone(GameObject palette)
  95. {
  96. if (!active)
  97. return;
  98. if (!selection.m_Scene.IsValid())
  99. {
  100. selection.m_Scene = EditorSceneManager.NewPreviewScene();
  101. if (!selection.m_Scene.IsValid())
  102. throw new InvalidOperationException("Preview scene could not be created");
  103. }
  104. SceneManager.MoveGameObjectToScene(selection.m_Target.transform.root.gameObject, selection.m_Scene);
  105. selection.m_OriginalPalette = palette;
  106. }
  107. internal static void RegisterUndo()
  108. {
  109. if (selection != null)
  110. Undo.RegisterCompleteObjectUndo(selection, kUpdateGridSelection);
  111. }
  112. private void OnDisable()
  113. {
  114. Clear();
  115. }
  116. }
  117. }