当前位置:   article > 正文

【Unity3d基础】Unity3d中自动生成asset bundle name_unity 设置assetbundlw名字

unity 设置assetbundlw名字
引言:
在unity3d的实际项目开发中,由于资源的增删改频繁,手动设置asset bundle name是一项繁琐且容易出错的工作,所以衍生出了开发自动化工具的需求。
工作原理:
以文件夹为目标,设置asset bundle name,在目标文件夹内的文件都会打进同一个asset bundle。
实现原理:
通过遍历Asset路径下的所有文件夹,收集所有的子文件夹列表,然后顺序设置asset bundle name。命名规格是将路径中的"\"替换为"_"。其中对文件夹的操作用到System.IO下面的接口。
另外添加了黑名单配置列表,用于过滤无需设置asset bundle name的文件夹。(例如导入的插件等)
特别的,为了避免资源冗余,仅对最低层的文件夹设置asset bundle name。
源代码:
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. public class AutoGenerateAssetBundleName
  7. {
  8. /// <summary>
  9. /// 黑名单
  10. /// </summary>
  11. static public string[] skip_dirs_all = { "testBlackList" };
  12. [MenuItem("Asset Bundle/Auto generate asset bundle name", false, 1)]
  13. public static void GenerateAssetBundleName()
  14. {
  15. string dataPath = Application.dataPath;
  16. string root_path = "";
  17. string sub_dir = "";
  18. int position = dataPath.LastIndexOf(@"/");
  19. if (position != -1)
  20. {
  21. root_path = dataPath.Substring(0, position + 1);
  22. sub_dir = dataPath.Substring(position + 1);
  23. }
  24. ClearAssetBundleName(root_path, sub_dir);
  25. Debug.Log("Begin GenerateAssetBundleName...");
  26. List<string> OutPathList = new List<string>();
  27. List<string> IncludeOutPathList = new List<string>();
  28. OutPathList = GetFilterDirectories(@root_path, @sub_dir);
  29. foreach(string AssetBundleName in OutPathList)
  30. {
  31. AssetImporter importer = AssetImporter.GetAtPath(AssetBundleName);
  32. if (importer != null)
  33. {
  34. importer.assetBundleName = AssetBundleName.Replace('/', '_');
  35. importer.assetBundleVariant = "";
  36. }
  37. }
  38. AssetDatabase.Refresh();
  39. Debug.Log("End GenerateAssetBundleName... Total asset bundle count: " + OutPathList.Count);
  40. }
  41. static List<string> GetFilterDirectories(string root_path, string sub_dir, bool bNeedSkip = true)
  42. {
  43. //需要过滤的文件夹
  44. List<string> skip_dirs = new List<string>(skip_dirs_all);
  45. string sub_folder_name = sub_dir;
  46. int position = sub_dir.LastIndexOf(@"/");
  47. if (position != -1)
  48. sub_folder_name = sub_dir.Substring(position + 1);
  49. if (bNeedSkip)
  50. {
  51. bool bSkipit = false;
  52. foreach(string skip_dir in skip_dirs)
  53. {
  54. if (string.Compare(skip_dir, sub_folder_name, true) == 0)
  55. {
  56. bSkipit = true;
  57. break;
  58. }
  59. }
  60. if (bSkipit)
  61. {
  62. return new List<string>();
  63. }
  64. }
  65. string[] dirs = Directory.GetDirectories(root_path + sub_dir, "*", SearchOption.TopDirectoryOnly);
  66. // 提取文件夹名
  67. string[] folder_names = new string[dirs.Length];
  68. for (int i = 0; i < dirs.Length; ++i)
  69. {
  70. int pos = dirs[i].LastIndexOf(@"\");
  71. if (pos != -1)
  72. folder_names[i] = dirs[i].Substring(pos + 1);
  73. }
  74. List<string> filter_dirs = new List<string>();
  75. if (dirs.Length == 0)
  76. {
  77. //没有子目录
  78. //如果sub_folder_name不在skip_dirs内,则设置assetbundlename
  79. bool bFindit = false;
  80. foreach(string skip_dir in skip_dirs)
  81. {
  82. if (string.Compare(skip_dir, sub_folder_name, true) == 0)
  83. {
  84. bFindit = true;
  85. break;
  86. }
  87. }
  88. if (!bFindit)
  89. {
  90. Debug.Log("collect valid asset bundle name: " + sub_dir);
  91. filter_dirs.Add(sub_dir);
  92. }
  93. return filter_dirs;
  94. }
  95. foreach (string folder_name in folder_names)
  96. {
  97. List<string> sub_filter_dirs = GetFilterDirectories(root_path, sub_dir + "/" + folder_name);
  98. filter_dirs.AddRange(sub_filter_dirs);
  99. }
  100. return filter_dirs;
  101. }
  102. static void GetAllDirectories(string root_path, string sub_dir, ref List<string> outPathList)
  103. {
  104. string[] dirs = Directory.GetDirectories(root_path + sub_dir, "*", SearchOption.TopDirectoryOnly);
  105. outPathList.Add(sub_dir);
  106. if (dirs.Length > 0)
  107. {
  108. // 提取文件夹名
  109. string[] folder_names = new string[dirs.Length];
  110. for(int i = 0; i < dirs.Length; ++i)
  111. {
  112. int pos = dirs[i].LastIndexOf(@"\");
  113. if (pos != -1)
  114. folder_names[i] = dirs[i].Substring(pos + 1);
  115. }
  116. foreach(string folder_name in folder_names)
  117. {
  118. GetAllDirectories(root_path, sub_dir + "/" + folder_name, ref outPathList);
  119. }
  120. }
  121. }
  122. static void ClearAssetBundleName(string root_path, string sub_dir)
  123. {
  124. List<string> OutPathList = new List<string>();
  125. GetAllDirectories(@root_path, @sub_dir, ref OutPathList);
  126. foreach(string folder in OutPathList)
  127. {
  128. AssetImporter importer = AssetImporter.GetAtPath(folder);
  129. if (importer != null)
  130. {
  131. importer.assetBundleName = "";
  132. }
  133. }
  134. AssetDatabase.RemoveUnusedAssetBundleNames();
  135. AssetDatabase.Refresh();
  136. }
  137. }
运行结果:
运行的log输出

文件夹的asset bundle name:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/81935
推荐阅读
相关标签
  

闽ICP备14008679号