void OnGUI_TilesetAtlas(bool isFirstUpdate) { GUILayout.Label("Tileset Atlas Configuration", EditorStyles.boldLabel); GUILayout.BeginHorizontal(); GUI.backgroundColor = m_atlasEditMode == eAtlasEditMode.AddAutoTileset? Color.gray : Color.white; if (GUILayout.Button("Add AutoTileset", GUILayout.Height(25))) { m_atlasEditMode = eAtlasEditMode.AddAutoTileset; } GUI.backgroundColor = m_atlasEditMode == eAtlasEditMode.AddNormalTileset ? Color.gray : Color.white; if (GUILayout.Button("Add NormalTileset", GUILayout.Height(25))) { m_atlasEditMode = eAtlasEditMode.AddNormalTileset; } GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUI.backgroundColor = m_atlasEditMode == eAtlasEditMode.EditTileset ? Color.gray : Color.white; if (GUILayout.Button("Edit Tileset", GUILayout.Height(25))) { m_atlasEditMode = eAtlasEditMode.EditTileset; } GUI.backgroundColor = m_atlasEditMode == eAtlasEditMode.RemoveTileset ? Color.gray : Color.white; if (GUILayout.Button("Remove Tileset", GUILayout.Height(25))) { m_atlasEditMode = eAtlasEditMode.RemoveTileset; } GUI.backgroundColor = Color.white; GUILayout.EndHorizontal(); int cols = m_autoTileset.AtlasTexture.width / m_autoTileset.TilesetSlotSize; //int rows = m_autoTileset.AtlasTexture.height / k_TilesetSlotSize; Rect rAtlas = new Rect(0, 0, m_autoTileset.AtlasTexture.width, m_autoTileset.AtlasTexture.height); if (rAtlas.width - position.width > rAtlas.height - (position.height - 120f)) { rAtlas.width = Mathf.Min(position.width, rAtlas.width); rAtlas.height *= rAtlas.width / m_autoTileset.AtlasTexture.width; } else { rAtlas.height = Mathf.Min((position.height - 120f), rAtlas.height); rAtlas.width *= rAtlas.height / m_autoTileset.AtlasTexture.height; } GUILayout.Box(m_autoTileset.AtlasTexture, GUILayout.Width(rAtlas.width), GUILayout.Height(rAtlas.height)); Rect rGuiAtlas = GUILayoutUtility.GetLastRect(); Vector2 vBtnOff = new Vector2(0.2f * rGuiAtlas.width / cols, 0.40f * rGuiAtlas.width / cols); Rect rButton = new Rect(0, 0, rGuiAtlas.width / cols - 2f * vBtnOff.x, rGuiAtlas.width / cols - 2f * vBtnOff.y); float scale = rAtlas.width / m_autoTileset.AtlasTexture.width; GUIStyle btnStyle = new GUIStyle("button"); btnStyle.richText = true; btnStyle.fontSize = (int)(50 * scale); btnStyle.fontStyle = FontStyle.Bold; for (int i = 0; i < m_autoTileset.AtlasSlots.Count; ++i) { AtlasSlot atlasSlot = m_autoTileset.AtlasSlots[i]; Rect rSlot = m_autoTileset.CalculateAtlasSlotRectByIdx(i); rButton.position = vBtnOff + rGuiAtlas.position + rSlot.position * scale; rButton.y = rGuiAtlas.y + rGuiAtlas.height - (rButton.y - rGuiAtlas.y) - rButton.height; if (atlasSlot.SubTilesets.Count == 0) { GUI.color = new Color(0f, 0.7f, 1f, 0.8f); Rect rSlotBox = rSlot; rSlotBox.position *= scale; rSlotBox.position += rGuiAtlas.position; rSlotBox.width *= scale; rSlotBox.height *= scale; rSlotBox.y = rGuiAtlas.y + rGuiAtlas.height - (rSlotBox.y - rGuiAtlas.y) - rSlotBox.height; GUI.Box(rSlotBox, "\n\nSlot " + (i + 1)); } GUI.color = new Color(0f, 0.7f, 1f, 0.8f); //+++ Show name list if (m_atlasEditMode == eAtlasEditMode.None) { if (atlasSlot.SubTilesets.Count == 1) { List <string> availableNames = m_autoTileset.CreateAvailableNameList(); availableNames.Insert(0, atlasSlot.SubTilesets[0].Name); Rect rPopup = new Rect(rButton.x, rButton.y, 50, 20); rPopup.position -= vBtnOff; int nameIdx = EditorGUI.Popup(rPopup, 0, availableNames.ToArray(), new GUIStyle("popup")); atlasSlot.SubTilesets[0].Name = availableNames[nameIdx]; } else { for (int j = 0; j < atlasSlot.SubTilesets.Count; ++j) { List <string> availableNames = m_autoTileset.CreateAvailableNameList(); availableNames.Insert(0, atlasSlot.SubTilesets[j].Name); Rect rPopup = new Rect(rButton.x, rButton.y, 50, 20); rPopup.position -= vBtnOff; if (j == 0) { rPopup.y += rSlot.height * scale / 2; } else if (j == 2) { rPopup.y += rSlot.height * scale / 2; rPopup.x += rSlot.width * scale / 2; } else if (j == 3) { rPopup.x += rSlot.width * scale / 2; } int nameIdx = EditorGUI.Popup(rPopup, 0, availableNames.ToArray(), new GUIStyle("popup")); atlasSlot.SubTilesets[j].Name = availableNames[nameIdx]; } } } //--- GUI.color = new Color(.5f, .5f, 0.5f, 0.7f); if (m_atlasEditMode == eAtlasEditMode.EditTileset && atlasSlot.SubTilesets.Count >= 1) { if (GUI.Button(rButton, "<color=white>" + (atlasSlot.SubTilesets.Count > 1 ? "[+]" : "[A]") + "Edit</color>", btnStyle)) { m_editAtlasSlot = atlasSlot; m_editMode = eEditMode.ChangeTileset; m_clearSlotOnCancel = false; m_atlasEditMode = eAtlasEditMode.None; } } else if (m_atlasEditMode == eAtlasEditMode.RemoveTileset && atlasSlot.SubTilesets.Count >= 1) { if (GUI.Button(rButton, "<color=white>" + (atlasSlot.SubTilesets.Count > 1 ? "[+]" : "[A]") + "Remove</color>", btnStyle)) { foreach (SubTilesetConf conf in atlasSlot.SubTilesets) { UtilsAutoTileMap.ClearAtlasArea(m_autoTileset.AtlasTexture, (int)conf.AtlasRec.x, (int)conf.AtlasRec.y, (int)conf.AtlasRec.width, (int)conf.AtlasRec.height); } atlasSlot.SubTilesets.Clear(); m_autoTileset.BuildSubTilesetsList(); EditorUtility.SetDirty(m_autoTileset); if (m_autoTileset.SubTilesets.Count == 0) { m_atlasEditMode = eAtlasEditMode.None; } } } else if (m_atlasEditMode == eAtlasEditMode.AddAutoTileset && atlasSlot.SubTilesets.Count == 0) { if (GUI.Button(rButton, "<color=white>Add AutoTileset</color>", btnStyle)) { List <string> availableNames = m_autoTileset.CreateAvailableNameList(); atlasSlot.SubTilesets.Add(new SubTilesetConf() { Name = availableNames.Count > 0 ? availableNames[0] : "-", AtlasRec = rSlot, HasAutotiles = true }); m_autoTileset.BuildSubTilesetsList(); m_editAtlasSlot = atlasSlot; m_editMode = eEditMode.ChangeTileset; m_clearSlotOnCancel = true; EditorUtility.SetDirty(m_autoTileset); } } else if (m_atlasEditMode == eAtlasEditMode.AddNormalTileset && atlasSlot.SubTilesets.Count == 0) { if (GUI.Button(rButton, "<color=white>Add Normal Tileset</color>", btnStyle)) { List <string> availableNames = m_autoTileset.CreateAvailableNameList(); Rect rect = new Rect(rSlot.x, rSlot.y, rSlot.width / 2, rSlot.height / 2); atlasSlot.SubTilesets.Add(new SubTilesetConf() { Name = availableNames.Count > 0 ? availableNames[0] : "-", AtlasRec = rect, HasAutotiles = false }); rect.y = rSlot.y + rect.height; atlasSlot.SubTilesets.Add(new SubTilesetConf() { Name = availableNames.Count > 1 ? availableNames[1] : "-", AtlasRec = rect, HasAutotiles = false }); rect.x = rSlot.x + rect.width; rect.y = rSlot.y; atlasSlot.SubTilesets.Add(new SubTilesetConf() { Name = availableNames.Count > 2 ? availableNames[2] : "-", AtlasRec = rect, HasAutotiles = false }); rect.y = rSlot.y + rect.height; atlasSlot.SubTilesets.Add(new SubTilesetConf() { Name = availableNames.Count > 3 ? availableNames[3] : "-", AtlasRec = rect, HasAutotiles = false }); m_autoTileset.BuildSubTilesetsList(); m_editAtlasSlot = atlasSlot; m_editMode = eEditMode.ChangeTileset; m_clearSlotOnCancel = true; EditorUtility.SetDirty(m_autoTileset); } } } GUI.color = Color.white; if (m_atlasEditMode != eAtlasEditMode.None && GUILayout.Button("Cancel Action", GUILayout.Height(25))) { m_atlasEditMode = eAtlasEditMode.None; } }
void OnGUI_TilesetAtlas(bool isFirstUpdate) { GUILayout.Label("Tileset Atlas Configuration", EditorStyles.boldLabel); GUILayout.BeginHorizontal(); GUI.backgroundColor = m_atlasEditMode == eAtlasEditMode.AddAutoTileset? Color.gray : Color.white; if (GUILayout.Button("Add AutoTileset", GUILayout.Height(25))) m_atlasEditMode = eAtlasEditMode.AddAutoTileset; GUI.backgroundColor = m_atlasEditMode == eAtlasEditMode.AddNormalTileset ? Color.gray : Color.white; if (GUILayout.Button("Add NormalTileset", GUILayout.Height(25))) m_atlasEditMode = eAtlasEditMode.AddNormalTileset; GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUI.backgroundColor = m_atlasEditMode == eAtlasEditMode.EditTileset ? Color.gray : Color.white; if (GUILayout.Button("Edit Tileset", GUILayout.Height(25))) m_atlasEditMode = eAtlasEditMode.EditTileset; GUI.backgroundColor = m_atlasEditMode == eAtlasEditMode.RemoveTileset ? Color.gray : Color.white; if (GUILayout.Button("Remove Tileset", GUILayout.Height(25))) m_atlasEditMode = eAtlasEditMode.RemoveTileset; GUI.backgroundColor = Color.white; GUILayout.EndHorizontal(); int cols = m_autoTileset.AtlasTexture.width / m_autoTileset.TilesetSlotSize; //int rows = m_autoTileset.AtlasTexture.height / k_TilesetSlotSize; Rect rAtlas = new Rect(0, 0, m_autoTileset.AtlasTexture.width, m_autoTileset.AtlasTexture.height); if (rAtlas.width - position.width > rAtlas.height - (position.height - 120f)) { rAtlas.width = Mathf.Min(position.width, rAtlas.width); rAtlas.height *= rAtlas.width / m_autoTileset.AtlasTexture.width; } else { rAtlas.height = Mathf.Min((position.height - 120f), rAtlas.height); rAtlas.width *= rAtlas.height / m_autoTileset.AtlasTexture.height; } GUILayout.Box(m_autoTileset.AtlasTexture, GUILayout.Width(rAtlas.width), GUILayout.Height(rAtlas.height)); Rect rGuiAtlas = GUILayoutUtility.GetLastRect(); Vector2 vBtnOff = new Vector2(0.2f * rGuiAtlas.width / cols, 0.40f * rGuiAtlas.width / cols); Rect rButton = new Rect(0, 0, rGuiAtlas.width / cols - 2f * vBtnOff.x, rGuiAtlas.width / cols - 2f * vBtnOff.y); float scale = rAtlas.width / m_autoTileset.AtlasTexture.width; GUIStyle btnStyle = new GUIStyle("button"); btnStyle.richText = true; btnStyle.fontSize = (int)(50*scale); btnStyle.fontStyle = FontStyle.Bold; for (int i = 0; i < m_autoTileset.AtlasSlots.Count; ++i ) { AtlasSlot atlasSlot = m_autoTileset.AtlasSlots[i]; Rect rSlot = m_autoTileset.CalculateAtlasSlotRectByIdx(i); rButton.position = vBtnOff + rGuiAtlas.position + rSlot.position * scale; rButton.y = rGuiAtlas.y + rGuiAtlas.height - (rButton.y - rGuiAtlas.y) - rButton.height; if (atlasSlot.SubTilesets.Count == 0) { GUI.color = new Color(0f, 0.7f, 1f, 0.8f); Rect rSlotBox = rSlot; rSlotBox.position *= scale; rSlotBox.position += rGuiAtlas.position; rSlotBox.width *= scale; rSlotBox.height *= scale; rSlotBox.y = rGuiAtlas.y + rGuiAtlas.height - (rSlotBox.y - rGuiAtlas.y) - rSlotBox.height; GUI.Box(rSlotBox, "\n\nSlot " + (i + 1)); } GUI.color = new Color(0f, 0.7f, 1f, 0.8f); //+++ Show name list if (m_atlasEditMode == eAtlasEditMode.None) { if (atlasSlot.SubTilesets.Count == 1) { List<string> availableNames = m_autoTileset.CreateAvailableNameList(); availableNames.Insert(0, atlasSlot.SubTilesets[0].Name); Rect rPopup = new Rect(rButton.x, rButton.y, 50, 20); rPopup.position -= vBtnOff; int nameIdx = EditorGUI.Popup(rPopup, 0, availableNames.ToArray(), new GUIStyle("popup")); atlasSlot.SubTilesets[0].Name = availableNames[nameIdx]; } else { for (int j = 0; j < atlasSlot.SubTilesets.Count; ++j) { List<string> availableNames = m_autoTileset.CreateAvailableNameList(); availableNames.Insert(0, atlasSlot.SubTilesets[j].Name); Rect rPopup = new Rect(rButton.x, rButton.y, 50, 20); rPopup.position -= vBtnOff; if (j == 0) rPopup.y += rSlot.height * scale / 2; else if (j == 2) { rPopup.y += rSlot.height * scale / 2; rPopup.x += rSlot.width * scale / 2; } else if (j == 3) { rPopup.x += rSlot.width * scale / 2; } int nameIdx = EditorGUI.Popup(rPopup, 0, availableNames.ToArray(), new GUIStyle("popup")); atlasSlot.SubTilesets[j].Name = availableNames[nameIdx]; } } } //--- GUI.color = new Color(.5f, .5f, 0.5f, 0.7f); if (m_atlasEditMode == eAtlasEditMode.EditTileset && atlasSlot.SubTilesets.Count >= 1) { if (GUI.Button(rButton, "<color=white>" + (atlasSlot.SubTilesets.Count > 1 ? "[+]" : "[A]") + "Edit</color>", btnStyle)) { m_editAtlasSlot = atlasSlot; m_editMode = eEditMode.ChangeTileset; m_clearSlotOnCancel = false; m_atlasEditMode = eAtlasEditMode.None; } } else if (m_atlasEditMode == eAtlasEditMode.RemoveTileset && atlasSlot.SubTilesets.Count >= 1) { if (GUI.Button(rButton, "<color=white>" + (atlasSlot.SubTilesets.Count > 1 ? "[+]" : "[A]") + "Remove</color>", btnStyle)) { foreach( SubTilesetConf conf in atlasSlot.SubTilesets ) { UtilsAutoTileMap.ClearAtlasArea(m_autoTileset.AtlasTexture, (int)conf.AtlasRec.x, (int)conf.AtlasRec.y, (int)conf.AtlasRec.width, (int)conf.AtlasRec.height); } atlasSlot.SubTilesets.Clear(); m_autoTileset.BuildSubTilesetsList(); EditorUtility.SetDirty(m_autoTileset); if (m_autoTileset.SubTilesets.Count == 0) m_atlasEditMode = eAtlasEditMode.None; } } else if (m_atlasEditMode == eAtlasEditMode.AddAutoTileset && atlasSlot.SubTilesets.Count == 0) { if (GUI.Button(rButton, "<color=white>Add AutoTileset</color>", btnStyle)) { List<string> availableNames = m_autoTileset.CreateAvailableNameList(); atlasSlot.SubTilesets.Add(new SubTilesetConf() { Name = availableNames.Count > 0 ? availableNames[0] : "-", AtlasRec = rSlot, HasAutotiles = true }); m_autoTileset.BuildSubTilesetsList(); m_editAtlasSlot = atlasSlot; m_editMode = eEditMode.ChangeTileset; m_clearSlotOnCancel = true; EditorUtility.SetDirty(m_autoTileset); } } else if (m_atlasEditMode == eAtlasEditMode.AddNormalTileset && atlasSlot.SubTilesets.Count == 0) { if (GUI.Button(rButton, "<color=white>Add Normal Tileset</color>", btnStyle)) { List<string> availableNames = m_autoTileset.CreateAvailableNameList(); Rect rect = new Rect(rSlot.x, rSlot.y, rSlot.width / 2, rSlot.height / 2); atlasSlot.SubTilesets.Add(new SubTilesetConf() { Name = availableNames.Count > 0 ? availableNames[0] : "-", AtlasRec = rect, HasAutotiles = false }); rect.y = rSlot.y + rect.height; atlasSlot.SubTilesets.Add(new SubTilesetConf() { Name = availableNames.Count > 1 ? availableNames[1] : "-", AtlasRec = rect, HasAutotiles = false }); rect.x = rSlot.x + rect.width; rect.y = rSlot.y; atlasSlot.SubTilesets.Add(new SubTilesetConf() { Name = availableNames.Count > 2 ? availableNames[2] : "-", AtlasRec = rect, HasAutotiles = false }); rect.y = rSlot.y + rect.height; atlasSlot.SubTilesets.Add(new SubTilesetConf() { Name = availableNames.Count > 3 ? availableNames[3] : "-", AtlasRec = rect, HasAutotiles = false }); m_autoTileset.BuildSubTilesetsList(); m_editAtlasSlot = atlasSlot; m_editMode = eEditMode.ChangeTileset; m_clearSlotOnCancel = true; EditorUtility.SetDirty(m_autoTileset); } } } GUI.color = Color.white; if ( m_atlasEditMode != eAtlasEditMode.None && GUILayout.Button("Cancel Action", GUILayout.Height(25))) m_atlasEditMode = eAtlasEditMode.None; }