- Home /
How should I make this type of text level editor? C#
example:
level1.txt level2.txt level3.txt
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,P,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,~~~,,
,,,,,,,,,,
,,,,,,,,,,
~~~~~~~~~~
private Tile LoadTile(char tileType, int x, int y)
{
switch (tileType)
{
// Blank space
case ',':
//return new Tile(null, TileCollision.Passable);
// Don't need to do anything.
return null;
// Exit
case 'X':
case 'Y':
case 'Z':
string levelCode = this.mappings[tileType.ToString()];
this.LoadExit(x, y, tileType, levelCode);
return null;
// Gem
case 'G':
if (this.LevelType == Game.LevelType.ForestLevel)
{
this.LoadGem(x, y, GemType.GoldNugget);
}
else
{
this.LoadGem(x, y, GemType.Normal);
}
return null;
// Powerup gem
case 'P':
this.LoadGem(x, y, GemType.PowerUp);
return null;
// Poison gem
case 'Q':
this.LoadGem(x, y, GemType.Ablaze);
return null;
// Floating platform
case '-':
return this.LoadTile(TileType.Platform, x, y);
case 'U':
this.LoadPipe(x, y);
return null;
// Various enemies
case 'A':
this.LoadEnemy(x, y, EnemyType.MonsterA, 1f);
return null;
case 'B':
this.LoadEnemy(x, y, EnemyType.MonsterB, 1f);
return null;
case 'C':
this.LoadEnemy(x, y, EnemyType.MonsterC, 1f);
return null;
case 'D':
this.LoadEnemy(x, y, EnemyType.MonsterD, 1f);
return null;
// Mini enemies.
case 'a':
this.LoadEnemy(x, y, EnemyType.MonsterA, 0.5f);
return null;
case 'b':
this.LoadEnemy(x, y, EnemyType.MonsterB, 0.5f);
return null;
case 'c':
this.LoadEnemy(x, y, EnemyType.MonsterC, 0.5f);
return null;
case 'd':
this.LoadEnemy(x, y, EnemyType.MonsterD, 0.5f);
return null;
case 'K':
this.LoadBoss(x, y, BossSprite.BossType.KingBaddie);
return null;
case 'L':
this.LoadBoss(x, y, BossSprite.BossType.GhostBaddie);
return null;
case 'M':
this.LoadBoss(x, y, BossSprite.BossType.DeerBoss);
return null;
// Platform block
case '~':
return this.LoadTile(TileType.BlockB, x, y); //("BlockB", 2, TileCollision.Platform);
//// Passable block
//case ':':
// return this.LoadTile(TileType.BlockB, x, y); //LoadVarietyTile("BlockB", 2, TileCollision.Passable);
// Player 1 start point
case '1':
this.LoadStart(x, y, PlayerType.Esko, this.mainPlayerIndex);
return null;
case '2':
// Only add Glaci if the second player has already entered the game.
if (GameplayScreen.IsPlayerTwoActive)
{
this.LoadStart(x, y, PlayerType.Glaci, GameplayScreen.PlayerTwoControllerIndex.Value);
}
else
{
this.glaciePosition = new Vector2(x, y);
}
this.isMoreFreezeEverything = true;
return null;
case '3':
this.LoadStart(x, y, PlayerType.Fernie, this.mainPlayerIndex);
return null;
// Impassable block
case '#':
return this.LoadTile(TileType.BlockA, x, y); //LoadVarietyTile("BlockA", 7, TileCollision.Impassable);
// Unknown tile type character
default:
throw new NotSupportedException(string.Format("Unsupported tile type character '{0}' at position {1}, {2}.", tileType, x, y));
}
}
I’m more hung up on the getting of the .txt file, getting the strings into a switch, and then next level transition than the actual x and y and prefabs and other parts of the level build just making note.
this is why i don't even want to bother asking any questions ^
If you would rephrase your post to include a relevant and specific question, the community would be happy to help you solve your problem. Unfortunately, without a question, one cannot possibly hope to get a solution.
Answer by thelackey3326 · Dec 03, 2014 at 03:55 AM
You'll want to use TextAsset, which works like any other asset. You could assign it to a field on a script or load it from Resources (which is popular because it can be unloaded when you're done with it).
Use its text property to get the file contents as a string. Iterate over the string passing each character to the LoadTile() method. For each character passed, x += 1. If the character is a newline, x = 0, y += 1.
{
List<string> lines = new List<string>();
lines = GetTextFromFile("Level1");
}
private List <string> GetTextFromFile (string fileName) {
// Extract file text into array of strings
TextAsset txt = (TextAsset) Resources.Load ("Level Files/" + fileName, typeof (TextAsset));
string levelFile = txt.text;
List<string> lines = new List<string>();
lines = levelFile.Split(new char[] { '\n', '\r' }).ToList();
lines.Reverse ();
return lines;
}
I Wanted Other People To Be Able To Use This As An Example.
Your answer
![](https://koobas.hobune.stream/wayback/20220613173024im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
can use resource.load to load levels(json file)? 1 Answer
Level managers and editor 0 Answers