Unty5.x里Assetbundle的自动标记和打包
来源:互联网 发布:淘宝节点处罚考试答案 编辑:程序博客网 时间:2024/05/25 08:15
最近研究了一下assetbundle的自动标记和打包,参考了很多网上教程写了一篇代码,废话不多说,直接看代码把
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor;using System.IO;public class AssetBundleEditor{ #region 自动做标记 //思路 //1.找到资源保存的文件夹 //2.遍历里面每个场景文件夹 //3.遍历场景文件夹里的所有文件系统 //4.如果访问的是文件夹:继续访问里面所有的文件系统,直到找到 文件(递归) //5.找到文件,修改他的 assetbundle labels //6.用 AssetImporter 类 修改名称和后缀 //7.保存对应的文件夹名和具体路径 [MenuItem("AssetBundle/Set AssetBundle Labels")] public static void SetAssetBundleLabels() { //移除所有没有使用的标记 AssetDatabase.RemoveUnusedAssetBundleNames(); //1.找到资源保存的文件夹 string assetDirectory = Application.dataPath + "/Res";//"D:/WorkSpace/UnityWorkSpace/MobaClient/AssetBundles/Assets/Res"; DirectoryInfo directoryInfo = new DirectoryInfo(assetDirectory); DirectoryInfo[] sceneDirectories = directoryInfo.GetDirectories(); //2.遍历里面每个场景文件夹 foreach (DirectoryInfo tempDirectoryInfo in sceneDirectories) { string sceneDirectory = assetDirectory + "/" + tempDirectoryInfo.Name; DirectoryInfo sceneDirectoryInfo = new DirectoryInfo(sceneDirectory); //错误检测 if (sceneDirectoryInfo == null) { Debug.LogError(sceneDirectory + "不存在"); return; } else { Dictionary<string, string> namePathDict = new Dictionary<string, string>(); //3.遍历场景文件夹里的所有文件系统 //D:\WorkSpace\UnityWorkSpace\MobaClient\AssetBundles\Assets\Res //D:/WorkSpace/UnityWorkSpace/MobaClient/AssetBundles/Assets/Res int index = sceneDirectory.LastIndexOf("/"); string sceneName = sceneDirectory.Substring(index + 1); onSceneFileSystemInfo(sceneDirectoryInfo, sceneName, namePathDict); onWriteConfig(sceneName, namePathDict); } }//end foreach AssetDatabase.Refresh(); Debug.LogWarning("设置成功"); }//end set /// <summary> /// 记录配置文件 /// </summary> private static void onWriteConfig(string sceneName, Dictionary<string, string> namePathDict) { string path = PathUtil.GetAssetBundleOutPath()+ "/" + sceneName + "Record.txt"; using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine(namePathDict.Count); foreach (KeyValuePair<string, string> kv in namePathDict) sw.WriteLine(kv.Key + " " + kv.Value); } } } private static void onSceneFileSystemInfo(FileSystemInfo fileSystemInfo, string sceneName, Dictionary<string, string> namePathDict) { if (!fileSystemInfo.Exists) { Debug.LogError(fileSystemInfo.FullName + "不存在"); return; } DirectoryInfo directoryInfo = fileSystemInfo as DirectoryInfo; FileSystemInfo[] fileSystemInfos = directoryInfo.GetFileSystemInfos(); foreach (var tempfileInfo in fileSystemInfos) { FileInfo fileInfo = tempfileInfo as FileInfo; if (fileInfo == null) { //代表强转失败,不是文件 就是文件夹 //4.如果访问的是文件夹:继续访问里面所有的文件系统,直到找到 文件(递归) onSceneFileSystemInfo(tempfileInfo, sceneName, namePathDict); } else { //文件 //5.找到文件,修改他的 assetbundle labels setLables(fileInfo, sceneName, namePathDict); } } } /// <summary> /// 修改资源文件的 assetbundle labels /// </summary> /// <param name="fileInfo"></param> /// <param name="sceneName"></param> private static void setLables(FileInfo fileInfo, string sceneName, Dictionary<string, string> namePathDict) { //忽视unity自身生成的meta文件 if (fileInfo.Extension == ".meta") return; string bundleName = getBundleName(fileInfo, sceneName); int index = fileInfo.FullName.IndexOf("Assets"); string assetPath = fileInfo.FullName.Substring(index); //6.用 AssetImporter 类 修改名称和后缀 AssetImporter assetImporter = AssetImporter.GetAtPath(assetPath); assetImporter.assetBundleName = bundleName; if (fileInfo.Extension == ".unity") assetImporter.assetBundleVariant = "u3d"; else assetImporter.assetBundleVariant = "assetbundle"; //添加到字典 string folderName = ""; if (bundleName.Contains("/")) folderName = bundleName.Split('/')[1]; else folderName = bundleName; string bundlePath = assetImporter.assetBundleName + "." + assetImporter.assetBundleVariant; if (!namePathDict.ContainsKey(folderName)) namePathDict.Add(folderName, bundlePath); } /// <summary> /// 获取包名 /// </summary> private static string getBundleName(FileInfo fileInfo, string sceneName) { string windowPath = fileInfo.FullName; string unityPath = windowPath.Replace(@"\", "/"); int Index = unityPath.IndexOf(sceneName) + sceneName.Length; string bundlePath = unityPath.Substring(Index + 1); if (bundlePath.Contains("/")) { string[] temp = bundlePath.Split('/'); return sceneName + "/" + temp[0]; } else { return sceneName; } } #endregion #region 打包 [MenuItem("AssetBundle/Build AssetBundles")] static void BuildAssetBundles() { string outPath = PathUtil.GetAssetBundleOutPath(); BuildPipeline.BuildAssetBundles(outPath, 0, BuildTarget.StandaloneWindows64); } #endregion #region 删除 [MenuItem("AssetBundle/Delete All")] static void DeleteAssetBundle() { string outPath = PathUtil.GetAssetBundleOutPath(); Directory.Delete(outPath, true); File.Delete(outPath + ".meta"); AssetDatabase.Refresh(); } #endregion}
还有一篇获取路径的
using System.Collections;using System.Collections.Generic;using UnityEngine;using System.IO;/// <summary>/// 路径/// </summary>public class PathUtil{ /// <summary> /// 获取assetbundle的输出目录 /// </summary> /// <returns></returns> public static string GetAssetBundleOutPath() { string outPath = getPlatformPath() + "/" + getPlatformName(); if (!Directory.Exists(outPath)) Directory.CreateDirectory(outPath); return outPath; } /// <summary> /// 自动获取对应平台的路径 /// </summary> /// <returns></returns> private static string getPlatformPath() { switch (Application.platform) { case RuntimePlatform.WindowsPlayer: case RuntimePlatform.WindowsEditor: return Application.streamingAssetsPath; case RuntimePlatform.Android: return Application.persistentDataPath; default: return null; } } /// <summary> /// 获取对应平台名字 /// </summary> /// <returns></returns> private static string getPlatformName() { switch (Application.platform) { case RuntimePlatform.WindowsPlayer: case RuntimePlatform.WindowsEditor: return "Windows"; case RuntimePlatform.Android: return "Android"; default: return null; } } /// <summary> /// 获取www协议的路径 /// </summary> public static string GetWWWPath() { switch (Application.platform) { case RuntimePlatform.WindowsPlayer: case RuntimePlatform.WindowsEditor: return "file:///"+ GetAssetBundleOutPath(); case RuntimePlatform.Android: return "jar:file://" + GetAssetBundleOutPath(); default: return null; } }}
阅读全文
0 0
- Unty5.x里Assetbundle的自动标记和打包
- unity5.x assetbundle打包和加载
- Unity5.X AssetBundle打包
- AssetBundle自动打包工具
- Unity5.x 编辑器中 获取所有标记的 AssetBundle 文件
- Unity中AssetBundle的打包和加载
- Unity5.x AssetBundle打包和加载深入分析
- AssetBundle打包和加载
- 【Unity开发】Unity5.x版本的AssetBundle打包
- 关于Unity5.x版本AssetBundle打包Shader的问题
- Unity3d的Assetbundle打包
- 5.x的AssetBundle
- AssetBundle -- 打包和本地加载
- Unity5.X打包与加载AssetBundle
- Unity5.x shader打包AssetBundle总结
- Unity5.x shader打包AssetBundle总结
- unity5.X AssetBundle资源打包(一 )
- UIFramework之Unity4.x AssetBundle打包策略
- mac电脑安装wxPython2.8.12.1不成功怎么办,报错:Could not find a version that satisfies the requirement 2.8.12.1 (f
- 浏览器禁用cookie后php如何保持session会话-use_trans_sid机制
- HDU2709Sumsets整数分解为加数,递推公式
- 直接插入排序
- Akka(5): ConsistentHashing Router
- Unty5.x里Assetbundle的自动标记和打包
- 使用对象:GPS信号处理
- matlab中几个取整语句的区别
- PHP之浅谈cookie和session
- statusbar toolbar color
- C++中栈区 堆区 常量区
- C++ 函数
- 【学习OpenCV】CV库的调用机制
- 考研阶段的数据结构代码