// foreach (var name in names) // { // if(name.GetAfterLastChar('/')=="self") // continue; // otherClassBody += "\t\tpublic const string "+name.Trim().GetAfterLastChar('/')+" = " + // "Path.Combine(Application.streamingAssetsPath, @\""+ name.Trim() +"\");\n"; // } /// <summary> /// 创建场景名文件 /// </summary> /// <param name="rootNs"></param> /// <param name="constNs"></param> /// <param name="autoDirName"></param> private static void CreateSceneNameClass(string rootNs, string constNs, string autoDirName) { var dic = new Dictionary <string, string>(); foreach (EditorBuildSettingsScene S in EditorBuildSettings.scenes) { var path = S.path; var name = Path.GetFileNameWithoutExtension(path); dic.Add(name, name); } FileUtil.CreateConstClassByDictionary( "SceneName", Application.dataPath + "/" + rootNs + "/" + constNs + "/" + autoDirName, rootNs + "." + constNs, dic); }
static void OnToolsGUI(string rootNs, string viewNs, string constNs, string dataNs, string autoDir, string scriptDir) { EditorGUILayout.Space(); _resourceManagerType = (ResourceManagerType)EditorGUILayout.EnumPopup("资源加载类型", _resourceManagerType); switch (_resourceManagerType) { #region ResourceManagerType.Resource case ResourceManagerType.Resource: EditorGUILayout.LabelField("自动常量文件生成目录", Application.dataPath + "/" + rootNs + "/" + constNs + "/" + autoDir); EditorGUILayout.LabelField("常量工具类生成目录", Application.dataPath + "/" + rootNs + "/Utility/" + autoDir + "/ConstUtil.cs"); EditorGUILayout.Space(); createPathFile = EditorGUILayout.Toggle("是否生成资源路径文件", createPathFile); EditorGUILayout.Space(); if (GUILayout.Button("生成常量")) { #region 遍历Resources生成常量文件 var resourceDir = Application.dataPath + "/Resources"; var rootDir = Application.dataPath + "/" + rootNs; autoClassName.Clear(); otherResPathDic.Clear(); otherResFileNameDic.Clear(); allResPathDic.Clear(); allResFileNameDic.Clear(); foreach (var fullName in Directory.GetFileSystemEntries(resourceDir)) { // Debug.Log(fullName); if (Directory.Exists(fullName)) { //如果是文件夹 OprateDir(new DirectoryInfo(fullName), rootNs, constNs, autoDir); } else { //是文件 OprateFile(new FileInfo(fullName)); } } //生成其他常量文件 if (otherResPathDic.Count > 0) { // Debug.Log("创建文件OtherResPath"); FileUtil.CreateConstClassByDictionary("OtherResPath", rootDir + "/" + constNs + "/" + autoDir, rootNs + "." + constNs, otherResPathDic); FileUtil.CreateConstClassByDictionary("OtherResName", rootDir + "/" + constNs + "/" + autoDir, rootNs + "." + constNs, otherResFileNameDic); autoClassName.Add("OtherRes"); } //生成常量工具类 if (allResPathDic.Count > 0) { var content = "\t\tprivate System.Collections.Generic.Dictionary<string,string> nameToPath \n" + "\t\t\t= new System.Collections.Generic.Dictionary<string,string>{\n"; foreach (var kv in allResFileNameDic) { content += "\t\t\t\t\t{ @\"" + kv.Value + "\" , @\"" + allResPathDic[kv.Key] + "\" },\n"; } content += "\t\t\t\t};\n"; content += "\t\tpublic override System.Collections.Generic.Dictionary<string,string> NameToPath => nameToPath;\n"; FileUtil.CreateClassFile("AssetConstUtil", rootNs + ".Utility", rootDir + "/Utility/" + autoDir, parentClass: "ReadyGamerOne.MemorySystem.AssetConstUtil<AssetConstUtil>", helpTips: "这个类提供了Resources下文件名和路径字典访问方式,同名资源可能引起bug", fileContent: content, autoOverwrite: true); } AssetDatabase.Refresh(); Debug.Log("生成结束"); #endregion } break; #endregion #region ResourceManagerType.AssetBundle case ResourceManagerType.AssetBundle: var newestVersion = PlayerPrefs.GetString(VersionDefine.PrefKey_LocalVersion, "0.0"); if (GUILayout.Button("显示本地版本")) { var versionData = PlayerPrefs.GetString(newestVersion); Debug.Log("本地版本号:" + newestVersion + "\n版本信息:" + versionData); } EditorGUI.BeginChangeCheck(); newestVersion = EditorGUILayout.DelayedTextField("本地版本", newestVersion); if (EditorGUI.EndChangeCheck()) { PlayerPrefs.SetString(VersionDefine.PrefKey_LocalVersion, newestVersion); } EditorGUILayout.Space(); EditorGUILayout.LabelField("打包目录", assetToBundleDir); if (GUILayout.Button("设置要打包的目录")) { assetToBundleDir = EditorUtility.OpenFolderPanel("选择需要打包的目录", Application.dataPath, ""); } EditorGUILayout.Space(); EditorGUILayout.LabelField("输出目录", outputDir); if (GUILayout.Button("设置输出目录")) { outputDir = EditorUtility.OpenFolderPanel("选择输出目录", Application.dataPath, ""); } EditorGUILayout.Space(); assetBundleOptions = (BuildAssetBundleOptions)EditorGUILayout.EnumFlagsField("打包选项", assetBundleOptions); buildTarget = (BuildTarget)EditorGUILayout.EnumPopup("目标平台", buildTarget); clearOutputDir = EditorGUILayout.Toggle("清空生成目录", clearOutputDir); EditorGUILayout.Space(); useForRuntime = EditorGUILayout.Foldout(useForRuntime, "使用用于生成运行时直接使用的AB包"); if (useForRuntime) { EditorGUILayout.LabelField("生成HotUpdatePath.cs路径", Application.dataPath + "/" + rootNs + "/" + constNs + "/" + autoDir + "/HotUpdatePath.cs"); useWeb = EditorGUILayout.Toggle("是否使用网络", useWeb); EditorGUILayout.LabelField("游戏自身AB包名字", streamingAbName); EditorGUILayout.Space(); if (GUILayout.Button("重新生成常量类【会覆盖】")) { if (!outputDir.Contains(Application.streamingAssetsPath)) { Debug.LogError("运行时使用的AB包必须在StreamingAssets目录下"); return; } if (assetBundleNames.Count == 0) { Debug.LogError("AB包数组未空"); return; } CreatePathDataClass(rootNs, constNs, autoDir, assetBundleNames); AssetDatabase.Refresh(); Debug.Log("生成完成"); } } if (GUILayout.Button("开始打包", GUILayout.Height(3 * EditorGUIUtility.singleLineHeight))) { if (createPathDataClass) { if (!outputDir.Contains(Application.streamingAssetsPath)) { Debug.LogError("运行时使用的AB包必须在StreamingAssets目录下"); return; } } if (assetBundleNames.Count != 0) { assetBundleNames.Clear(); } if (!Directory.Exists(assetToBundleDir)) { Debug.LogError("打包目录设置异常"); return; } if (!Directory.Exists(outputDir)) { Debug.LogError("输出目录设置异常"); return; } if (clearOutputDir) { if (Directory.Exists(outputDir)) { //获取指定路径下所有文件夹 string[] folderPaths = Directory.GetDirectories(outputDir); foreach (string folderPath in folderPaths) { Directory.Delete(folderPath, true); } //获取指定路径下所有文件 string[] filePaths = Directory.GetFiles(outputDir); foreach (string filePath in filePaths) { File.Delete(filePath); } } } var builds = new List <AssetBundleBuild>(); foreach (var dirPath in System.IO.Directory.GetDirectories(assetToBundleDir)) { var dirName = new DirectoryInfo(dirPath).Name; var paths = new List <string>(); var assetNames = new List <string>(); FileUtil.SearchDirectory(dirPath, fileInfo => { if (fileInfo.Name.EndsWith(".meta")) { return; } var pureName = Path.GetFileNameWithoutExtension(fileInfo.FullName); assetNames.Add(pureName); var fileLoadPath = fileInfo.FullName.Replace("\\", "/") .Replace(Application.dataPath, "Assets"); var ai = AssetImporter.GetAtPath(fileLoadPath); ai.assetBundleName = dirName; paths.Add(fileLoadPath); }, true); assetBundleNames.Add(dirName); FileUtil.CreateConstClassByDictionary( dirName + "Name", Application.dataPath + "/" + rootNs + "/" + constNs, rootNs + "." + constNs, assetNames.ToDictionary(name => name)); builds.Add(new AssetBundleBuild { assetNames = paths.ToArray(), assetBundleName = dirName }); } if (createPathDataClass) { CreatePathDataClass(rootNs, constNs, autoDir, assetBundleNames); } BuildPipeline.BuildAssetBundles(outputDir, builds.ToArray(), assetBundleOptions, buildTarget); AssetDatabase.Refresh(); Debug.Log("打包完成"); } break; #endregion } }
static void OnToolsGUI(string rootNs, string viewNs, string constNs, string dataNs, string autoDir, string scriptDir) { EditorGUILayout.Space(); _resourceManagerType = (ResourceManagerType)EditorGUILayout.EnumPopup("资源加载类型", _resourceManagerType); switch (_resourceManagerType) { #region ResourceManagerType.Resource case ResourceManagerType.Resource: EditorGUILayout.LabelField("自动常量文件生成目录", Application.dataPath + "/" + rootNs + "/" + constNs + "/" + autoDir); EditorGUILayout.LabelField("常量工具类生成目录", Application.dataPath + "/" + rootNs + "/Utility/" + autoDir + "/ConstUtil.cs"); EditorGUILayout.Space(); createPathFile = EditorGUILayout.Toggle("是否生成资源路径文件", createPathFile); EditorGUILayout.Space(); if (GUILayout.Button("生成常量")) { GenerateResourcesConst(rootNs, constNs, autoDir); } break; #endregion #region ResourceManagerType.AssetBundle case ResourceManagerType.AssetBundle: var newestVersion = PlayerPrefs.GetString(VersionDefine.PrefKey_LocalVersion, "0.0"); if (GUILayout.Button("显示本地版本")) { var versionData = PlayerPrefs.GetString(newestVersion); Debug.Log("本地版本号:" + newestVersion + "\n版本信息:" + versionData); } EditorGUI.BeginChangeCheck(); newestVersion = EditorGUILayout.DelayedTextField("本地版本", newestVersion); if (EditorGUI.EndChangeCheck()) { PlayerPrefs.SetString(VersionDefine.PrefKey_LocalVersion, newestVersion); } EditorGUILayout.Space(); EditorGUILayout.LabelField("打包目录", assetToBundleDir); if (GUILayout.Button("设置要打包的目录")) { assetToBundleDir = EditorUtility.OpenFolderPanel("选择需要打包的目录", Application.dataPath, ""); } EditorGUILayout.Space(); EditorGUILayout.LabelField("输出目录", outputDir); if (GUILayout.Button("设置输出目录")) { outputDir = EditorUtility.OpenFolderPanel("选择输出目录", Application.dataPath, ""); } EditorGUILayout.Space(); assetBundleOptions = (BuildAssetBundleOptions)EditorGUILayout.EnumFlagsField("打包选项", assetBundleOptions); buildTarget = (BuildTarget)EditorGUILayout.EnumPopup("目标平台", buildTarget); clearOutputDir = EditorGUILayout.Toggle("清空生成目录", clearOutputDir); EditorGUILayout.Space(); useForRuntime = EditorGUILayout.Foldout(useForRuntime, "使用用于生成运行时直接使用的AB包"); if (useForRuntime) { EditorGUILayout.LabelField("生成HotUpdatePath.cs路径", Application.dataPath + "/" + rootNs + "/" + constNs + "/" + autoDir + "/HotUpdatePath.cs"); useWeb = EditorGUILayout.Toggle("是否使用网络", useWeb); EditorGUILayout.LabelField("游戏自身AB包名字", streamingAbName); EditorGUILayout.Space(); if (GUILayout.Button("重新生成常量类【会覆盖】")) { if (!outputDir.Contains(Application.streamingAssetsPath)) { Debug.LogError("运行时使用的AB包必须在StreamingAssets目录下"); return; } if (assetBundleNames.Count == 0) { Debug.LogError("AB包数组未空"); return; } CreatePathDataClass(rootNs, constNs, autoDir, assetBundleNames); AssetDatabase.Refresh(); Debug.Log("生成完成"); } } if (GUILayout.Button("开始打包", GUILayout.Height(3 * EditorGUIUtility.singleLineHeight))) { if (createPathDataClass) { if (!outputDir.Contains(Application.streamingAssetsPath)) { Debug.LogError("运行时使用的AB包必须在StreamingAssets目录下"); return; } } if (assetBundleNames.Count != 0) { assetBundleNames.Clear(); } if (!Directory.Exists(assetToBundleDir)) { Debug.LogError("打包目录设置异常"); return; } if (!Directory.Exists(outputDir)) { Debug.LogError("输出目录设置异常"); return; } if (clearOutputDir) { if (Directory.Exists(outputDir)) { //获取指定路径下所有文件夹 string[] folderPaths = Directory.GetDirectories(outputDir); foreach (string folderPath in folderPaths) { Directory.Delete(folderPath, true); } //获取指定路径下所有文件 string[] filePaths = Directory.GetFiles(outputDir); foreach (string filePath in filePaths) { File.Delete(filePath); } } } var builds = new List <AssetBundleBuild>(); foreach (var dirPath in System.IO.Directory.GetDirectories(assetToBundleDir)) { var dirName = new DirectoryInfo(dirPath).Name; var paths = new List <string>(); var assetNames = new List <string>(); FileUtil.SearchDirectory(dirPath, fileInfo => { if (fileInfo.Name.EndsWith(".meta")) { return; } var pureName = Path.GetFileNameWithoutExtension(fileInfo.FullName); assetNames.Add(pureName); var fileLoadPath = fileInfo.FullName.Replace("\\", "/") .Replace(Application.dataPath, "Assets"); var ai = AssetImporter.GetAtPath(fileLoadPath); ai.assetBundleName = dirName; paths.Add(fileLoadPath); }, true); assetBundleNames.Add(dirName); FileUtil.CreateConstClassByDictionary( dirName + "Name", Application.dataPath + "/" + rootNs + "/" + constNs, rootNs + "." + constNs, assetNames.ToDictionary(name => name)); builds.Add(new AssetBundleBuild { assetNames = paths.ToArray(), assetBundleName = dirName }); } if (createPathDataClass) { CreatePathDataClass(rootNs, constNs, autoDir, assetBundleNames); } BuildPipeline.BuildAssetBundles(outputDir, builds.ToArray(), assetBundleOptions, buildTarget); AssetDatabase.Refresh(); Debug.Log("打包完成"); } break; #endregion } }
public static void GenerateResourcesConst(string rootNs, string constNs, string autoDir) { var resourceDir = Application.dataPath + "/Resources"; var rootDir = Application.dataPath + "/" + rootNs; autoClassName.Clear(); otherResPathDic.Clear(); otherResFileNameDic.Clear(); allResPathDic.Clear(); allResFileNameDic.Clear(); foreach (var fullName in Directory.GetFileSystemEntries(resourceDir)) { // Debug.Log(fullName); if (Directory.Exists(fullName)) { //如果是文件夹 OprateDir(new DirectoryInfo(fullName), rootNs, constNs, autoDir); } else { //是文件 OprateFile(new FileInfo(fullName)); } } //生成其他常量文件 if (otherResPathDic.Count > 0) { // Debug.Log("创建文件OtherResPath"); FileUtil.CreateConstClassByDictionary("OtherResPath", rootDir + "/" + constNs + "/" + autoDir, rootNs + "." + constNs, otherResPathDic); FileUtil.CreateConstClassByDictionary("OtherResName", rootDir + "/" + constNs + "/" + autoDir, rootNs + "." + constNs, otherResFileNameDic); autoClassName.Add("OtherRes"); } //生成常量工具类 if (allResPathDic.Count > 0) { var content = "\t\tprivate System.Collections.Generic.Dictionary<string,string> nameToPath \n" + "\t\t\t= new System.Collections.Generic.Dictionary<string,string>{\n"; foreach (var kv in allResFileNameDic) { content += "\t\t\t\t\t{ @\"" + kv.Value + "\" , @\"" + allResPathDic[kv.Key] + "\" },\n"; } content += "\t\t\t\t};\n"; content += "\t\tpublic override System.Collections.Generic.Dictionary<string,string> NameToPath => nameToPath;\n"; FileUtil.CreateClassFile("AssetConstUtil", rootNs + ".Utility", rootDir + "/Utility/" + autoDir, parentClass: "ReadyGamerOne.MemorySystem.AssetConstUtil<AssetConstUtil>", helpTips: "这个类提供了Resources下文件名和路径字典访问方式,同名资源可能引起bug", fileContent: content, autoOverwrite: true); } AssetDatabase.Refresh(); Debug.Log("生成结束"); }
static void OnToolsGUI(string rootNs, string viewNs, string constNs, string dataNs, string autoDir, string scriptNs) { var resources = Application.dataPath + "/Resources"; EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.LabelField("自动常量文件生成目录", Application.dataPath + "/" + rootNs + "/" + constNs + "/" + autoDir); EditorGUILayout.Space(); createPanelClasses = EditorGUILayout.Toggle("是否自动生成Panel类型", createPanelClasses); if (createPanelClasses) { EditorGUILayout.LabelField("自动生成Panel文件路径", Application.dataPath + "/" + rootNs + "/" + viewNs); } EditorGUILayout.Space(); createGameMgr = EditorGUILayout.Toggle("是否生成GameMgr", createGameMgr); if (createGameMgr) { EditorGUILayout.LabelField("自动生成" + rootNs + "Mgr文件路径", Application.dataPath + "/" + rootNs + "/" + scriptNs); } EditorGUILayout.Space(); if (GUILayout.Button("开启自动生成", GUILayout.Height(2 * EditorGUIUtility.singleLineHeight))) { autoClassName.Clear(); var rootDir = Application.dataPath + "/" + rootNs; FileUtil.CreateFolder(rootDir); FileUtil.CreateFolder(rootDir + "/" + constNs); FileUtil.CreateFolder(rootDir + "/" + constNs + "/" + autoDir); nameToPath.Clear(); if (!Directory.Exists(resources)) { Directory.CreateDirectory(resources); return; } FileUtil.SearchDirectory(resources, OprateFile, false, filPath => OprateDir(filPath, rootNs, constNs, autoDir) ); if (nameToPath.Count > 0) { FileUtil.CreateConstClassByDictionary("OtherResPath", rootDir + "/" + constNs + "/" + autoDir, rootNs + "." + constNs, nameToPath); FileUtil.CreateConstClassByDictionary("OtherResName", rootDir + "/" + constNs + "/" + autoDir, rootNs + "." + constNs, nameToPath.Select(pair => pair.Key).ToDictionary(name => name)); autoClassName.Add("OtherRes"); } if (autoClassName.Contains("Panel")) { CreatePanelFile(Application.dataPath + "/Resources/ClassPanel", viewNs, constNs, rootNs, autoDir); } if (createGameMgr) { CreateMgr(rootNs, constNs, scriptNs, autoDir); } AssetDatabase.Refresh(); Debug.Log("生成结束"); } EditorGUILayout.Space(); if (autoClassName.Count > 0) { var str = "生成的类型有:"; foreach (var name in autoClassName) { str += "\n" + name + "Name" + "\t\t" + name + "Path"; } EditorGUILayout.HelpBox(str, MessageType.Info); } }