Unity 檔案讀取與寫入
阿新 • • 發佈:2018-12-26
Resources.LoadAssetAtPath();
僅限於在編輯器內使用
Build後出來的的所有AssetDatabase.LoadAssetAtPath();的返回值都為null;不建議使用。呼叫路徑為:Assets\Resources\A.FBX
Resources.Load();
使用時不管資源有沒有用到,都會加載出來,呼叫路徑為:A。並且忽略檔案的字尾名
Resources.Load();使用範圍很廣泛,幾乎可以載入任何一種檔案格式。
Object prefab = Resources.Load("MyPrefab/Prefab01");
注:Resources.Load返回的是Object,這裡就不需要格式轉換,不過這樣做沒有什麼用,還是需要格式轉換的。有兩種格式:(T的格式取決於FileName檔案的格式)
T t = (T)Resources.Load(FileName);
T t = Resources.Load(FileName) as T;
T t = Resources.Load<T>(FileName);
載入文字
TextAsset textAsset = (TextAsset)Resources.Load("Test1");
string str = textAsset.text;
*如果讀取的是XML、Config格式檔案,可以直接使用xml.LoadXml(str)來解析。
載入圖片
Texture2D texture2D = Resources.Load("Test1") as Texture2D; Sprite sprite = Resources.Load("Test1") as Sprite;
其他情況
舉例:
GameObject prefab = Resources.Load("Prefabs/Prefab01") as GameObject;
棄用的方法
/*Object cubePreb = Resources.Load(cubePath, typeof(GameObject));*/
使用的方法
GameObject vrgiftsall = Resources.Load("Prefab/VR/biggiftsvr") as GameObject;
載入2D圖片,並建立Sprite圖片
Texture2D aa = (Texture2D)Resources.Load("FullScreenfild/ic_heng_touxiangda") as Texture2D; Sprite kk = Sprite.Create(aa, new Rect(0, 0, aa.width, aa.height), new Vector2(0.5f, 0.5f));
將Sprite圖片賦值給遊戲物體
UIinit.CAVA.transform.FindChild("Panel/head_portrait_bg/head_portrait/Image").GetComponent<Image>().sprite = kk;
其他
this.GetComponent<Image>().sprite = Resources.Load<Sprite>("78");
this.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("78");
WWW載入檔案
WWW載入檔案
WWW可以載入各種型別的檔案,包括文字(.txt .xml .Config .xlx/.xlsx) 圖片(.jpg .png) 打包檔案(.assetBundle .FBX)
處理打包檔案時
using System.Collections;
using UnityEngine;
public class Manager : MonoBehaviour
{
string path;
AssetBundle a;
Texture2D t;
private IEnumerator Start()
{
WWW www = new WWW(path);
yield return www;
a = www.assetBundle;
}
}
處理圖片時,可以使用 Texture2D型別的變數來接收 www讀取的圖片檔案
舉例:Texture2D t = www.texture;
Texture檔案其實就是圖片資源的預設格式,Unity拖入外部圖片時,圖片格式自動轉換成Texture型別。
Texture圖片格式檔案轉換為Texture2D圖片
WWW www = new WWW(path);
yield return www;
Texture2D texture = www.texture;
if (www != null && string.IsNullOrEmpty(www.error))
{
Texture2DList.Add(FileName, texture);
}
Texture圖片格式檔案轉換為Sprite(2D(UGUI))圖片
這種轉換是圖片格式從Texture轉換為Texture2D的延續,其實也就是先進行後者,再從Texture2D轉換為Sprite(UGUI)。
Texture2D texture2D ;
Sprite sprite;
texture2D = www.texture;
if (www != null && string.IsNullOrEmpty(www.error))
{
sprite = Sprite.Create(texture2D , new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero);
}
處理文字資訊時
可以使用 XmlDocument(using System.XML) 、JsonData(using LitJson)和FileInfo(using System.IO) 等等來處理文字中字串資訊
XmlDocument
1)XmlDocument(using System.XML)
XmlDocument myxmldoc = new XmlDocument();
string str;
WWW www = new WWW(path);
yield return www;
str = www.text;//文字資訊賦值給string變數
myxmldoc.LoadXml(www.text);//用XML載入
JsonData
LitJson是一種輕量級的資料處理方法。它比XML要更快,更效率,更省資源FileInfo
Properties
FileInfo 檢測路徑的檔案是否存在,主要用在對讀取文字程式的保護
FileInfo file = new FileInfo(path);
if (file.Exists) { } //使用在微軟window平臺上,在移動平臺上不可用
if (file != null) { }//為了解決在移動上檢測檔案存在,使用此方法
使用XmlReader載入文字資訊
XmlDocument xml = new XmlDocument();
XmlReaderSettings set = new XmlReaderSettings();
set.IgnoreComments = true;//忽視xml的註釋文件
xml.Load(XmlReader.Create(path, set));