using System.Collections.Generic; using UnityEditor; using UnityEngine; using System.Linq; using Unity.VisualScripting; public class Obj_Placement_TOOL : EditorWindow { // A Dictionary storing the original GameObject as a key, and a list of its instances as values. Dictionary> selectedObjects = new Dictionary>(); // MenuItem attribute makes this function available from the Unity menu. [MenuItem("Window/Damarjian/OBJ Placement Tool")] public static void ShowWindow() { // Creates a new window or focuses on the existing one. GetWindow("Gameobject Loader"); } // Method called for rendering and handling GUI events. private void OnGUI() { // Creates a centered bold label in the Editor window. GUILayout.Label("Use the following interface to create your own object loader", EditorStyles.boldLabel); // Button to select objects if (GUILayout.Button("Select Objects")) { SelectObjects(); } // For each GameObject in the dictionary, create a button. foreach (KeyValuePair> entry in selectedObjects) { // If the GameObject is not null and the button is clicked, create a copy of the object. if (entry.Key != null && GUILayout.Button(entry.Key.name)) { // Instantiate a copy of the GameObject at the origin of the scene. GameObject instantiatedObject = Instantiate(entry.Key, Vector3.zero, Quaternion.identity); // Add the newly created instance to the corresponding list in the dictionary. entry.Value.Add(instantiatedObject); } } // Create a flexible space that pushes the following layout elements to the bottom GUILayout.FlexibleSpace(); // Start a horizontal group to place Save, Load, and Clear buttons next to each other GUILayout.BeginHorizontal(); // Buttons to save, load, and clear the interface. if (GUILayout.Button("Save Interface")) { SaveInterface(); } if (GUILayout.Button("Load Interface")) { LoadInterface(); } if (GUILayout.Button("Clear Interface")) { ClearInterface(); } // End the horizontal group GUILayout.EndHorizontal(); } private void SelectObjects() { // Retrieves all GameObjects currently selected in the scene. GameObject[] selection = Selection.gameObjects; // Iterates through the selected GameObjects. foreach (GameObject obj in selection) { // If the GameObject isn't in the dictionary already, add it with an empty list. if (!selectedObjects.ContainsKey(obj)) { selectedObjects[obj] = new List(); } } } private void SaveInterface() { InterfaceState state = new InterfaceState { // Serialize the names of the original objects in the dictionary to a list. objectNames = selectedObjects.Keys.Select(obj => obj.name).ToList() }; string json = JsonUtility.ToJson(state); string path = "Assets/SavedInterface.json"; System.IO.File.WriteAllText(path, json); AssetDatabase.Refresh(); // Refreshes the asset database to show the new file in the editor } private void LoadInterface() { string path = Application.dataPath + "/SavedInterface.json"; if (System.IO.File.Exists(path)) { string json = System.IO.File.ReadAllText(path); InterfaceState state = JsonUtility.FromJson(json); // Clear the existing interface. selectedObjects.Clear(); // For each name, load the object and add it to the dictionary. foreach (var name in state.objectNames) { GameObject loadedObject = Resources.Load(name); if (loadedObject != null) { selectedObjects[loadedObject] = new List(); } } } else { Debug.LogWarning("Saved interface file not found!"); } } private void ClearInterface() { // Clear the dictionary. selectedObjects.Clear(); } } [System.Serializable] public class InterfaceState { // List of the names or some other identifiers for your original GameObjects. public List objectNames; }