Data与Resources文件夹
一般只读文件放到Resources目录
Data用于新建的文件或者要修改的文件
=================================================================
Unity默认的Resource加载
保存和读取资源的 最简单的 方式了。但是 如果想让资源在编译后不改变,只能这样做。
因为这里面的内容会 组合并编译到resource.assets文件(此文件可以在编译过的游戏的Data文件中找到)
可以从编译过的项目文件Resource.assets中去加载
用法
//Texture2D (Texture2D)Resouce.Load(fileName);
//文本资源 (TextAsset)Resouce.Load("ddd");
读取文本内容 TestAsset.text
//声音 (AudioClip)Resouce.Load();
==========================================================
加载编译之后的文件
using System.IO; using UnityEngine; using System.Collections; public class temp : MonoBehaviour { private string fileName = "a.jpg"; private string url; private Texture2D externalImage; // Use this for initialization void Start() { url = "file:" + Application.dataPath; url = Path.Combine(url, "Resources"); url = Path.Combine(url, fileName); StartCoroutine(LoadWWW()); } private IEnumerator LoadWWW() { yield return 0; WWW www=new WWW(url); yield return www; externalImage = www.texture; } // Update is called once per frame void Update() { } }
需要System.IO类
www类的对象时以“file:"开头的URL,
使用Path.Combine()而不是 ” /“以避免跨平台的问题
WWW和资源内容
WWW类定义了几个不同的属性和方法 用于把下载的资源 文件 数据提取到变量中
.text 只读 web数据作为string类型返回
.texture 只读 作为Texture2D类型返回
.GetAudioClip() 方法 作为AudioClip对象类型返回
=======================================================
通过从互联网 下载文件与加载外部资源文件
从服务器下载
using UnityEngine; using System.Collections; public class temp : MonoBehaviour { private string url="http://www.packtpub.com/sites/default/files/packt_logo.png"; private Texture2D externalImage; // Use this for initialization void Start() { StartCoroutine(LoadWWW()); } private void OnGUI() { GUILayout.Label("url="+url); GUILayout.Label(externalImage); } private IEnumerator LoadWWW() { yield return 0; WWW imageFile=new WWW(url); yield return imageFile; externalImage = imageFile.texture; } // Update is called once per frame void Update() { } }
==========================================================
使用外部文本文件和XML数据
最简单的是TextAsset 共有变量(编译后数据文件不再改变的情况下适用)
public TextAsset DataTestAsset; private string textData; void Start() { textData = DataTestAsset.text; }
C#文件流加载外部文本文件
using System; using UnityEngine; using System.Collections; using System.IO; public class FileReadWriteManager { public void WriteTextFile(string pathAndName, string stringData) { //remove file ,if exists FileInfo textFile=new FileInfo(pathAndName); if (textFile.Exists) { textFile.Delete(); } //create new empty file StreamWriter writer; writer = textFile.CreateText(); //write text to file writer.Write(stringData); writer.Close(); } public string ReadTextFile(string pathAndName) { string dataAsString = ""; try { //open text file StreamReader textReader = File.OpenText(pathAndName); //read content dataAsString = textReader.ReadToEnd(); textReader.Close(); } catch (Exception e) { } return dataAsString; } }
调用示例
private string fileName = "cities.txt"; private string filePath = ""; private FileReadWriteManager fileReadWriteManager=new FileReadWriteManager(); void Start() { filePath = Path.Combine(Application.dataPath, "Resources"); filePath = Path.Combine(filePath, fileName); string textContent = fileReadWriteManager.ReadTextFile(filePath); }
private string fileName = "hello.txt"; private string foleName = "Data"; //需要在Project面板中创建一个Data目录文件夹 private string filePath = ""; private FileReadWriteManager fileReadWriteManager=new FileReadWriteManager(); void Start() { filePath = Application.dataPath + Path.DirectorySeparatorChar + fileName; string textData = "abc def"; fileReadWriteManager.WriteTextFile(filePath, textData); filePath = Path.Combine(filePath, fileName); string textContent = fileReadWriteManager.ReadTextFile(filePath); }