public void StartLevel() { m_grid = new DungeonGenerator().Generate(SizeX, SizeY); // Change two random floor tiles to stairs var position = GetRandomTileOfType(TileType.Floor); m_grid[(int)position.x, (int)position.y] = TileType.StairsUp; position = GetRandomTileOfType(TileType.Floor); m_grid[(int)position.x, (int)position.y] = TileType.StairsDown; // Set all tiles for (int y = 0; y < m_grid.SizeY; y++) { for (int x = 0; x < m_grid.SizeX; x++) { var type = m_grid[x, y]; if (type == TileType.Empty) { m_tileMap.PaintTile(x, y, new Color(0, 0, 0, 0)); } else { m_tileMap[x, y] = m_tiles.GetId(type); } } } // Move player to stairs var stairs = FindTile(TileType.StairsUp); var playerBehaviour = GameObject.Find("Player").GetComponent <PlayerBehaviour>(); playerBehaviour.SetTilePosition((int)stairs.x, (int)stairs.y); playerBehaviour.renderer.sortingOrder = 1; TileMeshBehaviour mesh = m_tileMap.GetComponentInChildren <TileMeshBehaviour>(); if (mesh != null) { mesh.renderer.sortingOrder = 0; } }
private void OnSceneGUI() { TileMeshBehaviour mesh = m_tileMap.GetComponentInChildren <TileMeshBehaviour>(); if (mesh != null) { m_tileMap.GetComponentInChildren <TileMeshBehaviour>().transform.localPosition = Vector3.zero; } // TODO the scene gui should only be enabled if m_tileMap.ActiveInEditMode, but I havent found a good way to toggle //if (!m_tileMap.ActiveInEditMode) // return; // TODO ive found the EditorWindow base class which is probably a better approach to show this // http://docs.unity3d.com/ScriptReference/EditorWindow.html m_tilePickerPosition = GUILayout.Window(GUIUtility.GetControlID(FocusType.Passive), m_tilePickerPosition, OnTilePickerWindow, new GUIContent("Select a Tile")); DrawGrid(); if (m_activeInEditMode) { HandleMouseEvents(); } }
public override void OnInspectorGUI() { // base.OnInspectorGUI(); m_showMapSettings = EditorGUILayout.Foldout(m_showMapSettings, "Map Settings"); if (m_showMapSettings) { m_activeInEditMode = EditorGUILayout.Toggle("Active In Edit Mode", m_activeInEditMode); m_tilesX = EditorGUILayout.IntField( new GUIContent("Tiles X", "The number of tiles on the X axis"), m_tilesX); m_tilesY = EditorGUILayout.IntField( new GUIContent("Tiles Y", "The number of tiles on the Y axis"), m_tilesY); m_tileResolution = EditorGUILayout.IntField( new GUIContent("Tile Resolution", "The number of pixels along each axis on one tile"), m_tileResolution); if (m_tileResolution != m_tileMap.MeshSettings.TileResolution) { EditorGUILayout.HelpBox("Changing tile resolution will clear the current tile setup.\n" + string.Format("Current tile resolution is {0}.", m_tileMap.MeshSettings.TileResolution), MessageType.Warning); } m_tileSize = EditorGUILayout.FloatField( new GUIContent("Tile Size", "The size of one tile in Unity units"), m_tileSize); m_meshMode = (MeshMode)EditorGUILayout.EnumPopup("Mesh Mode", m_meshMode); // these settings only apply to the single quad mode mesh if (m_meshMode == MeshMode.SingleQuad) { m_textureFormat = (TextureFormat)EditorGUILayout.EnumPopup("Texture Format", m_textureFormat); m_textureFilterMode = (FilterMode)EditorGUILayout.EnumPopup("Filter Mode", m_textureFilterMode); } if (GUILayout.Button("Create/Recreate Mesh")) { bool canDelete = true; if (m_tileResolution != m_tileMap.MeshSettings.TileResolution) { canDelete = ShowTileDeletionWarning(); } if (canDelete) { m_tileMap.MeshSettings = new TileMeshSettings(m_tilesX, m_tilesY, m_tileResolution, m_tileSize, m_meshMode, m_textureFormat); // if settings didnt change the mesh wouldnt be created, force creation if (!m_tileMap.HasMesh) { m_tileMap.CreateMesh(); } m_activeInEditMode = true; } } if (GUILayout.Button("Destroy Mesh (keep data)")) { m_tileMap.DestroyMesh(); m_activeInEditMode = false; } if (GUILayout.Button("Clear")) { if (ShowTileDeletionWarning()) { m_tileMap.DestroyMesh(); m_tileMap.ClearTiles(); m_activeInEditMode = false; } } if (m_tileMap.ActiveInEditMode != m_activeInEditMode) { m_tileMap.ActiveInEditMode = m_activeInEditMode; OnSceneGUI(); // force redraw map editor box } } m_showSortSettings = EditorGUILayout.Foldout(m_showSortSettings, "Sort Settings"); if (m_showSortSettings) { TileMeshBehaviour mesh = m_tileMap.GetComponentInChildren <TileMeshBehaviour>(); // When first creating tile map or after deleting the mesh, // it will not be accessible until it's created by user. if (mesh != null && mesh.renderer != null) { EditorGUILayout.BeginHorizontal(); EditorGUI.BeginChangeCheck(); string[] sortingLayers = GetSortingLayerNames(); int currentLayer = 0; bool isLayerSet = mesh.renderer.sortingLayerName.Length > 0; if (!isLayerSet) { currentLayer = FindStringIndex(ref sortingLayers, "Default"); } else { currentLayer = FindStringIndex(ref sortingLayers, mesh.renderer.sortingLayerName); } int chosenLayer = EditorGUILayout.Popup("Sorting Layer Name", Mathf.Max(currentLayer, 0), sortingLayers); if (EditorGUI.EndChangeCheck() || !isLayerSet) { mesh.renderer.sortingLayerName = sortingLayers[chosenLayer]; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUI.BeginChangeCheck(); int order = EditorGUILayout.IntField("Sorting Order", mesh.renderer.sortingOrder); if (EditorGUI.EndChangeCheck()) { mesh.renderer.sortingOrder = order; } EditorGUILayout.EndHorizontal(); } else { EditorGUILayout.HelpBox("Mesh has not been created.\nPlease use 'Create/Recreate Mesh' button in the Map Settings.", MessageType.Info, true); } } m_showPickerSettings = EditorGUILayout.Foldout(m_showPickerSettings, "Tile Picker Settings"); if (m_showPickerSettings) { m_tilePickerXCount = Mathf.Clamp(EditorGUILayout.IntField( new GUIContent("Items Per Row", "The number of items to draw in a row in the Tile Picker Window."), m_tilePickerXCount), 1, int.MaxValue); m_tilePickerPosition.width = Mathf.Clamp(EditorGUILayout.FloatField( new GUIContent("Picker Width", "The width of the Tile Picker Window."), m_tilePickerPosition.width), 64f, float.MaxValue); m_tilePickerPosition.height = Mathf.Clamp(EditorGUILayout.FloatField( new GUIContent("Picker Height", "The height of the Tile Picker Window."), m_tilePickerPosition.height), 128f, float.MaxValue); } bool prominentImportArea = m_tileSheet.Ids.Count() == 0; m_showSprites = EditorGUILayout.Foldout(m_showSprites || prominentImportArea, "Sprites"); if (m_showSprites || prominentImportArea) { ShowImportDropArea(); } if (m_showSprites && !prominentImportArea) { if (GUILayout.Button("Delete all")) { foreach (var id in m_tileMap.TileSheet.Ids) { m_tileMap.TileSheet.Remove(id); } m_thumbnailCache.Clear(); } if (GUILayout.Button("Refresh thumbnails")) { m_thumbnailCache.Clear(); } m_sortSpritesByName = GUILayout.Toggle(m_sortSpritesByName, "Sort sprites by name"); var ids = m_tileSheet.Ids.ToList(); ids.Sort(); for (int i = 0; i < ids.Count; i++) { var sprite = m_tileSheet.Get(ids[i]); ShowSprite(sprite); // add separators, except below the last one // could probably find a better looking one if (i < (ids.Count - 1)) { GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1)); } } } EditorUtility.SetDirty(this); }