UNITY编辑器模式下static变量的坑
在unity中写编辑器扩展工具,如在编辑器中加个菜单,点击这个菜单项时执行打包功能。
类如下,其中的静态变量,如果每次进来不清空,则LIST会越来越大,打包函数执行完后系统不会帮我们清空
#if UNITY_EDITOR using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; using LitJson; using System.Security.Cryptography; //资源清单,列出了游戏中用到的所有资源 [Serializable] public class AssetsList { /*** 资源清单 * <相对路径, MD5串> */ public Dictionary<string, string> Assets2Md5 = new Dictionary<string, string>();//容量未设置,后面可以写成常量,根据上次数据得出 } public class AssetBundleBuilder { public static string BundlePath = ""; public static AssetsList AssetsList = new AssetsList(); public static List<string> DependAssets_PathList = new List<string>(8000); //InGameRes所依赖的ArtSource等文件夹下的资源 public static List<string> Assets_PathList = new List<string>(8000); //InGameRes文件夹下的资源 public static List<AssetBundleBuild> BuildList = new List<AssetBundleBuild>(18000); //估计值,后面可以写成常量,根据上次数据得出 public static string ResRootPath = "Assets/Resources"; //资源目录,根据此目录查找所有依赖的资源 public static string ResDependPath = "Assets/ArtSource"; //资源目录,根据此目录查找所有依赖的资源 public static string ResFolder = "Resources"; //资源打包的依据文件夹,此文件夹下的所有文件及所有依赖文件都被打进AB包,不要在此文件夹下放备份或无用的资源 public static string Depends_List_File = "Assets/depends_list.json"; public static string Assets_List_File = "Assets/assets_list.json"; public static int FileNumLimit = 10; //测试使用,只收集限定数量的文件 public static void BuildAB(BuildTarget buildTarget) { if (!Directory.Exists(ResRootPath)) { Debug.LogError("资源目录不存在:" + ResRootPath); return; } var files = Directory.GetFiles(ResRootPath, "*", SearchOption.AllDirectories); var stopwt = System.Diagnostics.Stopwatch.StartNew(); var t1 = stopwt.ElapsedMilliseconds; /*** * 坑,这里必须要清空,否则每次进来就会往Assets_PathList里添加一次内容 */ BuildList.Clear(); Assets_PathList.Clear(); DependAssets_PathList.Clear(); AssetsList.Assets2Md5.Clear(); var infoTitle = "收集文件,目录 : " + ResRootPath; //第一步,将ResRootPath目录下的文件收集起来,此目录的东西都是游戏中要用到的,此目录之外的东西不一定会用到 var idx = 0; for (