void GenerateRoomFromFile(string path) { Room room = Instantiate <GameObject>(RoomPrefab).GetComponent <Room>(); room.transform.parent = this.transform; BaseLevelObject Level = UnityEditor.AssetDatabase.LoadMainAssetAtPath(path) as BaseLevelObject; char[] currentLevelData = Level.GetLevelData(); int rowCount = Level.numRows; RoomStartPos = new Vector2(0, CellHeight * rowCount); room.RoomPosition = new Vector2(RoomStartPos.x, RoomStartPos.y); for (int i = 0; i < currentLevelData.Length; i++) { int x = i / rowCount; int y = i % rowCount; char currentChar = currentLevelData[i]; if (TileMappings.ContainsKey(currentChar)) { room.SetRoomSprite(x, y, TileMappings[currentChar]); } } GameObject player = Instantiate <GameObject>(PlayerPrefab); player.transform.position = new Vector3(CellWidth * 2, CellHeight * 2, 0); }
private void LoadLevelData(string filename) { selectedX = -1; selectedY = -1; if (filename == string.Empty) { return; } string assetPath = "Assets/Resources/ScriptableLevels/" + filename + ".asset"; selectedLevel = AssetDatabase.LoadMainAssetAtPath(assetPath) as BaseLevelObject; }
private void OnGUI() { GUILayout.BeginVertical(); GUILayout.Label("Level To Edit"); source = EditorGUILayout.ObjectField(source, typeof(BaseLevelObject), true) as BaseLevelObject; GUILayout.Space(20); if (GUILayout.Button("Load Level Data", GUILayout.Height(50), GUILayout.Width(150))) { Debug.Log(source.name); LoadLevelData(source.name); } GUILayout.Space(50); if (selectedLevel == null) { GUILayout.EndVertical(); return; } GUILayout.Label("Level Editing"); if (selectedLevel.LevelData == null) { return; } if (selectedLevel.numRows > 0 && selectedLevel.numColumns > 0) { for (int i = 0; i < selectedLevel.numRows; i++) { GUILayout.BeginHorizontal(); for (int j = 0; j < selectedLevel.numColumns; j++) { if (GUILayout.Button(selectedLevel.LevelData[(j * selectedLevel.numColumns) + i].ToString(), GUILayout.Width(25.0f), GUILayout.Height(25.0f))) { selectedX = i; selectedY = j; } } GUILayout.EndHorizontal(); } } GUILayout.Space(20); GUILayout.Label("Selected Tile Value"); tileScrollPos = EditorGUILayout.BeginScrollView(tileScrollPos, false, false, GUILayout.Height(200.0f)); if (GUILayout.Button("Wall", GUILayout.Height(25.0f), GUILayout.Width(100.0f))) { //selectedLevel.LevelData.SetValue('#', (selectedY * selectedLevel.numColumns) + selectedX); selectedLevel.LevelData[(selectedY * selectedLevel.numColumns) + selectedX] = '#'; } else if (GUILayout.Button("Floor", GUILayout.Height(25.0f), GUILayout.Width(100.0f))) { //selectedLevel.LevelData.SetValue(' ', (selectedY * selectedLevel.numColumns) + selectedX); selectedLevel.LevelData[(selectedY * selectedLevel.numColumns) + selectedX] = ' '; } else if (GUILayout.Button("Hole", GUILayout.Height(25.0f), GUILayout.Width(100.0f))) { //selectedLevel.LevelData.SetValue('x', (selectedY * selectedLevel.numColumns) + selectedX); selectedLevel.LevelData[(selectedY * selectedLevel.numColumns) + selectedX] = 'x'; } EditorGUILayout.EndScrollView(); GUILayout.Space(20); if (GUILayout.Button("Save Level Data", GUILayout.Height(50), GUILayout.Width(150), GUILayout.Height(100))) { SaveLevelData(); } GUILayout.EndVertical(); }