public static int GetLastUsedGridTileCollectionIndex() { //try to find the last used brush collection and return it if (EditorPrefs.HasKey(lastGridTileCollectionEditPrefsKey)) { string guid = EditorPrefs.GetString(lastGridTileCollectionEditPrefsKey, ""); string path = AssetDatabase.GUIDToAssetPath(guid); GridTileCollection lastUsedCollection = AssetDatabase.LoadAssetAtPath <GridTileCollection>(path); return(lastUsedCollection.GetIndex()); } else { return(0); } }
void OnEnable() { Instance = this; Undo.undoRedoPerformed += ClearLastPreview; m_SelectedGridTileCollectionIndex = GridTileCollection.GetLastUsedGridTileCollectionIndex(); }
public override void OnPaintInspectorGUI() { if (m_GridTileBrush.pickedTile) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Currently Picked Tiles (Pick Tool)", EditorStyles.boldLabel); if (GUILayout.Button("Unpick Tiles")) { m_GridTileBrush.ResetPick(); return; } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(7.5f); int rowLength = 1; int maxRowLength = m_GridTileBrush.size.x; var previewSize = Mathf.Min(((Screen.width - 35) / maxRowLength), 100); _scrollViewScrollPosition = EditorGUILayout.BeginScrollView(_scrollViewScrollPosition, false, false); if (maxRowLength < 1) { maxRowLength = 1; } foreach (TilePaletteGridTileBrush.BrushCell tileBrush in m_GridTileBrush.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.gridTile != null ? new GUIContent(AssetPreview.GetAssetPreview(tileBrush.gridTile.gameObject), tileBrush.gridTile.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_SelectedGridTileCollectionIndex; if (m_Collection != null) { serializedObject_brushObject = new SerializedObject(m_Collection); } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Active GridTile Collection:"); GridTileCollection.GridTileCollectionList gridTileCollectionList = GridTileCollection.GetBrushCollectionsInProject(); m_SelectedGridTileCollectionIndex = EditorGUILayout.Popup(m_SelectedGridTileCollectionIndex, gridTileCollectionList.GetNameList()); if (prevSelectedGridTileCollectionIndex != m_SelectedGridTileCollectionIndex || m_Collection == null) //select only when brush collection changed or is null { m_Collection = gridTileCollectionList.brushCollections[m_SelectedGridTileCollectionIndex]; m_Collection.SetLastUsedGridTileCollection(); m_GridTileBrush.ClearCellFromEditor(); var tileBrush = m_Collection.m_SelectedGridTileBrush; if (tileBrush != null) { m_GridTileBrush.SetCellFromEditor(Vector3Int.zero, tileBrush.m_GridTile, tileBrush.m_Height, tileBrush.m_Offset, Quaternion.Euler(tileBrush.m_Rotation)); } } 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_GridTileBrushes.Count / maxRowLength)) * 3; _scrollViewScrollPosition = EditorGUILayout.BeginScrollView(_scrollViewScrollPosition, false, false); if (maxRowLength < 1) { maxRowLength = 1; } foreach (GridTileBrush tileBrush in m_Collection.m_GridTileBrushes) { //check if brushObject is null, if so skip this brush if (tileBrush == null || tileBrush.m_GridTile == 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_SelectedGridTileBrush != null && m_Collection.m_SelectedGridTileBrush.m_GridTile != null && m_Collection.m_SelectedGridTileBrush.m_GridTile == tileBrush.m_GridTile) { GUI.backgroundColor = PrimarySelectedColor; if (m_GridTileBrush.editorCell != null && m_GridTileBrush.editorCell.gridTile != tileBrush.m_GridTile) { m_GridTileBrush.SetCellFromEditor(Vector3Int.zero, tileBrush.m_GridTile, tileBrush.m_Height, tileBrush.m_Offset, Quaternion.Euler(tileBrush.m_Rotation)); } } //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_GridTile.gameObject), tileBrush.m_GridTile.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_SelectedGridTileBrush != tileBrush) { m_Collection.m_SelectedGridTileBrushIndex = m_Collection.m_GridTileBrushes.IndexOf(tileBrush); m_GridTileBrush.SetCellFromEditor(Vector3Int.zero, tileBrush.m_GridTile, tileBrush.m_Height, tileBrush.m_Offset, Quaternion.Euler(tileBrush.m_Rotation)); } else { m_Collection.m_SelectedGridTileBrushIndex = -1; m_GridTileBrush.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 GridTile to the collection."), GUILayout.Width(100), GUILayout.Height(100))) { AddGridTileBrushPopup.Initialize(m_Collection.m_GridTileBrushes); } Color guiBGColor = GUI.backgroundColor; EditorGUILayout.EndHorizontal(); EditorGUILayout.EndScrollView(); EditorGUILayout.BeginHorizontal(); GUI.backgroundColor = green; if (GUILayout.Button(new GUIContent("Add GridTile", "Add a GridTile to the collection."))) { AddGridTileBrushPopup.Initialize(m_Collection.m_GridTileBrushes); } EditorGUI.BeginDisabledGroup(m_Collection.m_SelectedGridTileBrush == null || m_Collection.m_SelectedGridTileBrush.m_GridTile == null); GUI.backgroundColor = red; //remove selected brushes button if (GUILayout.Button(new GUIContent("Remove Selected Tile", "Removes the selected gridtile from the collection."))) { if (m_Collection.m_SelectedGridTileBrush != null) { m_Collection.RemoveTile(m_Collection.m_SelectedGridTileBrush); m_Collection.m_SelectedGridTileBrushIndex = -1; m_Collection.Save(); } } EditorGUI.EndDisabledGroup(); //remove all brushes button EditorGUI.BeginDisabledGroup(m_Collection.m_GridTileBrushes.Count == 0); if (GUILayout.Button(new GUIContent("Remove All Tiles", "Removes all tiles from the collection.")) && RemoveAllBrushes_Dialog(m_Collection.m_GridTileBrushes.Count)) { m_Collection.RemoveAllTiles(); m_Collection.Save(); } EditorGUI.EndDisabledGroup(); EditorGUILayout.EndHorizontal(); GUI.backgroundColor = guiBGColor; if (m_Collection.m_GridTileBrushes != null && m_Collection.m_GridTileBrushes.Count > 0 && m_Collection.m_SelectedGridTileBrush != null && m_Collection.m_SelectedGridTileBrush.m_GridTile != null) { EditorGUILayout.Space(10f); EditorGUILayout.BeginHorizontal(); EditorGUI.BeginChangeCheck(); EditorGUILayout.LabelField("Tile Settings:" + " (" + m_Collection.m_SelectedGridTileBrush.m_GridTile.gameObject.name + ")", EditorStyles.boldLabel); if (GUILayout.Button(new GUIContent("Reset Settings", "Restores the settings for the current GridTile."), GUILayout.MaxWidth(120))) { m_Collection.m_SelectedGridTileBrush.ResetParameters(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(5f); EditorGUILayout.BeginHorizontal(); m_Collection.m_SelectedGridTileBrush.m_Height = EditorGUILayout.IntField(new GUIContent("Height in Grid", "Changes the height parameter of the tile."), m_Collection.m_SelectedGridTileBrush.m_Height); if (GUILayout.Button(new GUIContent("Reset Height", "Restores the tile height to 0."), GUILayout.MaxWidth(230))) { m_Collection.m_SelectedGridTileBrush.m_Height = 0; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); m_Collection.m_SelectedGridTileBrush.m_Offset = EditorGUILayout.Vector3Field(new GUIContent("Position Offsets", "Changes the position offset from the Cell center."), m_Collection.m_SelectedGridTileBrush.m_Offset); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); m_Collection.m_SelectedGridTileBrush.m_Rotation = EditorGUILayout.Vector3Field(new GUIContent("Rotation Offset", "Changes the rotation offset from the current orientation."), m_Collection.m_SelectedGridTileBrush.m_Rotation); EditorGUILayout.EndHorizontal(); if (EditorGUI.EndChangeCheck()) { // Update the cell's settings m_GridTileBrush.SetCellFromEditor(Vector3Int.zero, m_Collection.m_SelectedGridTileBrush.m_GridTile, m_Collection.m_SelectedGridTileBrush.m_Height, m_Collection.m_SelectedGridTileBrush.m_Offset, Quaternion.Euler(m_Collection.m_SelectedGridTileBrush.m_Rotation)); } } EditorGUILayout.Space(10f); /* if (GUI.changed && m_Collection != null) { m_Collection.Save(); } */ #endregion } }