Unity中的资源管理
Unity中的资源管理
++++资源加载方式:
--脚本拖拽(本地)
--Resources加载(本地)
--AssetBundle加载(本地、远程[WWW])
#1、Resources类读取资源
++1.1、Resources类读取资源
----Load : 从Resources文件夹加载单个资源(可指定类型)。
----LoadAll : 从Resources文件夹加载多个资源(可指定类型)。
----LoadAsync : 异步从Resources文件夹加载单个资源。
----UnloadAsset : 从内存卸载资源。
----UnloadUnusedAssets : 卸载用不到的资源。
++++1、Resources.Load
++++2、常见的资源类型
++1.1.1、Resources.Load
--Resources.Load(“glass”); //通过名称获取资源
--Resources.Load<Texture>(“glass”); //通过名称获取指定类型的资源
--Resources.Load(“glass”, typeof(Texture)) as Texture; //当资源重名时使用
--示例:
void Start(){
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Plane);
Renderer rend = go.GetComponent<Renderer>();
rend.material.mainTexture = Resources.Load(“glass”) as Texture;
}
++1.1.2、常见的资源类型
--GameObject : 预制体
--Material : 材质
--Texture : 纹理
--Sprite : 精灵(小图片)
--Shader : 着色器
--Mesh : 网格
--AudioClip : 音频
#2、AssetBundle资源打包(新5.x)
++2.1、AssetBundle资源包
++++AssetBundle是Unity引擎提供的一种资源压缩文件,文件扩展名通常为unity3d或assetbundle。
++2.2、AssetBundle简介
++++流程: 开发者对资源进行打包后上传到网络服务器上,使用者的客户端从网络上加载资源包,进而获取最新资源。
++++用途:1、更新资源(如:游戏封面发生变化);2、压缩资源,减少所占空间。
++++特点: 资源使用灵活。
++++问题: 压缩失败,丢失关联,内存溢出,加载失败。
++2.3、资源设置
++++AssetBundle名称: materialasset.red
++2.4、BuildPipeline类
++++BuildPipeline.BuildAssetBundles打包资源
--outputPath : 资源打包的生成路径
--assetBundleOptions : AssetBundle打包设置
--targetPlatform : AssetBundle目标平台
++2.5、Editor打包AssetBundle
[MenuItem(“Assets/Build AssetBundles”)]
static void BuildAllAssetBundles(){
BuildPipeline.BuildAssetBundles(Application.dataPath + “/AssetBundles”, BuildAssetBundleOptions.None, BuildTarget.StandaloneOSXIntel);
}
++2.6、打包AssetBundle
++++1、编辑好一个材质素材和预设体素材的AssetBundle名称。
++++2、打包成AssetBundle。
++2.7、所有资源的说明清单
++++AssetBundles.mainfest(名称与文件名同名)
--版本号
--各个资源文件的说明
--资源名称(Name)
--资源依赖(Dependencies)
++2.8、每个资源的说明清单
++++prefab.redcube.mainfest
--版本号
--冗余校验(CRC)
--资源哈希(Hashes)
--所有类类型(Class Types)
--资源路径(Assets)
--资源依赖(Dependencies)
++2.9、依赖
++++如果资源存在依赖,打包时需连同依赖资源一起打包。
#3、AssetBundle资源使用(新5.x)
++3.1、加载流程
++++【资源清单】=>【清单说明】=>【依赖文件】=>【目标文件】
++3.2、下载资源清单
string Cdn = “http://127.127.1.1/”; //服务器地址
string mUrl = Cdn + “AssetBundles”; //额外的清单地址,清单文件名称与它所生成的文件名称一致,本例中为AssetBundles
WWW mwww = WWW.LoadFromCacheOrDownload(mUrl,1); //先下载清单
yield return mwww; //等待下载
++3.3、下载清单说明Mainfest
AssetBundle mab = mwww.assetBundle; //获取到清单
AssetBundleManifest manifest = (AssetBundleManifest)mab.LoadAsset(“AssetBundleManifest”); //找到清单所对应的说明文件Manifest,字符串为AssetBundleManifest,固定写法
mab.Unload(false); //暂不销毁资源
++3.4、下载依赖资源
string realName = “prefab.redcube”; //需要加载的资源(一个预设体)
string[] dps = mainfest.GetAllDependencies(realName); //获取此资源的依赖关系
AssetBundle[] abs = new AssetBundle[dps.Length]; //创建依赖资源列表
//逐个下载放入列表
for(int i = 0; i <dps.Length; i++){
string dUrl= Cdn + dps[i]; //服务器地址+依赖资源名称
WWW dwww = WWW.LoadFromCacheOrDownload(dUrl, mainfest.GetAssetBundleHash(dps[i])); //下载依赖资源
yield return dwww; //等待下载
abs[i] = dwww.assetBundle; //导入列表
object[] o = abs[i].LoadAllAssets(); //获取所有资源(调试使用)
Debug.Log((o[0] as Material).name);
}
++3.5、下载目标资源
WWW www= WWW.LoadFromCacheOrDownload(Cdn + realName, mainfest.GetAssetBundleHash(realName)); //依赖资源下载完毕之后,下载主资源
yield return www; //等待下载
//异常处理
if(!string.IsNullOrEmpty(www.error)){
Debug.Log(www.error);
}else{
AssetBundle ab= www.assetBundle; //获取资源
object[] oo= ab.LoadAllAssets(); //获取所有资源
GameObject gobj= 00[0] asGameObject; //获取预设体
if(gobj !=null){
Instantiate(gobj); //加载到场景
}
ab.Unload(false); //暂不销毁资源
}
//暂不销毁依赖资源,方便下面继续使用,如果不在需要可以销毁
foreach(AssetBundle abin abs){
ab.Unload(false); //暂不销毁资源
}
++新AssetBundle的优势
++++Manifest的链式结构
++++增量打包
#立钻哥哥Unity 学习空间: http://blog.****.net/VRunSoftYanlz/
++立钻哥哥推荐的拓展学习链接(Link_Url):
++++立钻哥哥Unity 学习空间: http://blog.****.net/VRunSoftYanlz/
++++Unity引擎基础:https://blog.****.net/vrunsoftyanlz/article/details/78881685
++++Unity面向组件开发:https://blog.****.net/vrunsoftyanlz/article/details/78881752
++++Unity物理系统:https://blog.****.net/vrunsoftyanlz/article/details/78881879
++++Unity2D平台开发:https://blog.****.net/vrunsoftyanlz/article/details/78882034
++++UGUI基础:https://blog.****.net/vrunsoftyanlz/article/details/78884693
++++UGUI进阶:https://blog.****.net/vrunsoftyanlz/article/details/78884882
++++UGUI综合:https://blog.****.net/vrunsoftyanlz/article/details/78885013
++++Unity动画系统基础:https://blog.****.net/vrunsoftyanlz/article/details/78886068
++++Unity动画系统进阶:https://blog.****.net/vrunsoftyanlz/article/details/78886198
++++Navigation导航系统:https://blog.****.net/vrunsoftyanlz/article/details/78886281
++++Unity特效渲染:https://blog.****.net/vrunsoftyanlz/article/details/78886403
++++Unity数据存储:https://blog.****.net/vrunsoftyanlz/article/details/79251273
++++Unity中Sqlite数据库:https://blog.****.net/vrunsoftyanlz/article/details/79254162
++++WWW类和协程:https://blog.****.net/vrunsoftyanlz/article/details/79254559
++++Unity网络:https://blog.****.net/vrunsoftyanlz/article/details/79254902
++++C#事件:https://blog.****.net/vrunsoftyanlz/article/details/78631267
++++C#委托:https://blog.****.net/vrunsoftyanlz/article/details/78631183
++++C#集合:https://blog.****.net/vrunsoftyanlz/article/details/78631175
++++C#泛型:https://blog.****.net/vrunsoftyanlz/article/details/78631141
++++C#接口:https://blog.****.net/vrunsoftyanlz/article/details/78631122
++++C#静态类:https://blog.****.net/vrunsoftyanlz/article/details/78630979
++++C#中System.String类:https://blog.****.net/vrunsoftyanlz/article/details/78630945
++++C#数据类型:https://blog.****.net/vrunsoftyanlz/article/details/78630913
++++Unity3D默认的快捷键:https://blog.****.net/vrunsoftyanlz/article/details/78630838
++++游戏相关缩写:https://blog.****.net/vrunsoftyanlz/article/details/78630687
++++设计模式简单整理:https://blog.****.net/vrunsoftyanlz/article/details/79839641
++++U3D小项目参考:https://blog.****.net/vrunsoftyanlz/article/details/80141811
++++UML类图:https://blog.****.net/vrunsoftyanlz/article/details/80289461
++++Unity知识点0001:https://blog.****.net/vrunsoftyanlz/article/details/80302012
++++U3D_Shader编程(第一篇:快速入门篇):https://blog.****.net/vrunsoftyanlz/article/details/80372071
++++U3D_Shader编程(第二篇:基础夯实篇):https://blog.****.net/vrunsoftyanlz/article/details/80372628
++++立钻哥哥Unity 学习空间: http://blog.****.net/VRunSoftYanlz/
--_--VRunSoft : lovezuanzuan--_--