assetbundle打包场景在Windows平台上打包无限失败.紧急求助

unity提供了assetbunlde机制,下面介绍一种方法将指定目录下的所有文件打包成AssetBundle先说明步骤,再上代码。步骤一、选择要打包成assetbundle的目录,本案例使用assetbundle_raw步骤二、把要打包的资源或者目录都放到assetbundle_raw目录,资源可以是& &&&&&&&&&&&&&&&prefab,png,fbx,font,audio,txt等。步骤三、给assetbundle_raw目录下所有的资设置assetbundle名称,如果是目录,会递归。&& & &&&&&&&&名称格式:资源名称.资源后缀.unity3d,&&&&&&&&&&&&如果有子目录,则资源名称为:子目录/子目录/.../子目录/资源名称.资源后缀.unity3d&&&&&&&&&&&&在给这些资源设置assetbundle名称的时候会把该资源所依赖的所有资源都设置上&&&&&&&&&&&&assetbundle名称。脚本已经写好,稍后附上,这一步你只需要点击&&&&&&&&&&&&Tools-&AssetBundle-&Set Asset Bundle Names就可以。步骤四、选择assetbundle输出目录,本案例使用StreamingAssets目录。只所以要使用这个目录,主要&&&&&&&&&&&&是考虑到本地测试会比较方便,打包好的assetbundle肯定需要进行一下测试,没问题后再放到&&&&&&&&&&&&资源上,另外就是可以把StreamingAssets目录作为本地资源测试服务器,还可以打包到&&&&&&&&&&&&设备上进行测试。操作:Tools-&AssetBundle-&Build Bundles。步骤五、生成资源版本文件。步骤四执行完毕后,assetbundle已经生成,另外还会生成Manifest文件,&&&&&&&&&&&&因为assetbundle和平台相关,以平台为例,会生成android和android.manifest文件,&&&&&&&&&&&&我们需要根据android文件获取每个bundle的hash值,生成版本文件,用于热更新。废话不多&&&&&&&&&&&&说,操作:Tools-&AssetBundle-&Generate Resource
File。至此,assetbundle打包完成。特别需要注意:&&&&放到assetbundle_raw目录下的资源,互相之间不要有任何依赖关系,否则,在步骤三设置名称的阶段,被依赖的资源就会被设置两次或者多次assetbundle名称,这样它只能使用最后设置的名称。如果是这样的话,在加载assetbundle的时候,就可能出现已经加载的assetbundle被再次加载而报错。因为被加载的assetbundle在被释放前不能再次加载。所以,放到assetbundle_raw目录下的资源,互相之间不要有任何依赖关系;放到assetbundle_raw目录下的资源,互相之间不要有任何依赖关系;放到assetbundle_raw目录下的资源,互相之间不要有任何依赖关系。重要的话说三遍。另外需要注意的地方,在前面的中也提到了。要打包的资源名称不要有空格,空格在pc上支持,在android上不支持名称用小写,下划线,数字,尽量不要用大写,因为unity打包成assetbundle后的资源名称都是小写建议unity所有的资源命名的时候用小写,下划线,数字,尽量不要用大写如果用到一些插件,例如NGUI等,里面的资源,如,shader文件名称中间是有空格的,它会作为依赖资源打包成assetbundle,这样的手机上加载就可能出错。编辑器代码:using UnityEusing .Cusing UnityEusing System.IO;using Susing System.Collections.Gusing System.Tusing LitJpublic class Builder : Editor{& & public static string sourcePath = Application.dataPath + "/assetbundle_raw";& & const string AssetBundlesOutputPath = "Assets/StreamingAssets";& & static string m_assetPath = Application.streamingAssetsP& & static string assetTail = ".unity3d";& & static bool canFinish =& & //生成的bundle的相对路径列表& & static List&string& bundleRelativePaths = new List&string&();& & [MenuItem("Tools/AssetBundle/Set Asset Bundle Names")]& & public static void SetBundleNames()& & {& & & & ClearAssetBundlesName();& & & & bundleRelativePaths.Clear();& & & & Pack(sourcePath);& & & & Debug.Log("Bundles 名称设置完成");& & }& & [MenuItem("Tools/AssetBundle/Build Bundles")]& & public static void BuildAssetBundle()& & {& & & & string outputPath = bine(AssetBundlesOutputPath, Platform.GetPlatformFolder(EditorUserBuildSettings.activeBuildTarget));& & & & if (!Directory.Exists(outputPath))& & & & {& & & & & & Directory.CreateDirectory(outputPath);& & & & }& & & & //根据BuildSetting里面所激活的平台进行打包& & & & BuildPipeline.BuildAssetBundles(outputPath, 0, EditorUserBuildSettings.activeBuildTarget);& & & & Debug.Log("打包完成");& & & & Asset.Refresh();& & }& & [MenuItem("Tools/AssetBundle/Generate Resource Version File")]& & public static void GenerateVersionFile()& & {& & & & //生成& & & & GenerateCfgFile();& & & & EditorCoroutineRunner.StartEditorCoroutine(wait());& & & & Debug.Log("成功生成资源版本文件");& & & & AssetDatabase.Refresh();& & }& & static IEnumerator wait()& & {& & & & Debug.Log("wait");& & & & while (!canFinish)& & & & & && & }& & /// &summary&& & /// 生成资源配置文件resource_version& & /// &/summary&& & static void GenerateCfgFile()& & {& & & & Debug.Log("GenerateCfgFile");& & & & getManifest();& & }& & /// &summary&& & /// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包& & /// 之前说过,只要设置了AssetBundleName的,都会进行打包,不论在什么目录下& & /// &/summary&& & static void ClearAssetBundlesName()& & {& & & & string[] bundleNames = AssetDatabase.GetAllAssetBundleNames();& & & & int length = bundleNames.L& & & & Debug.Log(length);& & & & string[] oldAssetBundleNames = new string[length];& & & && & & & for (int j = 0; j & bundleNames.L j++)& & & & {& & & & & & AssetDatabase.RemoveAssetBundleName(bundleNames[j], true);& & & & }& & & & length = AssetDatabase.GetAllAssetBundleNames().L& & & & Debug.Log(length);& & }& & static void Pack(string source)& & {& & & & DirectoryInfo folder = new DirectoryInfo(source);& & & & FileSystemInfo[] files = folder.GetFileSystemInfos();& & & & int length = files.L& & & & for (int i = 0; i & i++)& & & & {& & & & & & if (files[i] is DirectoryInfo)& & & & & & {& & & & & & & & Pack(files[i].FullName);& & & & & & }& & & & & & else& & & & & & {& & & & & & & & if (!files[i].Name.EndsWith(".meta"))& & & & & & & & {& & & & & & & & & & file(files[i].FullName);& & & & & & & & & & Debug.Log("files[i].FullName is " + files[i].FullName);& & & & & & & & }& & & & & & & &&& & & & & & }& & & & }& & }& & static void file(string source)& & {& & & & string _source = Replace(source);& & & & string _assetPath = "Assets" + _source.Substring(Application.dataPath.Length);& & & & string _assetPath2 = _source.Substring(Application.dataPath.Length + 1);& & & & Debug.Log("_assetPath is " + _assetPath);& & & & Debug.Log("_assetPath2 is " + _assetPath2);& & & & //在代码中给资源设置AssetBundleName& & & & AssetImporter assetImporter = AssetImporter.GetAtPath(_assetPath);& & & & string assetName = _assetPath2.Substring(_assetPath2.IndexOf("/") + 1);& & & & //assetName = assetName.Replace(Path.GetExtension(assetName), ".unity3d");& & & & assetName = assetName + ".unity3d";& & & & assetImporter.assetBundleName = assetN& & & & fileDependences(_assetPath);& & & & Debug.Log("assetName is " + assetName);& & & & //保存bundle的相对路径& & & & if (!bundleRelativePaths.Contains(assetName))& & & & & & bundleRelativePaths.Add(assetName);& & }& & static string Replace(string s)& & {& & & & return s.Replace("\\", "/");& & }& & static void fileDependences(string path)& & {& & & & try& & & & {& & & & & & //string path = AssetDatabase.GetAssetPath(0);& & & & & & Debug.Log("path is " + path);& & & & & & string[] deps = AssetDatabase.GetDependencies(path);& & & & & & int counter = 0;& & & & & & for (int i = 0; i & deps.L i++)& & & & & & {& & & & & & & & if (deps[i].Equals(path) || deps[i].EndsWith(".cs"))& & & & & & & & & && & & & & & & & ++& & & & & & }& & & & & & if (counter == 0)& & & & & & & && & & & & & for (int i = 0; i & deps.L i++)& & & & & & {& & & & & & & & Debug.Log("deps " + i + " is " + deps[i]);& & & & & & & & if (deps[i].Equals(path) || deps[i].EndsWith(".cs"))& & & & & & & & & && & & & & & & & AssetImporter ai = AssetImporter.GetAtPath(deps[i]);& & & & & & & & string assetName = deps[i] + ".unity3d";& & & & & & & & assetName = assetName.Substring(assetName.IndexOf("/") + 1);& & & & & & & & Debug.Log("assetName is " + assetName);& & & & & & & & ai.assetBundleName = assetN& & & & & & & & //保存bundle的相对路径& & & & & & & & if (!bundleRelativePaths.Contains(assetName))& & & & & & & & & & bundleRelativePaths.Add(assetName);& & & & & & & & fileDependences(deps[i]);& & & & & & }& & & & }& & & & catch (Exception error)& & & & {& & & & & & Debug.Log("error is " + error);& & & & }& & }& & #region LoadAssetBundle& & /// &summary&& & /// 加载目标资源& & /// &/summary&& & /// &param name="name"&&/param&& & /// &param name="callback"&&/param&& & public static void LoadAssetBundle(string name, Action&UnityEngine.Object& callback)& & {& & & & name = name + assetT//eg:ui/panel.unity3d& & & & Action&List&AssetBundle&& action = (depenceAssetBundles) =&& & & & {& & & & & & string realName = GetRuntimePlatform() + "/" +//eg:/ui/panel.unity3d& & & & & & Debug.Log("realName is " + realName);& & & & & & LoadResReturnWWW(realName, (www) =&& & & & & & {& & & & & & & & int index = realName.LastIndexOf("/");& & & & & & & & string assetName = realName.Substring(index + 1);& & & & & & & & assetName = assetName.Replace(assetTail, "");& & & & & & & & AssetBundle assetBundle = www.assetB& & & & & & & & UnityEngine.Object obj = assetBundle.LoadAsset(assetName);//LoadAsset(name),这个name没有后缀,eg:panel& & & & & & & & //卸载资源内存& & & & & & & & assetBundle.Unload(false);& & & & & & & & for (int i = 0; i & depenceAssetBundles.C i++)& & & & & & & & {& & & & & & & & & & depenceAssetBundles[i].Unload(false);& & & & & & & & }& & & & & & & & //加载目标资源完成的回调& & & & & & & & callback(obj);& & & & & & });& & & & };& & & & LoadDependenceAssets(name, action);& & }& & static void writeCfgFile(AssetBundleManifest manifest)& & {& & & & if (null == manifest)& & & & & && & & & StringBuilder sb = new StringBuilder();& & & & JsonWriter js = new JsonWriter(sb);& & & & try& & & & {& & & & & & js.WriteObjectStart();& & & & & & js.WritePropertyName("id");& & & & & & js.Write(0);& & & & & & js.WritePropertyName("version");& & & & & & js.Write("1.0");& & & & & & string platform = Platform.GetPlatformFolder(EditorUserBuildSettings.activeBuildTarget);& & & & & & js.WritePropertyName("manifest");& & & & & & js.Write(platform);& & & & & & js.WritePropertyName("resource");& & & & & & string[] bundleNames = AssetDatabase.GetAllAssetBundleNames();& & & & & & js.WriteObjectStart();& & & & & & foreach (string path in bundleNames)& & & & & & {& & & & & & & & Hash128 hash = manifest.GetAssetBundleHash(path);& & & & & & & & //if (hash.isValid)& & & & & & & & {& & & & & & & & & & js.WritePropertyName(path);& & & & & & & & & & js.Write(hash.ToString());& & & & & & & & }& & & & & & }& & & & & & js.WriteObjectEnd();& & & & & & js.WriteObjectEnd();& & & & }& & & & catch (Exception error)& & & & {& & & & & & Debug.Log("Write json error : " + error.);& & & & }& & & & string strVersion = sb.ToString().ToLower();& & & & try& & & & {& & & & & & string platform = GetRuntimePlatform();& & & & & & File.WriteAllText(AssetBundlesOutputPath + "/" + platform + "/resource_version", strVersion);& & & & }& & & & catch (Exception error)& & & & {& & & & & & Debug.Log("Write Cfg file error : " + error.Message);& & & & }& & & & canFinish =& & }& & /// &summary&& & /// 获取总manifest& & /// &/summary&& & static void getManifest()& & {& & & & Action&AssetBundleManifest& dependenceAction = (manifest) =&& & & & {& & & & & & writeCfgFile(manifest);& & & & };& & & & LoadAssetBundleManifest(dependenceAction);& & }& & /// &summary&& & /// 加载目标资源的依赖资源& & /// &/summary&& & /// &param name="targetAssetName"&&/param&& & /// &param name="action"&&/param&& & private static void LoadDependenceAssets(string targetAssetName, Action&List&AssetBundle&& action)& & {& & & & Debug.Log("要加载的目标资源:" + targetAssetName);//ui/panel.unity3d& & & & Action&AssetBundleManifest& dependenceAction = (manifest) =&& & & & {& & & & & & List&AssetBundle& depenceAssetBundles = new List&AssetBundle&();//用来存放加载出来的依赖资源的AssetBundle& & & & & & string[] dependences = manifest.GetAllDependencies(targetAssetName);& & & & & & //获取hash值& & & & & & Hash128 hash = manifest.GetAssetBundleHash(targetAssetName);& & & & & & Debug.Log(targetAssetName + " hash is " + hash);& & & & & & Debug.Log("依赖文件个数:" + dependences.Length);& & & & & & int length = dependences.L& & & & & & int finishedCount = 0;& & & & & & if (length == 0)& & & & & & {& & & & & & & & //没有依赖& & & & & & & & action(depenceAssetBundles);& & & & & & }& & & & & & else& & & & & & {& & & & & & & & //有依赖,加载所有依赖资源& & & & & & & & for (int i = 0; i & i++)& & & & & & & & {& & & & & & & & & & string dependenceAssetName = dependences[i];& & & & & & & & & & dependenceAssetName = GetRuntimePlatform() + "/" + dependenceAssetN//eg:Windows/altas/heroiconatlas.unity3d& & & & & & & & & & //加载,加到assetpool& & & & & & & & & & LoadResReturnWWW(dependenceAssetName, (www) =&& & & & & & & & & & {& & & & & & & & & & & & int index = dependenceAssetName.LastIndexOf("/");& & & & & & & & & & & & string assetName = dependenceAssetName.Substring(index + 1);& & & & & & & & & & & & assetName = assetName.Replace(assetTail, "");& & & & & & & & & & & & AssetBundle assetBundle = www.assetB& & & & & & & & & & & & UnityEngine.Object obj = assetBundle.LoadAsset(assetName);& & & & & & & & & & & & //assetBundle.Unload(false);& & & & & & & & & & & & depenceAssetBundles.Add(assetBundle);& & & & & & & & & & & & finishedCount++;& & & & & & & & & & & & if (finishedCount == length)& & & & & & & & & & & & {& & & & & & & & & & & & & & //依赖都加载完了& & & & & & & & & & & & & & action(depenceAssetBundles);& & & & & & & & & & & & }& & & & & & & & & & });& & & & & & & & }& & & & & & }& & & & };& & & & LoadAssetBundleManifest(dependenceAction);& & }& & /// &summary&& & /// 加载AssetBundleManifest& & /// &/summary&& & /// &param name="action"&&/param&& & private static void LoadAssetBundleManifest(Action&AssetBundleManifest& action)& & {& & & & string manifestName = GetRuntimePlatform();& & & & Debug.Log("Application.platform is " + Application.platform);& & & & manifestName = manifestName + "/" + manifestN//eg:Windows/Windows& & & & Debug.Log("manifestName is " + manifestName);& & & & LoadResReturnWWW(manifestName, (www) =&& & & & {& & & & & & AssetBundle assetBundle = www.assetB& & & & & & UnityEngine.Object obj = assetBundle.LoadAsset("AssetBundleManifest");& & & & & & assetBundle.Unload(false);& & & & & & AssetBundleManifest manif = obj as AssetBundleM& & & & & & Debug.Log("(www) " + manif.name);& & & & & & action(manif);& & & & });& & }& & #endregion& & #region ExcuteLoader& & public static void LoadResReturnWWW(string name, Action&WWW& callback)& & {& & & & string path = "file://" + m_assetPath + "/" +& & & & Debug.Log("m_assetPath is " + m_assetPath);& & & & Debug.Log("name is " + name);& & & & Debug.Log("加载:" + path);& & & & EditorCoroutineRunner.StartEditorCoroutine(LoaderRes(path, callback));& & }& & static IEnumerator LoaderRes(string path, Action&WWW& callback)& & {& & & & WWW www = new WWW(path);& & & && & & & callback(www);& & }& & #endregion& & #region Util& & /// &summary&& & /// 平台对应& & /// &/summary&& & /// &returns&&/returns&& & private static string GetRuntimePlatform()& & {& & & & string platform = "";& & & & if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)& & & & {& & & & & & //platform = "Windows";& & & & & & platform = "android";& & & & }& & & & else if (Application.platform == RuntimePlatform.Android)& & & & {& & & & & & platform = "android";& & & & }& & & & else if (Application.platform == RuntimePlatform.Player)& & & & {& & & & & & platform = "ios";& & & & }& & & & else if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor)& & & & {& & & & & & platform = "osx";& & & & }& & & && & }& & #endregion}public class Platform{& & public static string GetPlatformFolder(BuildTarget target)& & {& & & & switch (target)& & & & {& & & & & & case BuildTarget.Android:& & & & & & & & return "android";& & & & & & case BuildTarget.iOS:& & & & & & & & return "ios";& & & & & & case BuildTarget.WebPlayer:& & & & & & & & return "webplayer";& & & & & & case BuildTarget.StandaloneWindows:& & & & & & case BuildTarget.StandaloneWindows64:& & & & & & & & return "windows";& & & & & & case BuildTarget.StandaloneOSXIntel:& & & & & & case BuildTarget.StandaloneOSXIntel64:& & & & & & case BuildTarget.StandaloneOSXUniversal:& & & & & & & & return "osx";& & & & & & default:& & & & & & & && & & & }& & }}用到了一个工具类,这是一个在编辑器下可运行的协程类,我也是从网上找的using UnityEusing UnityEusing System.Cusing System.Collections.Gusing pilerSpublic static class EditorCoroutineRunner{& & private class EditorCoroutine : IEnumerator& & {& & & & private Stack&IEnumerator& executionS& & & & public EditorCoroutine(IEnumerator iterator)& & & & {& & & & & & this.executionStack = new Stack&IEnumerator&();& & & & & & this.executionStack.Push(iterator);& & & & }& & & & public bool MoveNext()& & & & {& & & & & & IEnumerator i = this.executionStack.Peek();& & & & & & if (i.MoveNext())& & & & & & {& & & & & & & & object result = i.C& & & & & & & & if (result != null && result is IEnumerator)& & & & & & & & {& & & & & & & & & & this.executionStack.Push((IEnumerator)result);& & & & & & & & }& & & & & & & && & & & & & }& & & & & & else& & & & & & {& & & & & & & & if (this.executionStack.Count & 1)& & & & & & & & {& & & & & & & & & & this.executionStack.Pop();& & & & & & & & & && & & & & & & & }& & & & & & }& & & & & && & & & }& & & & public void Reset()& & & & {& & & & & & throw new System.NotSupportedException("This Operation Is Not Supported.");& & & & }& & & & public object Current& & & & {& & & & & & get { return this.executionStack.Peek().C }& & & & }& & & & public bool Find(IEnumerator iterator)& & & & {& & & & & & return this.executionStack.Contains(iterator);& & & & }& & }& & private static List&EditorCoroutine& editorCoroutineL& & private static List&IEnumerator&& & public static IEnumerator StartEditorCoroutine(IEnumerator iterator)& & {& & & & if (editorCoroutineList == null)& & & & {& & & & & & // test& & & & & & editorCoroutineList = new List&EditorCoroutine&();& & & & }& & & & if (buffer == null)& & & & {& & & & & & buffer = new List&IEnumerator&();& & & & }& & & & if (editorCoroutineList.Count == 0)& & & & {& & & & & & EditorApplication. += U& & & & }& & & & // add iterator to buffer first& & & & buffer.Add(iterator);& & & && & }& & private static bool Find(IEnumerator iterator)& & {& & & & // If this iterator is already added& & & & // Then ignore it this time& & & & foreach (EditorCoroutine editorCoroutine in editorCoroutineList)& & & & {& & & & & & if (editorCoroutine.Find(iterator))& & & & & & {& & & & & & & && & & & & & }& & & & }& & & && & }& & private static void Update()& & {& & & & // EditorCoroutine execution may append new iterators to buffer& & & & // Therefore we should run EditorCoroutine first& & & & editorCoroutineList.RemoveAll& & & & (& & & & & & coroutine =& { return coroutine.MoveNext() == }& & & & );& & & & // If we have iterators in buffer& & & & if (buffer.Count & 0)& & & & {& & & & & & foreach (IEnumerator iterator in buffer)& & & & & & {& & & & & & & & // If this iterators not exists& & & & & & & & if (!Find(iterator))& & & & & & & & {& & & & & & & & & & // Added this as new EditorCoroutine& & & & & & & & & & editorCoroutineList.Add(new EditorCoroutine(iterator));& & & & & & & & }& & & & & & }& & & & & & // Clear buffer& & & & & & buffer.Clear();& & & & }& & & & // If we have no running EditorCoroutine& & & & // Stop calling update anymore& & & & if (editorCoroutineList.Count == 0)& & & & {& & & & & & EditorApplication.update -= U& & & & }& & }}后面我会分享热更新策略,用到了上面生成的资源版本文件。以上是我用assetbundle打包的使用案例,本人能力有限,有错误和不足的地方,还请不吝赐教,万分感激!跟我从零基础学习Unity3D开发--资源打包篇(AssetBundle) -
跟我从零基础学习Unity3D开发--资源打包篇(AssetBundle)
好久没更新了,一直在加班敢项目进度。这里和关注我的博客的童鞋表示一下歉意!这里有我录的Unity3D从零开始的视频教程大家可以关注一下:/view/555 &视频会陆续的更新,需要讲什么也可以留言给我。
之前在项目中一直用的是别人以前的写的打包代码,后来项目需要需要修改以前的代码把打包的代码过了一遍,今天把自己遇到的问题和打包思路在这路写下来。
在untiy支援加载方面分为两个种类:
第一类:Resources方式加载-------我们不用打包资源,放在项目中的Resources目录下就能在代码中直接加载出来。
第二类:assetBundle方式加载-----需要我们打包资源,把每个资源打包成一个个Bundle,然后加载。
两类资源加载方式在游戏项目中都会用,Resources加载的资源一般是要求非常及时的,assetBundle加载相对要慢一些因为需要把assetBundle资源解压出来。assetBundle也是方便项目中做热更新(在游戏对外发布的过程中,我们需要替换一些资源,通过热更新的方式把资源更新出去供当前的客户端下载替换)。Assetbundle是官方推荐使用的打包方式,AssetbundleUnity(Pro)提供的资源打包策略。
打包思路:
a、打包的资源一定要放在StreamingAssets目录下因为在移动平台下只能访问这个目录
b、AssetBundle的保存后缀名可以是assetbundle或者unity3d
c、BuildAssetBundle要根据不同的平台单独打包,BuildTarget参数指定平台,如果不指定,默认的webplayer
1、准备好我们需要打包的资源
2、加载需要打包的资源
3、获取当前对象列表的所有依赖项
4、设置打包编译选项
5、开始打包
重要函数解释: Selection.objects & ---------------------------------------------------& 这个方法是用于判断在Asset目录中返回选中了哪些资源文件 AssetDatabase.GetAssetpath()-------------------------------------- 获取的是工程文件相对于Asset的路径 AssetDatabase.LoadMainAssetAtPath()---------------------------- & 加载指定路径下的资源 EditorUtility.CollectDependencies()--------------------------------- 获取当前对象列表的所有依赖项 BuildAssetBundleOptions ------------------------------------------- 打包编译选项 BuildPipeline.BuildAssetBundle() ---------------------------------- 打包函数 Path.GetFileNameWithoutExtension() ----------------------------- 获取路径下的不要扩展名的文件名 Path.ChangeExtension() -------------------------------------------- & 改变路径的文件后缀名 EditorUserBuildSettings.activeBuildTarget ----------------------- & 判断当前编译平台 & & 下面列出打包实例代码! 实例: &
1 using UnityE
2 using System.C
3 using UnityE
4 using System.IO;
5 using System.Collections.G
6 public class AssetBoundExport :EditorWindow {
[MenuItem(&Window/资源打包&)]
static void Init()
EditorWindow.GetWindow(typeof(AssetBoundExport));
void OnGUI()
*ui1.prefab
和ui2.prefab 是两个预制件,里面都包含loading.png这张图
*这个方法是讲依赖打包的
*通一个资源用在多个预制件中的时候我们就采用这种依赖打包更小
if (GUILayout.Button(&打包UI对象&, GUILayout.Width(100)))
BuildPipeline.PushAssetDependencies();
Object baseTexture = AssetDatabase.LoadMainAssetAtPath(&Assets/Texture/loading.png&);
ExportBundle&Texture2D&(AssetDatabase.GetAssetPath(baseTexture));
BuildPipeline.PushAssetDependencies();
Object p1 = AssetDatabase.LoadMainAssetAtPath(&Assets/Prefab/ui1.prefab&);
ExportBundle&GameObject&(AssetDatabase.GetAssetPath(p1));
BuildPipeline.PopAssetDependencies();
BuildPipeline.PushAssetDependencies();
Object p2 = AssetDatabase.LoadMainAssetAtPath(&Assets/Prefab/ui2.prefab&);
ExportBundle&GameObject&(AssetDatabase.GetAssetPath(p2));
BuildPipeline.PopAssetDependencies();
BuildPipeline.PopAssetDependencies();
AssetDatabase.Refresh();
/// &summary&
/// 这个方法是用于单独给某类资源打包(这个方法实例是打图片的)
/// 应用场景:在实际项目中我们每次不一定是要全部打包,更多的时候是只需要打某一个资源
/// &/summary&
[MenuItem(&Assets/打包/Textrue&)]
public static void PackageTextureAssetBundle()
Object[] _Object = Selection.
for (int i = 0; i & _Object.L i++)
ExportBundle&Texture2D&(AssetDatabase.GetAssetPath(_Object[i]));
/// &summary&
/// 打包通用类
/// &/summary&
/// &typeparam name=&T&&&/typeparam&
/// &param name=&path&&&/param&
private static void ExportBundle&T&(string path) where T:Object
T assetObject = AssetDatabase.LoadMainAssetAtPath(path) as T;
if (assetObject == null)
Debug.Log(&can't load &+path);
string _path = Application.dataPath + &/StreamingAssets/PC/& + Path.GetFileNameWithoutExtension(path);
_path = Path.ChangeExtension(_path, &.unity3d&);
List&Object& obejectList = new List&Object&();
Object[] tempobject = new Object[1];
tempobject[0] = assetO
Object[] kDepens = EditorUtility.CollectDependencies(tempobject);
for (int i = 0; i & kDepens.L i++)
obejectList.Add(kDepens[i]);
BuildAssetBundleOptions babo = BuildAssetBundleOptions.CollectDependencies | pleteAssets
85 | BuildAssetBundleOptions.DeterministicAssetB
bool istru=
BuildPipeline.BuildAssetBundle(assetObject, obejectList.ToArray(),_path, babo, EditorUserBuildSettings.activeBuildTarget);
if (istru == true)
Debug.Log(&打包成功&);
Debug.Log(&打包失败&);
好久没更新了,一直在加班敢项目进度。这里和关注我的博客的童鞋表示一下歉意!这里有我录的Unity3D从零开始的视频教程大家可以关注一下:/view/555 视频会陆续的更新,需...
相关阅读排行
相关内容推荐
请激活账号
为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。
您的注册邮箱:
如果您没有收到激活邮件,请注意检查垃圾箱。

我要回帖

更多关于 assetbundle打包脚本 的文章

 

随机推荐