unity5.4版本打包AssetBundle与加载(避免材质丢失网格丢失)
来源:互联网 发布:下载硕鼠软件 编辑:程序博客网 时间:2024/05/20 15:39
unity5.4版本打包AssetBundle与加载(避免材质丢失网格丢失)
打包AssetBundle 有各种依赖关系要处理,针对不同的项目具体的打包策略也不同,这里假定是单个不重用的资源打包 完全没有任何依赖,做一个最基础的AssetBundle打包和加载的记录。
打包的代码
usingUnityEngine;
usingUnityEditor;
usingSystem.IO;
publicclassAutoCreateAssetBundle:Editor
{
publicstaticstringsourcePath =Application.dataPath +"/Resources";
conststringAssetBundlesOutPutPath ="Assets/StreamingAssets";
[MenuItem("Tools/Build")]
publicstaticvoidCreateAssetBundle()
{
stringoutputPath =Path.Combine(AssetBundlesOutPutPath,Platform.GetPlatformFloder(EditorUserBuildSettings.activeBuildTarget));
if(!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
BuildPipeline.BuildAssetBundles(outputPath,BuildAssetBundleOptions.None,EditorUserBuildSettings.activeBuildTarget);
AssetDatabase.Refresh();
}
[MenuItem("Tools/Clear")]
publicstaticvoidClearAllAssetBundleNames()
{
ClearAssetBundleName();
}
staticvoidClearAssetBundleName()
{
intlength =AssetDatabase.GetAllAssetBundleNames().Length;
Debug.Log("old length" + length);
string[] oldAssetBundleNames =newstring[length];
for(inti = 0; i < length; i++)
{
oldAssetBundleNames[i] =AssetDatabase.GetAllAssetBundleNames()[i];
}
for(intj = 0; j < oldAssetBundleNames.Length; j++)
{
AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j],true);
}
length =AssetDatabase.GetAllAssetBundleNames().Length;
Debug.Log("clear length" + length);
}
}
publicclassPlatform
{
publicstaticstringGetPlatformFloder(BuildTargettarget)
{
switch(target)
{
caseBuildTarget.Android:
return"Android";
caseBuildTarget.iOS:
return"IOS";
caseBuildTarget.StandaloneWindows:
caseBuildTarget.StandaloneWindows64:
return"Windows";
caseBuildTarget.StandaloneOSXIntel:
caseBuildTarget.StandaloneOSXIntel64:
caseBuildTarget.StandaloneOSXUniversal:
return"MAC";
default:
returnnull;
}
}
}
测试项目里有模型prefab,设定AssetBundle名字 prefab-bundle,打包成Assetbundle文件到 根据目标平台创建出来的合理保存路径中如Windows、Android、IOS等,得到四个文件其中有.magnifest后缀的文件 是说明文件,剩下的两个没有后缀文件 prefab-bundle 和 Windows才是加载的时候需要用到的文件。点开带.magnifest后缀的文件会发现 在依赖关系那里是空的,因为这里只设置了一个prefab的AssetBundle的名字 所以打包了一个单独的prefab,没有任何依赖关系。尽管如此 在加载具体的prefab之前还是需要先加载总文件夹的AssetBundleManifest文件才行。例如此处打包的是Windows平台的资源,保存路径设置的就是Windows 所以 总依赖文件就是Windows文件。
//load AssetBundleManirest first
publicvoidMyLoad(stringManifestName,stringtargetModelName,stringtargetModelBundleName,CallBackcallBack)
{
//加载AssetBundleManifest文件
AssetBundleassetBundleManifest =AssetBundle.LoadFromFile(GetLocalLoadURL(ManifestName));
if(assetBundleManifest !=null)
{
AssetBundleManifestmanifest = (AssetBundleManifest)assetBundleManifest.LoadAsset("AssetBundleManifest");
//先加载依赖文件
string[] depends = manifest.GetAllDependencies(targetModelName);
AssetBundle[] dependsAssetbundle =newAssetBundle[depends.Length];
for(intindex = 0; index < depends.Length; index++)
{
dependsAssetbundle[index] =AssetBundle.LoadFromFile(GetLocalLoadURL( depends[index]));
}
AssetBundlebundle =AssetBundle.LoadFromFile(GetLocalLoadURL(targetModelBundleName));
GameObjectobj = bundle.LoadAsset("jinjiu01")asGameObject;
if(obj !=null)
{
obj = Instantiate(obj);
obj.transform.localScale =newVector3(5,5,5);
obj.AddComponent<RotaController>();
callBack(obj);
}
//释放各种Assetbundle
assetBundleManifest.Unload(false);
bundle.Unload(false);
if(dependsAssetbundle.Length > 0)
{
for(inti = 0; i < dependsAssetbundle.Length; i++)
{
dependsAssetbundle[i].Unload(false);
}
}
}
}
这就是最最基础的打包和加载了。在unity4版本以前 如果没有依赖文件是可以直接加载对应的ab包的,在unity5版本后面需要先加载总AssetBundleManifest文件否则会出现丢失材质球 丢失网格模型等 各种丢失。
阅读全文
0 0
- unity5.4版本打包AssetBundle与加载(避免材质丢失网格丢失)
- Unity5.x 加载Assetbundle shader丢失问题记录
- Unity5.X打包与加载AssetBundle
- 使用AssetBundle加载材质时出现材质上的Shader丢失。
- Unity5.4打包AssetBundle
- Unity5 AssetBundle 打包以及加载
- Unity5 AssetBundle 打包以及加载
- Unity5 多场景 打包Assetbundle 以及 Shader Stripping 导致 LightMap 全部丢失的解决方法
- Unity5 多场景 打包Assetbundle 以及 Shader Stripping 导致 LightMap 全部丢失的解决方法
- Unity5.4 AssetBundle的完整项目源码(包括打包和加载)
- unity5.x assetbundle打包和加载
- Unity5 AssetBundle 打包代码和加载代码
- unity4.x升级到unity5.x材质丢失
- Unity加载AssetBundle后shader丢失问题
- 【Unity】Unity5版本的AssetBundle打包方案之资源打包
- 浅尝unity5.4AssetBundle打包流程
- 浅尝unity5.4AssetBundle打包流程
- Unity5.4新版AssetBundle资源打包
- js里面的push和concat的区别
- 书法空间更名为「书法孔见」通告
- Floyd-Warshall算法求解所有结点对的最短路径问题Java实现
- 城市的联动框
- anjular 的service
- unity5.4版本打包AssetBundle与加载(避免材质丢失网格丢失)
- android 仿ppt进入动画效果合集
- setLayerType的一些问题
- 简要分析unity3d中 yield
- 通过#if和#endif灵活的为自己代码注释
- java程序员学习路线指南
- Spring MVC 中的基于注解的 Controller
- MapReduce程序理解
- mysql 到出到csv 带列表明