Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.IO; //This namespace contains classes such as 'StreamReader' and 'StreamWriter'.
- public class TextFile :MonoBehaviour{
- private void Start(){
- //The '@' is used before a string so that the escape sequence operator '\' gets ignored.
- string textPath = @"C:\Users\SwordSkill2\Desktop\Temp\Unity\TestFolder\PlayerInfo\ExampleText.txt";
- StreamReader textReading = null;
- if(!File.Exists(textPath)){
- StreamWriter textWriting = new StreamWriter(textPath);
- textWriting.WriteLine ("Hello World!");
- textWriting.WriteLine ("This is another line.");
- textWriting.WriteLine ("This is yet another line.");
- textWriting.WriteLine ("Ok, I think I should stop now...");
- textWriting.Flush ();
- textWriting.Close ();
- }
- if(File.Exists (textPath))
- textReading = new StreamReader (textPath);
- //Prints the whole text in one string.
- //Debug.Log (textReading.ReadToEnd ());
- //Splits the text to multiple strings for each new line.
- string[] lines = textReading.ReadToEnd ().Split ('\n');
- //Prints each line. (Including the Empty Lines.)
- for(ushort i=0; i < lines.Length; i++)
- Debug.Log (lines[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement