項目中遇到需要讀取dat文件里文字內容的情況,姑且把我遇到的問題和解決方法總結一下~
之前加載xml的時候使用的是TextAsset,但是TextAsset不支持.dat文件T_T
TextAsset支持的文件類型如下:
- .txt
- .html
- .htm
- .xml
- .bytes
- .json
- .csv
- .yaml
- .fnt
詳細見官方說明:
http://docs.unity3d.com/550/Documentation/Manual/class-TextAsset.html
在bing搜索了一下關鍵字(度娘不給力),終於找到了解決方法,參考鏈接:
http://answers.unity3d.com/questions/181172/dat-file-location.html
要用到的就是這個類:StreamReader,用於讀取數據,相應的寫數據類為StreamWriter
具體是這樣做的:
//Application.dataPath是工程路徑,strFolderName是文件夾名,strFileName是文件名
StreamReader reader = new StreamReader(Application.dataPath+strFolderName+strFileName);
//轉化為字符串
string strAll = reader.ReadToEnd ();
然后就可以從字符串中獲得需要的內容了~~
