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