* 方法1: 使用Application.persistentDataPath
* PC、Android,IOS適用
* 但因為不知道如何將檔案放在此位置,所以此方法需要自行開檔寫資料進去,然後才讀取。
* */
using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System;
public class Text1 : MonoBehaviour
{
//文本中每行的內容.
ArrayList infoall;
//skin資源,這裡用來顯示中文.
public GUISkin skin;
// Use this for initialization
void Start ()
{
//刪除文件.
DeleteFile(Application.persistentDataPath,"FileName.txt");
//創建文件.
CreateFile(Application.persistentDataPath,"FileName.txt","test");
//得到文本中每一行的內容.
infoall = LoadFile(Application.persistentDataPath,"FileName.txt");
}
/**
* path:文件創建目錄.
* name:文件的名稱.
* info:寫入的內容.
*/
void CreateFile(string path,string name,string info)
{
//文件流信息.
StreamWriter sw;
FileInfo t = new FileInfo(path+"//"+ name);
if(!t.Exists)
{
//如果文件不存在則創建.
sw = t.CreateText();
}
else
{
//如果此文件存在則打開.
sw = t.AppendText();
}
//以行的形式寫入信息.
sw.WriteLine(info);
//關閉流.
sw.Close();
//銷毀流.
sw.Dispose();
}
/**
* path:讀取文件的路徑.
* name:讀取文件的名稱.
*/
ArrayList LoadFile(string path,string name)
{
//使用流的形式讀取.
StreamReader sr =null;
try{
sr = File.OpenText(path+"//"+ name);
}catch(Exception e)
{
//路徑與名稱未找到文件則直接回傳null.
return null;
}
string line;
ArrayList arrlist = new ArrayList();
while ((line = sr.ReadLine()) != null)
{
//一行一行的讀取.
//將每一行的內容存入數組鏈表容器中.
arrlist.Add(line);
}
//關閉流.
sr.Close();
//銷毀流.
sr.Dispose();
//將數組鏈表容器返回.
return arrlist;
}
/**
* path:刪除文件的路徑.
* name:刪除文件的名稱.
*/
void DeleteFile(string path,string name)
{
File.Delete(path+"//"+ name);
}
void OnGUI()
{
//用新的skin資源,顯示中文.
GUI.skin = skin;
//讀取文件中的所有內容.
foreach(string str in infoall)
{
//繪製在螢幕上.
GUILayout.Label(str);
}
GUILayout.Label(Application.persistentDataPath);
GUILayout.Label(Application.dataPath);
GUILayout.Label(Application.streamingAssetsPath);
GUILayout.Label(Application.temporaryCachePath);
//PC上的位置.
// C:/Users/[用戶名稱]/AppData/LocalLow/[CompanyName]/ReadWrite
// D:/Unity/ReadWrite/Assets
// D:/Unity/ReadWrite/Assets/StreamingAssets
// C:/Users/[用戶名稱]/AppData/Local/Temp/[CompanyName]/ReadWrite
//Android上的位置.
// /data/data/com.CompanyName.ReadWrite/files
// /data/data/com.CompanyName.ReadWrite-1.apk
// jar:file:///data/app/com.CompanyName.Readrite-1.apk!/assets
// /data/data/com.CompanyName.ReadWrite/cache
}
}
/**
* 方法2: 使用Application.streamingAssetsPath
* PC、Android,IOS適用
* 但是Android平台必須用WWW加載.
* 註:xml檔內的格式不符合此文件規格所以會報錯,沒時間研究XML,所以先這樣囉!!
* */
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
public class Reward
{
public int taskNo;
public Task[] task = new Task[15];
//public Attribute attribute;
public Reward () {}
public struct Task
{
[XmlAttribute("taskReward")]
public string taskReward{ get; set;}
public Id id1;
public Id id2;
public Id id3;
}
public struct Id
{
[XmlAttribute("flag")]
public bool flag{ get; set;}
[XmlAttribute("name")]
public string name{ get; set;}
[XmlText()]
public string description{get;set;}
}
}
public class Text2 : MonoBehaviour {
Reward reward ;
FileInfo fileInfo;
string _data;
// Use this for initialization
void Start ()
{
reward = new Reward();
LoadXML();
}
void LoadXML()
{
if(Application.platform == RuntimePlatform.IPhonePlayer)
{
fileInfo = new FileInfo(Application.dataPath + "/Raw/" + "FileName.xml");
StreamReader r = fileInfo.OpenText();
_data = r.ReadToEnd();
r.Close();
}
else if(Application.platform == RuntimePlatform.Android)
{
fileInfo = new FileInfo(Application.streamingAssetsPath+"/FileName.xml");
StartCoroutine("LoadWWW");
}
else
{
fileInfo = new FileInfo(Application.dataPath + "/StreamingAssets/"+ "FileName.xml");
StreamReader r = fileInfo.OpenText();
_data = r.ReadToEnd();
r.Close();
}
if(_data.ToString() != "")
{
reward = (Reward)DeserializeObject(_data);
}
}
void OnGUI()
{
GUI.Label(new Rect(0,0,Screen.width,Screen.height), _data);
if(Input.GetKey(KeyCode.Space))
{
Application.Quit();
}
}
IEnumerator LoadWWW()
{
//底下兩種路徑方法都可以用.
//方法1:
//string filepath = "jar:file://" + Application.dataPath + "!/assets/FileName.xml";
//WWW www = new WWW(filepath);
//方法2:
WWW www = new WWW(Application.streamingAssetsPath+"/FileName.xml");
yield return www;
_data =www.text;
}
public void Save()
{
_data = SerializeObject(reward);
StreamWriter writer;
fileInfo.Delete();
writer = fileInfo.CreateText();
writer.Write(_data);
writer.Close();
}
string UTF8ByteArrayToString(byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
string constructedString = encoding.GetString(characters);
return (constructedString);
}
byte[] StringToUTF8ByteArray(string pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteArray = encoding.GetBytes(pXmlString);
return byteArray;
}
// Here we serialize our Reward object of reward
string SerializeObject(object pObject)
{
string XmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(Reward));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, pObject);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
return XmlizedString;
}
// Here we deserialize it back into its original form
object DeserializeObject(string pXmlizedString)
{
XmlSerializer xs = new XmlSerializer(typeof(Reward));
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
//XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
return xs.Deserialize(memoryStream);
}
}
/**
* 方法3: 使用Resources
* PC、Android,IOS適用
* 但在行動平台上似乎只能讀,不能寫(未深入研究)
* */
using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System;
public class Text3 : MonoBehaviour {
//文本中每行的內容.
ArrayList infoall;
//skin資源,這裡用來顯示中文.
public GUISkin skin;
// Use this for initialization
void Start ()
{
//創建文件.
//CreateFile(Application.dataPath + "/Resources/", "FileName.txt", "test");
//得到文本中每一行的內容.
infoall = LoadFile(Application.dataPath + "/Resources/","FileName.txt");
}
/**
* path:文件創建目錄.
* name:文件的名稱.
* info:寫入的內容.
*/
void CreateFile(string path,string name,string info)
{
if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
return;
}
//文件流信息.
StreamWriter sw;
FileInfo t = new FileInfo(path+"//"+ name);
if(!t.Exists)
{
//如果文件不存在則創建.
sw = t.CreateText();
}
else
{
//如果此文件存在則打開.
sw = t.AppendText();
}
//以行的形式寫入信息.
sw.WriteLine(info);
//關閉流.
sw.Close();
//銷毀流.
sw.Dispose();
}
/**
* path:讀取文件的路徑.
* name:讀取文件的名稱.
*/
ArrayList LoadFile(string path,string name)
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
//使用流的形式讀取.
StreamReader sr = null;
#elif UNITY_ANDROID || UNITY_IPHONE
StringReader sr = null;
#endif
try
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
sr = File.OpenText(path+"//"+ name);
#elif UNITY_ANDROID || UNITY_IPHONE
TextAsset logFile = (TextAsset)Resources.Load("FileName");
sr = new StringReader(logFile.text);
#endif
}
catch(Exception e)
{
//路徑與名稱未找到文件則直接回傳null.
return null;
}
string line;
ArrayList arrlist = new ArrayList();
while ((line = sr.ReadLine()) != null)
{
//一行一行的讀取.
//將每一行的內容存入數組鏈表容器中.
arrlist.Add(line);
}
//關閉流.
sr.Close();
//銷毀流.
sr.Dispose();
//將數組鏈表容器返回.
return arrlist;
}
/**
* path:刪除文件的路徑.
* name:刪除文件的名稱.
*/
void DeleteFile(string path,string name)
{
File.Delete(path+"//"+ name);
}
}