Advertisement
SwordSkill

Text Files Unity C#

Nov 28th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO; //This namespace contains classes such as 'StreamReader' and 'StreamWriter'.
  4.  
  5. public class TextFile :MonoBehaviour{
  6.     private void Start(){
  7.         //The '@' is used before a string so that the escape sequence operator '\' gets ignored.
  8.         string textPath = @"C:\Users\SwordSkill2\Desktop\Temp\Unity\TestFolder\PlayerInfo\ExampleText.txt";
  9.         StreamReader textReading = null;
  10.  
  11.         if(!File.Exists(textPath)){
  12.             StreamWriter textWriting = new StreamWriter(textPath);
  13.             textWriting.WriteLine ("Hello World!");
  14.             textWriting.WriteLine ("This is another line.");
  15.             textWriting.WriteLine ("This is yet another line.");
  16.             textWriting.WriteLine ("Ok, I think I should stop now...");
  17.             textWriting.Flush ();
  18.             textWriting.Close ();
  19.         }
  20.         if(File.Exists (textPath))
  21.             textReading = new StreamReader (textPath);
  22.  
  23.         //Prints the whole text in one string.
  24.         //Debug.Log (textReading.ReadToEnd ());
  25.  
  26.         //Splits the text to multiple strings for each new line.
  27.         string[] lines = textReading.ReadToEnd ().Split ('\n');
  28.  
  29.         //Prints each line. (Including the Empty Lines.)
  30.         for(ushort i=0; i < lines.Length; i++)
  31.             Debug.Log (lines[i]);
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement