Last active
July 7, 2016 13:36
-
-
Save nothke/74e6f53c76855fe4f27f37c1fa0e6b8f to your computer and use it in GitHub Desktop.
Simple text reader for lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a comment, won't be read by the script, but won't work in the same line as a value | |
// key starts with # everything below is added to the list | |
#PREFIXES | |
Sul | |
Ham | |
Nur | |
Pom | |
//Kul < commented out, won't be read | |
#SUFFIXES | |
iat | |
arm | |
ol | |
onial |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections.Generic; | |
using System.IO; | |
public class SReader | |
{ | |
public static string[] GetLines(string path, string key) | |
{ | |
if (!File.Exists(path)) | |
Debug.LogWarning("SReader: File named " + path + " doesn't exist"); | |
string[] allLines = File.ReadAllLines(path); | |
List<string> lines = new List<string>(); | |
int startInt = -1; | |
for (int i = 0; i < allLines.Length; i++) | |
{ | |
if (allLines[i] == "#" + key) | |
{ | |
startInt = i + 1; | |
break; | |
} | |
} | |
if (startInt == -1) | |
{ | |
Debug.LogWarning("SReader: Key #" + key + " not found"); | |
return null; | |
} | |
for (int j = startInt; j < allLines.Length; j++) | |
{ | |
if (!allLines[j].StartsWith("//") && allLines[j] != "") | |
{ | |
if (allLines[j].StartsWith("#")) | |
break; | |
lines.Add(allLines[j]); | |
} | |
} | |
return lines.ToArray(); | |
} | |
public static string GetRandom(string[] lines) | |
{ | |
if (lines == null) return ""; | |
if (lines.Length == 0) return ""; | |
return lines[Random.Range(0, lines.Length)]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class SReaderExample : MonoBehaviour | |
{ | |
// arrays of values | |
string[] prefixes; | |
string[] suffixes; | |
void Start() | |
{ | |
// Fill arrays | |
prefixes = SReader.GetLines("read_this.txt", "PREFIXES"); | |
suffixes = SReader.GetLines("read_this.txt", "SUFFIXES"); | |
} | |
string GetRandomName() | |
{ | |
return SReader.GetRandom(prefixes) + SReader.GetRandom(suffixes); | |
} | |
void Update() | |
{ | |
// Press space to output a random name | |
if (Input.GetKeyDown(KeyCode.Space)) | |
Debug.Log(GetRandomName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment