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));版权声明:本文为a555666_原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。