// --------------------------------------------------------------------------- // Load preferences from state scriptable object // --------------------------------------------------------------------------- public static void Restore_Palette_Items() { // Get or scriptable object to store the interface state data Get_Reference_To_Scriptable_Object(); // Get palette item count int paletteItemCount = state.prefabs.Length; // Load palette item tooltips string[] paletteItemTooltip = state.paletteItemTooltip; // Create empty Texture2D array and empty GUIContent array Texture2D[] paletteTexture2D = new Texture2D[paletteItemCount]; GUIContent[] paletteGuiContent = new GUIContent[paletteItemCount]; // Get texture path to save palette images string texturePath = MAST_Asset_Loader.GetMASTRootFolder() + "/Etc/Temp/temp_palette_image_"; // Loop through each palette item for (int i = 0; i < paletteItemCount; i++) { // Load Texture2D saved as PNG on disk back to palette item byte[] bytes = File.ReadAllBytes(texturePath + i.ToString("000") + ".png"); paletteTexture2D[i] = new Texture2D(2, 2); paletteTexture2D[i].LoadImage(bytes); } // Copy palette GUI content to Palette class MAST_Palette.RestorePaletteData(state.prefabs, paletteTexture2D, paletteItemTooltip); }
public static GameObject PlacePrefabInScene() { // Make sure target parent is referenced ReferenceTargetParent(); if (MAST_Placement_Visualizer.visualizerOnGrid && MAST_Placement_Visualizer.GetGameObject() != null && MAST_Placement_Visualizer.GetGameObject() != null) { // If drawing continuous and an object was just drawn here, exit without drawing if (MAST_Placement_Interface.placementMode == MAST_Placement_Interface.PlacementMode.DrawContinuous) { if (lastPosition == MAST_Placement_Visualizer.GetGameObject().transform.position) { return(null); } } // If GameObject already exists here, exit without placing the block if (GameObjectAlreadyHere()) { return(null); } // Instantiate the prefab GameObject newPrefab = (GameObject)PrefabUtility.InstantiatePrefab(MAST_Palette.GetSelectedPrefab()); // Correct GameObject transform and name "to remove (clone)" newPrefab.transform.rotation = MAST_Placement_Visualizer.GetGameObject().transform.rotation; newPrefab.transform.localScale = MAST_Placement_Visualizer.GetGameObject().transform.localScale; newPrefab.transform.position = MAST_Placement_Visualizer.GetGameObject().transform.position; newPrefab.name = MAST_Palette.GetSelectedPrefab().name; // Make new prefab child of the target parent newPrefab.transform.parent = targetParent.transform; // Add MAST script to GameObject if it was missing it if (!newPrefab.GetComponent <MAST_Prefab_Component>()) { newPrefab.AddComponent <MAST_Prefab_Component>(); } // Randomize the seed after placement if (MAST_Settings.gui.toolbar.selectedDrawToolIndex == 3) { MAST_Placement_Randomizer.GenerateNewRandomSeed(); } // Save this GameObject position lastPosition = MAST_Placement_Visualizer.GetGameObject().transform.position; // Make this an Undo point, just after placing the prefab Undo.RegisterCreatedObjectUndo(newPrefab, "Placed new Prefab"); // Return with newly created GameObject return(newPrefab); } return(null); }
// --------------------------------------------------------------------------- #region Palette GUI // --------------------------------------------------------------------------- private void DisplayPaletteGUI() { // Only draw prefab palette if it is ready if (MAST_Palette.IsReady()) { DisplayPaletteGUIPopulated(); } else { DisplayPaletteGUIPlaceholder(); } }
// Change visualizer prefab when a new item is selected in the palette menu public static void ChangeSelectedPrefab() { // Get reference to the MAST script attached to the GameObject MAST_Placement_Helper.mastScript = MAST_Palette.GetSelectedPrefab().GetComponent <MAST_Prefab_Component>(); // Remove any existing visualizer MAST_Placement_Visualizer.RemoveVisualizer(); // Create a new visualizer MAST_Placement_Visualizer.CreateVisualizer(MAST_Palette.GetSelectedPrefab()); }
// --------------------------------------------------------------------------- // Save preferences to state scriptable object // --------------------------------------------------------------------------- public static void Save_Palette_Items(bool forceSave = false) { // Get or create a scriptable object to store the interface state data Get_Reference_To_Scriptable_Object(); // If palette data has changed or if forcing a save "from loading new prefabs with button" if (MAST_Palette.GetPrefabArray() != state.prefabs || forceSave) { // Delete any previous palette items same in the "MAST/Etc/Temp" folder string paletteImagePath = MAST_Asset_Loader.GetMASTRootFolder() + "/Etc/Temp"; if (Directory.Exists(paletteImagePath)) { Directory.Delete(paletteImagePath, true); } Directory.CreateDirectory(paletteImagePath); // Save prefabs state.prefabs = MAST_Palette.GetPrefabArray(); // Define palette item tooltip array string[] paletteItemTooltip = new string[MAST_Palette.GetGUIContentArray().Length]; // Get texture path to save palette images string texturePath = MAST_Asset_Loader.GetMASTRootFolder() + "/Etc/Temp/temp_palette_image_"; // Loop through each item in the palette for (int i = 0; i < MAST_Palette.GetGUIContentArray().Length; i++) { // Get the tooltip from the palette GUIContent paletteItemTooltip[i] = MAST_Palette.GetGUIContentArray()[i].tooltip; // Encode this palette item image to PNG then save to disk byte[] bytes = MAST_Palette.GetTexture2DArray()[i].EncodeToPNG(); File.WriteAllBytes(texturePath + i.ToString("000") + ".png", bytes); } // Save palette item tooltips and images (converted to byte arrays) state.paletteItemTooltip = paletteItemTooltip; } // Save state changes to disk Save_Changes_To_Disk(); }
// ----------------------------------------------------------------------- // Get a random prefab from the same category as the selected prefab // ----------------------------------------------------------------------- private static void GetRandomPrefabInCategory(int categoryID) { // Create a list to hold all viable replacement prefabs List <int> replacementPrefabIndexList = new List <int>(); // Define MAST component script variable outside the foreach loop MAST_Prefab_Component mastScript; // Loop through each prefab in the palette for (int prefabIndex = 0; prefabIndex < MAST_Palette.GetPrefabArray().Length; prefabIndex++) { // Get the MAST component script attached to this prefab mastScript = MAST_Palette.GetPrefabArray()[prefabIndex].GetComponent <MAST_Prefab_Component>(); // Wrap within a try/catch incase a MAST component script isn't attached to a prefab try { // If prefab category ID matches and prefab is replaceable, add it to replacement prefab list if (mastScript.categoryID == categoryID) { if (mastScript.randomizer.replaceable) { replacementPrefabIndexList.Add(prefabIndex); } } } catch { // Do nothing with an error. No MAST script was attached, so no categoryID } } // Get a random number between 0 and the total amount of replacement prefabs - 1 int replacementPrefabIndex = (int)Random.Range(0, replacementPrefabIndexList.Count); // Select the new prefab MAST_Palette.selectedItemIndex = replacementPrefabIndexList[replacementPrefabIndex]; MAST_Placement_Interface.ChangeSelectedPrefab(); }
// --------------------------------------------------------------------------- #region Toolbar GUI // --------------------------------------------------------------------------- private void DisplayToolbarGUI() { // Calculate toolbar icon size toolBarIconSize = (position.height / 15.3f) * MAST_Settings.gui.toolbar.scale; GUILayout.Space(toolBarIconSize / 10); GUILayout.BeginVertical(); GUILayout.Space(toolBarIconSize / 7.5f); // ---------------------------------------------- // Grid Section // ---------------------------------------------- GUILayout.BeginVertical("MAST Toolbar BG Inset"); // ------------------------------------ // Grid Up Button // ------------------------------------ if (GUILayout.Button(new GUIContent(iconGridUp, "Move Grid Up"), "MAST Half Button", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize / 2))) { MAST_Grid_Manager.MoveGridUp(); } // ------------------------------------ // Toggle Grid Button // ------------------------------------ EditorGUI.BeginChangeCheck(); // OnScene Enable/Disable Randomizer Icon Button MAST_Grid_Manager.gridExists = GUILayout.Toggle( MAST_Grid_Manager.gridExists, new GUIContent(iconGridToggle, "Toggle Scene Grid"), "MAST Toggle", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize)); // If randomizer enabled value changed, process the change if (EditorGUI.EndChangeCheck()) { MAST_Grid_Manager.ChangeGridVisibility(); } // ------------------------------------ // Grid Down Button // ------------------------------------ if (GUILayout.Button(new GUIContent(iconGridDown, "Move Grid Down"), "MAST Half Button", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize / 2))) { MAST_Grid_Manager.MoveGridDown(); } GUILayout.EndVertical(); GUILayout.Space(toolBarIconSize / 5); // ---------------------------------------------- // Draw Tool Section // ---------------------------------------------- GUILayout.BeginVertical("MAST Toolbar BG Inset"); // ------------------------------------ // Add Draw Tool Toggle Group // ------------------------------------ EditorGUI.BeginChangeCheck(); // Create drawtools SelectionGrid int newSelectedDrawToolIndex = GUILayout.SelectionGrid( MAST_Settings.gui.toolbar.selectedDrawToolIndex, guiContentDrawTool, 1, "MAST Toggle", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize * 5)); // If the draw tool was changed if (EditorGUI.EndChangeCheck()) { // If the draw tool was clicked again, deselect it if (newSelectedDrawToolIndex == MAST_Settings.gui.toolbar.selectedDrawToolIndex) { MAST_Settings.gui.toolbar.selectedDrawToolIndex = -1; MAST_Placement_Interface.ChangePlacementMode(MAST_Placement_Interface.PlacementMode.None); drawing = false; painting = false; erasing = false; } // If a different draw tool was clicked, change to it else { MAST_Settings.gui.toolbar.selectedDrawToolIndex = newSelectedDrawToolIndex; if (MAST_Settings.gui.toolbar.selectedDrawToolIndex != 1) { drawing = false; } if (MAST_Settings.gui.toolbar.selectedDrawToolIndex != 2) { painting = false; } if (MAST_Settings.gui.toolbar.selectedDrawToolIndex != 4) { erasing = false; } switch (MAST_Settings.gui.toolbar.selectedDrawToolIndex) { // Draw Single Tool selected case 0: MAST_Placement_Interface.ChangePlacementMode(MAST_Placement_Interface.PlacementMode.DrawSingle); break; // Draw Continuous Tool selected case 1: MAST_Placement_Interface.ChangePlacementMode(MAST_Placement_Interface.PlacementMode.DrawContinuous); break; // Flood Fill Tool selected case 2: MAST_Placement_Interface.ChangePlacementMode(MAST_Placement_Interface.PlacementMode.PaintArea); break; // Randomizer Tool selected case 3: MAST_Placement_Interface.ChangePlacementMode(MAST_Placement_Interface.PlacementMode.Randomize); SceneView.RepaintAll(); break; // Eraser Tool selected case 4: MAST_Placement_Interface.ChangePlacementMode(MAST_Placement_Interface.PlacementMode.Erase); SceneView.RepaintAll(); break; } } } GUILayout.EndVertical(); GUILayout.Space(toolBarIconSize / 5); // ---------------------------------------------- // Manipulate Section // ---------------------------------------------- GUILayout.BeginVertical("MAST Toolbar BG Inset"); // ------------------------------------ // Rotate Button // ------------------------------------ if (GUILayout.Button(new GUIContent(iconRotate, "Rotate Prefab/Selection"), "MAST Button", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize))) { MAST_Placement_Manipulate.RotateObject(); } // OnScene Change Rotate Axis Icon Button switch (MAST_Placement_Manipulate.GetCurrentRotateAxis()) { case MAST_Placement_Manipulate.Axis.X: if (GUILayout.Button(new GUIContent(iconAxisX, "Change Rotate Axis"), "MAST Half Button", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize / 2))) { MAST_Placement_Manipulate.ToggleRotateAxis(); } break; case MAST_Placement_Manipulate.Axis.Y: if (GUILayout.Button(new GUIContent(iconAxisY, "Change Rotate Axis"), "MAST Half Button", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize / 2))) { MAST_Placement_Manipulate.ToggleRotateAxis(); } break; case MAST_Placement_Manipulate.Axis.Z: if (GUILayout.Button(new GUIContent(iconAxisZ, "Change Rotate Axis"), "MAST Half Button", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize / 2))) { MAST_Placement_Manipulate.ToggleRotateAxis(); } break; } GUILayout.Space(toolBarIconSize / 10); // ------------------------------------ // Flip Button // ------------------------------------ if (GUILayout.Button(new GUIContent(iconFlip, "Flip Prefab/Selection"), "MAST Button", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize))) { MAST_Placement_Manipulate.FlipObject(); } // OnScene Change Flip Axis Icon Button switch (MAST_Placement_Manipulate.GetCurrentFlipAxis()) { case MAST_Placement_Manipulate.Axis.X: if (GUILayout.Button(new GUIContent(iconAxisX, "Change Flip Axis"), "MAST Half Button", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize / 2))) { MAST_Placement_Manipulate.ToggleFlipAxis(); } break; case MAST_Placement_Manipulate.Axis.Y: if (GUILayout.Button(new GUIContent(iconAxisY, "Change Flip Axis"), "MAST Half Button", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize / 2))) { MAST_Placement_Manipulate.ToggleFlipAxis(); } break; case MAST_Placement_Manipulate.Axis.Z: if (GUILayout.Button(new GUIContent(iconAxisZ, "Change Flip Axis"), "MAST Half Button", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize / 2))) { MAST_Placement_Manipulate.ToggleFlipAxis(); } break; } GUILayout.EndVertical(); GUILayout.Space(toolBarIconSize / 5); // ---------------------------------------------- // Misc Section // ---------------------------------------------- GUILayout.BeginVertical("MAST Toolbar BG Inset"); // ------------------------------------ // Load Palette Button // ------------------------------------ if (GUILayout.Button(new GUIContent(iconLoadFromFolder, "Load prefabs from a project folder"), "MAST Button", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize))) { // Process prefabs for palette and save chosen path for future use string chosenPath = MAST_Palette.LoadPrefabs(MAST_Interface_Data_Manager.state.lastPrefabPath); // If a folder was chosen "Cancel was not clicked" if (chosenPath != "") { // Save the path the user chose and all palette items MAST_Interface_Data_Manager.state.lastPrefabPath = chosenPath; MAST_Interface_Data_Manager.Save_Palette_Items(true); MAST_Interface_Data_Manager.Save_Changes_To_Disk(); } } // ------------------------------------ // Open Tools Window Button // ------------------------------------ if (GUILayout.Button(new GUIContent(iconTools, "Tools"), "MAST Button", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize))) { // If Tools window is closed, show and initialize it if (Tools == null) { Tools = (MAST_Tools_Window)EditorWindow.GetWindow( typeof(MAST_Tools_Window), true, "MAST Tools"); Tools.minSize = Tools.maxSize = new Vector2(330, 300); } // If Tools window is open, close it else { EditorWindow.GetWindow(typeof(MAST_Tools_Window)).Close(); } } // ------------------------------------ // Open Settings Window Button // ------------------------------------ if (GUILayout.Button(new GUIContent(iconSettings, "Settings"), "MAST Button", GUILayout.Width(toolBarIconSize), GUILayout.Height(toolBarIconSize))) { // If Settings window is closed, show and initialize it if (Settings == null) { Settings = (MAST_Settings_Window)EditorWindow.GetWindow( typeof(MAST_Settings_Window), true, "MAST Settings"); Settings.minSize = Settings.maxSize = new Vector2(330, 300); Settings.Initialize(); } // If Settings window is open, close it else { EditorWindow.GetWindow(typeof(MAST_Settings_Window)).Close(); } } GUILayout.EndVertical(); GUILayout.EndVertical(); }
// Handle SceneView GUI private void SceneviewGUI(SceneView sceneView) { bool scrollWheelUsed = false; // If SHIFT key is held down if (Event.current.shift) { // If mouse scrollwheel was used if (Event.current.type == EventType.ScrollWheel) { // If scrolling wheel down if (Event.current.delta.y > 0) { // Select next prefab and cycle back to top of prefabs if needed MAST_Palette.selectedItemIndex++; if (MAST_Palette.selectedItemIndex >= MAST_Palette.GetPrefabArray().Length) { MAST_Palette.selectedItemIndex = 0; } scrollWheelUsed = true; } // If scrolling wheel dupown else if (Event.current.delta.y < 0) { // Select previous prefab and cycle back to bottom of prefabs if needed MAST_Palette.selectedItemIndex--; if (MAST_Palette.selectedItemIndex < 0) { MAST_Palette.selectedItemIndex = MAST_Palette.GetPrefabArray().Length - 1; } scrollWheelUsed = true; } } } // If successfully scrolled wheel if (scrollWheelUsed) { // If no draw tool is selected, then select the draw single tool if (MAST_Settings.gui.toolbar.selectedDrawToolIndex == -1) { MAST_Settings.gui.toolbar.selectedDrawToolIndex = 0; MAST_Placement_Interface.ChangePlacementMode(MAST_Placement_Interface.PlacementMode.DrawSingle); } // If erase draw tool isn't selected, change the visualizer prefab if (MAST_Settings.gui.toolbar.selectedDrawToolIndex != 4) { MAST_Placement_Interface.ChangeSelectedPrefab(); } // Repaint all views UnityEditorInternal.InternalEditorUtility.RepaintAllViews(); // Keep mouseclick from selecting other objects GUIUtility.hotControl = GUIUtility.GetControlID(FocusType.Passive); Event.current.Use(); } }
// --------------------------------------------------------------------------- // MAST Window is Enabled // --------------------------------------------------------------------------- private void OnEnable() { //Debug.Log("Interface - On Enable"); // Initialize Preference Manager MAST_Settings.Initialize(); // Initialize the MAST State Manager MAST_Interface_Data_Manager.Initialize(); // Set up deletegates so that OnScene is called automatically #if UNITY_2019_1_OR_NEWER SceneView.duringSceneGui -= this.OnScene; SceneView.duringSceneGui += this.OnScene; #else SceneView.onSceneGUIDelegate -= this.OnScene; SceneView.onSceneGUIDelegate += this.OnScene; #endif // Set up deletegates for exiting editor mode and returning to editor mode from play mode MAST_PlayModeStateListener.onExitEditMode -= this.ExitEditMode; MAST_PlayModeStateListener.onExitEditMode += this.ExitEditMode; MAST_PlayModeStateListener.onEnterEditMode -= this.EnterEditMode; MAST_PlayModeStateListener.onEnterEditMode += this.EnterEditMode; // Set scene to be updated by mousemovement wantsMouseMove = true; // If Enabled in Editor Mode if (!inPlayMode) { // Load icon textures if (iconGridToggle == null) { LoadImages(); } // Load custom gui styles if (guiSkin == null) { guiSkin = MAST_Asset_Loader.GetGUISkin(); } // Load interface data back from saved state MAST_Interface_Data_Manager.Load_Interface_State(); // Create a new grid if needed if (MAST_Interface_Data_Manager.state.gridExists) { MAST_Grid_Manager.gridExists = true; MAST_Grid_Manager.ChangeGridVisibility(); } // Change placement mode back to what was saved MAST_Placement_Interface.ChangePlacementMode( (MAST_Placement_Interface.PlacementMode)MAST_Settings.gui.toolbar.selectedDrawToolIndex); // TODO: Reselect item in palette //MAST_Palette.selectedItemIndex = newSelectedPaletteItemIndex; // If palette images were lost due to serialization, load the palette from saved state if (MAST_Palette.ArePaletteImagesLost()) { MAST_Interface_Data_Manager.Restore_Palette_Items(); } } // If Enabled in Run Mode else { // Nothing so far, because everything is being triggered in ExitEditMode event method } }
private void DisplayPaletteGUIPopulated() { GUILayout.BeginVertical("MAST Toolbar BG"); // Begin toolbar vertical layout GUILayout.BeginHorizontal(); // --------------------------------------------- // Calculate Palette SelectionGrid size // --------------------------------------------- // Verical scroll view for palette items scrollPos = EditorGUILayout.BeginScrollView(scrollPos); // Get scrollview width and height of scrollview if is resized float scrollViewWidth = EditorGUIUtility.currentViewWidth - scrollBarWidth - toolBarIconSize - 20; int rowCount = Mathf.CeilToInt(MAST_Palette.GetGUIContentArray().Length / (float)MAST_Interface_Data_Manager.state.columnCount); float scrollViewHeight = rowCount * ((scrollViewWidth) / MAST_Interface_Data_Manager.state.columnCount); // --------------------------------------------- // Get palette background image // --------------------------------------------- string paletteGUISkin = null; switch (MAST_Settings.gui.palette.bgColor) { case MAST_GUI_ScriptableObject.PaleteBGColor.Dark: paletteGUISkin = "MAST Palette Item Dark"; break; case MAST_GUI_ScriptableObject.PaleteBGColor.Gray: paletteGUISkin = "MAST Palette Item Gray"; break; case MAST_GUI_ScriptableObject.PaleteBGColor.Light: paletteGUISkin = "MAST Palette Item Light"; break; } EditorGUI.BeginChangeCheck(); // --------------------------------------------- // Draw Palette SelectionGrid // --------------------------------------------- int newSelectedPaletteItemIndex = GUILayout.SelectionGrid( MAST_Palette.selectedItemIndex, MAST_Palette.GetGUIContentArray(), MAST_Interface_Data_Manager.state.columnCount, paletteGUISkin, GUILayout.Width((float)scrollViewWidth), GUILayout.Height(scrollViewHeight) ); // If changes to UI value ocurred, update the grid if (EditorGUI.EndChangeCheck()) { // If palette item was deselected by being clicked again if (newSelectedPaletteItemIndex == MAST_Palette.selectedItemIndex) { MAST_Palette.selectedItemIndex = -1; // If erase draw tool isn't selected, remove the draw tool and visualizer if (MAST_Settings.gui.toolbar.selectedDrawToolIndex != 4) { MAST_Settings.gui.toolbar.selectedDrawToolIndex = -1; MAST_Placement_Interface.ChangePlacementMode(MAST_Placement_Interface.PlacementMode.None); } } // If palette item selection has changed else { MAST_Palette.selectedItemIndex = newSelectedPaletteItemIndex; // If no draw tool is selected, then select the draw single tool if (MAST_Settings.gui.toolbar.selectedDrawToolIndex == -1) { MAST_Settings.gui.toolbar.selectedDrawToolIndex = 0; MAST_Placement_Interface.ChangePlacementMode(MAST_Placement_Interface.PlacementMode.DrawSingle); } // If erase draw tool isn't selected, change the visualizer prefab if (MAST_Settings.gui.toolbar.selectedDrawToolIndex != 4) { MAST_Placement_Interface.ChangeSelectedPrefab(); } } } EditorGUILayout.EndScrollView(); GUILayout.EndHorizontal(); // Palette Column Count Slider MAST_Interface_Data_Manager.state.columnCount = (int)GUILayout.HorizontalSlider(MAST_Interface_Data_Manager.state.columnCount, 1, 10); GUILayout.Space(toolBarIconSize / 10); GUILayout.EndVertical(); }