Unity5自动命名Assetbundle并打包
2016-03-24 11:05
405 查看
http://www.shihuanjue.com/?p=57
using UnityEngine; using System.Collections; using UnityEditor; using System.IO; /// <summary> /// 把Resource下的资源打包成.unity3d 到StreamingAssets目录下 /// </summary> public class Builder : Editor { public static string sourcePath = Application.dataPath + "/Resources"; const string AssetBundlesOutputPath = "Assets/StreamingAssets"; [MenuItem("Tools/AssetBundle/Build")] public static void BuildAssetBundle() { ClearAssetBundlesName (); Pack (sourcePath); string outputPath = Path.Combine (AssetBundlesOutputPath,Platform.GetPlatformFolder(EditorUserBuildSettings.activeBuildTarget)); if (!Directory.Exists (outputPath)) { Directory.CreateDirectory(outputPath); } //根据BuildSetting里面所激活的平台进行打包 BuildPipeline.BuildAssetBundles (outputPath,0,EditorUserBuildSettings.activeBuildTarget); AssetDatabase.Refresh (); Debug.Log ("打包完成"); } /// <summary> /// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包 /// 之前说过,只要设置了AssetBundleName的,都会进行打包,不论在什么目录下 /// </summary> static void ClearAssetBundlesName() { int length = AssetDatabase.GetAllAssetBundleNames ().Length; Debug.Log (length); string[] oldAssetBundleNames = new string[length]; for (int i = 0; i < length; i++) { oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i]; } for (int j = 0; j < oldAssetBundleNames.Length; j++) { AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j],true); } length = AssetDatabase.GetAllAssetBundleNames ().Length; Debug.Log (length); } static void Pack(string source) { DirectoryInfo folder = new DirectoryInfo (source); FileSystemInfo[] files = folder.GetFileSystemInfos (); int length = files.Length; for (int i = 0; i < length; i++) { if(files[i] is DirectoryInfo) { Pack(files[i].FullName); } else { if(!files[i].Name.EndsWith(".meta")) { file (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); //在代码中给资源设置AssetBundleName AssetImporter assetImporter = AssetImporter.GetAtPath (_assetPath); string assetName = _assetPath2.Substring (_assetPath2.IndexOf("/") + 1); assetName = assetName.Replace(Path.GetExtension(assetName),".unity3d"); //Debug.Log (assetName); assetImporter.assetBundleName = assetName; } static string Replace(string s) { return s.Replace("\\","/"); } } 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: return null; } } }
相关文章推荐
- Unity新项目如何快速理清顶层代码结构
- Unity3D 物体跟随鼠标旋转
- unity MD5加密
- Unity中的场景切换
- unity获取物体下的所有子物体
- Unity3D手游开发中所有特殊的文件夹
- 【Unity3D技巧】在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信 (二) : 引入中间层NotificationCenter
- Unity3D问题之EnhanceScollView选择角色3D循环滚动效果实现
- unity2d之2d帧动画创建
- unity工具IGamesTools之批量生成帧动画
- unity Game Center iOS 认证失败social.authenticate
- 一个伟大的发现,装X一下。笔记本win7系统64位机器运行unity 时,屏幕模糊解决办法
- Unity基础知识总结
- Unity3D动态字体破碎问题的解决方案
- Unity3D研究院之IOS&Android收集Log文件(六十二)
- unity 内置的CG结构解析
- 全面理解 Unity UI 系统
- Unity3D UGUI之fbx模型导入问题
- Unity Shader 2
- Unity对象池试水