private void OnDestroy() { if (Instance == this) { Instance = null; } }
public static void Initialize(List <GridObjectBrush> m_Brushes) { if (Instance != null) { return; } Instance = (AddGridObjectBrushPopup)EditorWindow.GetWindowWithRect(typeof(AddGridObjectBrushPopup), new Rect(0, 0, 400, 180)); GUIContent titleContent = new GUIContent(_windowName); Instance.titleContent = titleContent; Instance.m_Brushes = m_Brushes; Instance.ShowUtility(); Instance.Repaint(); }
public override void OnPaintInspectorGUI() { if (m_GridObjectBrush.pickedObject) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Currently Picked Objects (Pick Tool)", EditorStyles.boldLabel); if (GUILayout.Button("Unpick Objects")) { m_GridObjectBrush.ResetPick(); return; } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(7.5f); int rowLength = 1; int maxRowLength = m_GridObjectBrush.size.x; var previewSize = Mathf.Min(((Screen.width - 35) / maxRowLength), 100); _scrollViewScrollPosition = EditorGUILayout.BeginScrollView(_scrollViewScrollPosition, false, false); if (maxRowLength < 1) { maxRowLength = 1; } foreach (TilePaletteGridObjectBrush.BrushCell tileBrush in m_GridObjectBrush.cells) { //check if row is longer than max row length if (rowLength > maxRowLength) { rowLength = 1; EditorGUILayout.EndHorizontal(); } //begin row if rowLength == 1 if (rowLength == 1) { EditorGUILayout.BeginHorizontal(); } GUIContent btnContent = tileBrush != null && tileBrush.gridObject != null ? new GUIContent(AssetPreview.GetAssetPreview(tileBrush.gridObject.gameObject), tileBrush.gridObject.gameObject.name) : new GUIContent("", "There is no tile at this position."); if (GUILayout.Button(btnContent, GUILayout.Width(previewSize), GUILayout.Height(previewSize))) { } rowLength++; } //check if row is longer than max row length if (rowLength > maxRowLength) { rowLength = 1; EditorGUILayout.EndHorizontal(); } if (rowLength == 1) { EditorGUILayout.BeginHorizontal(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndScrollView(); } else { // If there is no tile picked show the collections GUI #region GridTile Collection SerializedObject serializedObject_brushObject = null; int prevSelectedGridTileCollectionIndex = m_SelectedGridObjectCollectionIndex; if (m_Collection != null) { serializedObject_brushObject = new SerializedObject(m_Collection); } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Active GridTile Collection:"); GridObjectCollection.GridObjectCollectionList gridObjectCollectionList = GridObjectCollection.GetGridObjectCollectionsInProject(); m_SelectedGridObjectCollectionIndex = EditorGUILayout.Popup(m_SelectedGridObjectCollectionIndex, gridObjectCollectionList.GetNameList()); if (prevSelectedGridTileCollectionIndex != m_SelectedGridObjectCollectionIndex || m_Collection == null) //select only when brush collection changed or is null { m_Collection = gridObjectCollectionList.brushCollections[m_SelectedGridObjectCollectionIndex]; m_Collection.SetLastUsedGridObjectCollection(); m_GridObjectBrush.ClearCellFromEditor(); var tileBrush = m_Collection.m_SelectedGridObjectBrush; if (tileBrush != null) { m_GridObjectBrush.SetCellFromEditor(Vector3Int.zero, tileBrush.m_GridObject, tileBrush.m_InitialOrientation); } } if (GUILayout.Button("+")) { Debug.Log("Create a new collection"); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(7.5f); int rowLength = 1; int maxRowLength = Mathf.FloorToInt((Screen.width - 15) / 100); int columns = Mathf.CeilToInt((m_Collection.m_GridObjectBrushes.Count / maxRowLength)) * 3; _scrollViewScrollPosition = EditorGUILayout.BeginScrollView(_scrollViewScrollPosition, false, false); if (maxRowLength < 1) { maxRowLength = 1; } foreach (GridObjectBrush tileBrush in m_Collection.m_GridObjectBrushes) { //check if brushObject is null, if so skip this brush if (tileBrush == null || tileBrush.m_GridObject == null) { continue; } //check if row is longer than max row length if (rowLength > maxRowLength) { rowLength = 1; EditorGUILayout.EndHorizontal(); } //begin row if rowLength == 1 if (rowLength == 1) { EditorGUILayout.BeginHorizontal(); } //change color Color guiColor = GUI.backgroundColor; if (m_Collection.m_SelectedGridObjectBrush != null && m_Collection.m_SelectedGridObjectBrush.m_GridObject != null && m_Collection.m_SelectedGridObjectBrush.m_GridObject == tileBrush.m_GridObject) { GUI.backgroundColor = PrimarySelectedColor; if (m_GridObjectBrush.editorCell != null && m_GridObjectBrush.editorCell.gridObject != tileBrush.m_GridObject) { m_GridObjectBrush.SetCellFromEditor(Vector3Int.zero, tileBrush.m_GridObject, tileBrush.m_InitialOrientation); } } //Create the brush entry in the scroll view and check if the user clicked on the created button (change the currently selected/edited brush accordingly and add it to the current brushes if possible) GUIContent btnContent = new GUIContent(AssetPreview.GetAssetPreview(tileBrush.m_GridObject.gameObject), tileBrush.m_GridObject.gameObject.name); if (GUILayout.Button(btnContent, GUILayout.Width(100), GUILayout.Height(100))) { //select the currently edited brush and deselect all selected brushes if (m_Collection.m_SelectedGridObjectBrush != tileBrush) { m_Collection.m_SelectedGridObjectBrushIndex = m_Collection.m_GridObjectBrushes.IndexOf(tileBrush); m_GridObjectBrush.SetCellFromEditor(Vector3Int.zero, tileBrush.m_GridObject, tileBrush.m_InitialOrientation); } else { //m_Collection.m_SelectedGridTileBrush = null; m_Collection.m_SelectedGridObjectBrushIndex = -1; m_GridObjectBrush.ClearCellFromEditor(); } } GUI.backgroundColor = guiColor; rowLength++; } //check if row is longer than max row length if (rowLength > maxRowLength) { rowLength = 1; EditorGUILayout.EndHorizontal(); } if (rowLength == 1) { EditorGUILayout.BeginHorizontal(); } //add button if (GUILayout.Button(new GUIContent("+", "Add a GridObject to the collection."), GUILayout.Width(100), GUILayout.Height(100))) { AddGridObjectBrushPopup.Initialize(m_Collection.m_GridObjectBrushes); } Color guiBGColor = GUI.backgroundColor; EditorGUILayout.EndHorizontal(); EditorGUILayout.EndScrollView(); EditorGUILayout.BeginHorizontal(); GUI.backgroundColor = green; if (GUILayout.Button(new GUIContent("Add GridObject", "Add a GridObject to the collection."))) { AddGridObjectBrushPopup.Initialize(m_Collection.m_GridObjectBrushes); } EditorGUI.BeginDisabledGroup(m_Collection.m_SelectedGridObjectBrush == null || m_Collection.m_SelectedGridObjectBrush.m_GridObject == null); GUI.backgroundColor = red; //remove selected brushes button if (GUILayout.Button(new GUIContent("Remove Selected GridObject", "Removes the selected gridobject from the collection."))) { if (m_Collection.m_SelectedGridObjectBrush != null) { m_Collection.RemoveObject(m_Collection.m_SelectedGridObjectBrush); m_Collection.m_SelectedGridObjectBrushIndex = -1; m_Collection.Save(); } } EditorGUI.EndDisabledGroup(); //remove all brushes button EditorGUI.BeginDisabledGroup(m_Collection.m_GridObjectBrushes.Count == 0); if (GUILayout.Button(new GUIContent("Remove All GridObjects", "Removes all tiles from the collection.")) && RemoveAllBrushes_Dialog(m_Collection.m_GridObjectBrushes.Count)) { m_Collection.RemoveAllObjects(); m_Collection.Save(); } EditorGUI.EndDisabledGroup(); EditorGUILayout.EndHorizontal(); GUI.backgroundColor = guiBGColor; if (m_Collection.m_GridObjectBrushes != null && m_Collection.m_GridObjectBrushes.Count > 0 && m_Collection.m_SelectedGridObjectBrush != null && m_Collection.m_SelectedGridObjectBrush.m_GridObject != null) { EditorGUILayout.Space(10f); EditorGUILayout.BeginHorizontal(); EditorGUI.BeginChangeCheck(); EditorGUILayout.LabelField("GridObject Settings:" + " (" + m_Collection.m_SelectedGridObjectBrush.m_GridObject.gameObject.name + ")", EditorStyles.boldLabel); if (GUILayout.Button(new GUIContent("Reset Settings", "Restores the settings for the current GridObject."), GUILayout.MaxWidth(120))) { m_Collection.m_SelectedGridObjectBrush.ResetParameters(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(5f); EditorGUILayout.BeginHorizontal(); m_Collection.m_SelectedGridObjectBrush.m_InitialOrientation = (Orientations)EditorGUILayout.EnumPopup(new GUIContent("Initial Orientation", "Changes the initial orientation of the gridobject."), m_Collection.m_SelectedGridObjectBrush.m_InitialOrientation); EditorGUILayout.EndHorizontal(); if (EditorGUI.EndChangeCheck()) { // Update the cell's settings m_GridObjectBrush.SetCellFromEditor(Vector3Int.zero, m_Collection.m_SelectedGridObjectBrush.m_GridObject, m_Collection.m_SelectedGridObjectBrush.m_InitialOrientation); } } EditorGUILayout.Space(10f); /* * if (GUI.changed && m_Collection != null) * { * m_Collection.Save(); * } */ #endregion } }