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
原创粉丝点击