public override void OnEnable() { base.OnEnable(); m_brush = (RandomBrush)target; if (m_brush.Tileset != null) { m_brush.Tileset.OnTileSelected += OnTileSelected; m_brush.Tileset.OnBrushSelected += OnBrushSelected; } m_randTileList = new ReorderableList(serializedObject, serializedObject.FindProperty("RandomTileList"), true, true, true, true); m_randTileList.drawHeaderCallback += (Rect rect) => { EditorGUI.LabelField(rect, "Random Tiles", EditorStyles.boldLabel); }; m_randTileList.drawElementCallback += (Rect rect, int index, bool isActive, bool isFocused) => { Rect rTile = rect; rTile.width = rTile.height = m_brush.Tileset.VisualTileSize.y; uint tileData = m_brush.RandomTileList[index].tileData; int tileId = (int)(tileData & Tileset.k_TileDataMask_TileId); int brushId = Tileset.GetBrushIdFromTileData(tileData); TilesetBrush brush = m_brush.Tileset.FindBrush(brushId); if (brush) { GUI.Box(new Rect(rTile.position - Vector2.one, rTile.size + 2 * Vector2.one), ""); TilesetEditor.DoGUIDrawTileFromTileData(rTile, tileData, m_brush.Tileset, brush.GetAnimUV()); } else if (tileId != Tileset.k_TileId_Empty) { GUI.Box(new Rect(rTile.position - Vector2.one, rTile.size + 2 * Vector2.one), ""); TilesetEditor.DoGUIDrawTileFromTileData(rTile, tileData, m_brush.Tileset); } Rect rTileId = rect; rTileId.x += rTile.width + 10; rTileId.width -= rTile.width + 20; rTileId.height = rect.height / 2; if (brush) { GUI.Label(rTileId, "Brush Id(" + brushId + ")"); } else { GUI.Label(rTileId, "Id(" + tileId + ")"); } SerializedProperty randomTileDataProperty = m_randTileList.serializedProperty.GetArrayElementAtIndex(index); SerializedProperty probabilityFactorProperty = randomTileDataProperty.FindPropertyRelative("probabilityFactor"); Rect rProbabilityField = new Rect(rect.x + rTile.width + 10f, rect.y + EditorGUIUtility.singleLineHeight * 2.5f, rect.width - rTile.width - 10f, EditorGUIUtility.singleLineHeight); Rect rProbabilityLabel = new Rect(rProbabilityField.x, rProbabilityField.y - EditorGUIUtility.singleLineHeight, rProbabilityField.width, rProbabilityField.height); float sumProbabilityFactor = m_brush.GetSumProbabilityFactor(); float probability = sumProbabilityFactor >= 0 ? probabilityFactorProperty.floatValue * 100f / sumProbabilityFactor : 100f; EditorGUI.PrefixLabel(rProbabilityLabel, new GUIContent("Probability (" + Mathf.RoundToInt(probability) + "%)")); EditorGUI.PropertyField(rProbabilityField, probabilityFactorProperty, GUIContent.none); if (probabilityFactorProperty.floatValue == 0f) { serializedObject.ApplyModifiedProperties(); sumProbabilityFactor = m_brush.GetSumProbabilityFactor(); if (sumProbabilityFactor <= 0f) { probabilityFactorProperty.floatValue = 0.01f; } } if (GUI.Button(new Rect(rect.x + rect.width - 50f, rect.y, 50f, EditorGUIUtility.singleLineHeight), "Clear")) { m_brush.RandomTileList[index].tileData = Tileset.k_TileData_Empty; } }; m_randTileList.onSelectCallback += (ReorderableList list) => { TileSelectionWindow.Show(m_brush.Tileset); TileSelectionWindow.Instance.Ping(); }; m_randTileList.onAddCallback += (ReorderableList list) => { if (list.index >= 0) { list.serializedProperty.InsertArrayElementAtIndex(list.index); } else { list.serializedProperty.InsertArrayElementAtIndex(0); } list.index = Mathf.Max(0, list.index + 1); list.serializedProperty.serializedObject.ApplyModifiedProperties(); m_brush.RandomTileList[list.index].probabilityFactor = 1f; if (m_brush.Tileset.SelectedTile != null) { m_randTileList.GrabKeyboardFocus(); OnTileSelected(m_brush.Tileset, -1, m_brush.Tileset.SelectedTileId); } }; }
private void DisplayAutotiling() { if (Tileset.SelectedBrushId != Tileset.k_BrushId_Default) { TilesetBrush brush = Tileset.FindBrush(Tileset.SelectedBrushId); if (brush) { EditorGUILayout.BeginHorizontal(); GUILayoutUtility.GetRect(1, 1, GUILayout.Width(Tileset.VisualTileSize.x), GUILayout.Height(Tileset.VisualTileSize.y)); TilesetEditor.DoGUIDrawTileFromTileData(GUILayoutUtility.GetLastRect(), brush.GetAnimTileData(), Tileset, brush.GetAnimUV()); GUILayout.Label("(" + brush.name + ":" + brush.GetType().Name + ")", EditorStyles.boldLabel); EditorGUILayout.EndHorizontal(); if (!m_brushEditor || m_brushEditor.target != brush) { m_brushEditor = TilesetBrushEditor.CreateEditor(brush) as TilesetBrushEditor; } (m_brushEditor as TilesetBrushEditor).DoInspectorGUI(); } } else { bool isMultiselection = Tileset.TileSelection != null; Tile selectedTile = isMultiselection ? Tileset.Tiles[(int)(Tileset.TileSelection.selectionData[0] & Tileset.k_TileDataMask_TileId)] : Tileset.SelectedTile; GUILayoutUtility.GetRect(1, 1, GUILayout.Width(Tileset.VisualTileSize.x), GUILayout.Height(Tileset.VisualTileSize.y)); Rect tileUV = selectedTile.uv; GUI.color = Tileset.BackgroundColor; GUI.DrawTextureWithTexCoords(GUILayoutUtility.GetLastRect(), EditorGUIUtility.whiteTexture, tileUV, true); GUI.color = Color.white; GUI.DrawTextureWithTexCoords(GUILayoutUtility.GetLastRect(), Tileset.AtlasTexture, tileUV, true); if (isMultiselection) { EditorGUILayout.LabelField("* Multi-selection Edition", EditorStyles.boldLabel); } EditorGUI.BeginChangeCheck(); selectedTile.autilingGroup = TilesetEditor.DoGroupFieldLayout(Tileset, "Group", selectedTile.autilingGroup); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(Tileset, "Tile Autotiling Data Changed"); if (isMultiselection) { for (int i = 0; i < Tileset.TileSelection.selectionData.Count; ++i) { Tile tile = Tileset.Tiles[(int)(Tileset.TileSelection.selectionData[i] & Tileset.k_TileDataMask_TileId)]; tile.autilingGroup = selectedTile.autilingGroup; } } EditorUtility.SetDirty(Tileset); } } }