Toxic Star - Editor
type: channel
link: https://toxicstar.top/index.php/tag/Editor/
items: Unity编辑器脚本面板显示扩展
items: Unity文件打开和定位工具
items: Unity一键生成UI代码
items: Unity一键生成图集
items: Unity一键打AB包

Unity编辑器脚本面板显示扩展
type: item
date: 2023-06-09T14:14:00+08:00
description: /// /// typeof(PoolTest = 需要显示面板的类) /// [CustomEditor(typeof(PoolTest), true)] public class PoolAnalyze_ClassObjectPoolEditor : Editor { public override void OnInspectorGUI() { base.OnInspectorGUI(); GUILayout.BeginHorizontal("box"); GUILayout.Label("类名"); GUILayout.Label("池中数量", GUILayout.Width(50)); GUILayout.Label("常驻数量", GUILayout.Width(50)); GUILayout.EndHorizontal(); if (GameEntry.Pool.ClassObjectPool != null) { foreach (var item in GameEntry.Pool.ClassObjectPool.InspectorDic) { GUILayout.BeginHorizontal("box"); GUILayout.Label(item.Key); GUILayout.Label(item.Value.ToString(), GUILayout.Width(50)); GUILayout.Label("0", GUILayout.Width(50)); GUILayout.EndHorizontal(); } //重绘 Repaint(); } } }

Unity文件打开和定位工具
type: item
date: 2023-06-09T14:10:00+08:00
description: [tabs][tab name="快捷打开工具" active="true"]/********************************************* * YSFramework * 快捷打开工具 * 创建时间：2022/12/28 15:52:23 *********************************************/ using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Threading; using UnityEditor; using UnityEngine; namespace Framework { /// /// 打开文件/文件夹工具 /// public class OpenFileTools { /// /// 打开文件/文件夹方法 /// /// 文件路径 public static void OpenFile(string path) { Thread newThread = new Thread(new ParameterizedThreadStart(CmdOpenDirectory)); newThread.Start(path); } /// /// 真正打开文件/文件夹的方法 /// private static void CmdOpenDirectory(object obj) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.Arguments = "/c start " + obj.ToString(); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); p.WaitForExit(); p.Close(); } [MenuItem("GameObject/打开UI代码", false, 10001)] public static void OpenUIScript() { //选择的对象 var go = Selection.activeGameObject; //脚本路径 var uiRootPath = Application.dataPath + "/GameData/Scripts/UI/"; //文件夹信息 var dirInfo = new DirectoryInfo(uiRootPath); string targetName = string.Empty; //UI打开方式 if (go.name.StartsWith("UI")) { //直接就是脚本名 targetName = go.name.Replace("(Clone)", "") + ".cs"; } //Unit打开方式 if (go.name.EndsWith("Unit")) { var goName = go.name.Replace("(Clone)", "") + ".cs"; //判断是否为独立Unit var parentGo = go.transform.parent; if (parentGo != null && !parentGo.name.StartsWith("Canvas")) { while (!parentGo.name.StartsWith("UI")) { parentGo = parentGo.parent; } //UI名+下划线+Unit名 targetName = parentGo.name + "_" + goName; } else { //默认名+下划线+Unit名 targetName = "Main_" + goName; } } //遍历脚本文件 foreach (var file in dirInfo.GetFileSystemInfos("*.cs", SearchOption.AllDirectories)) { //找到了直接打开 if (file.Name.Equals(targetName)) { OpenFile(file.FullName); UnityEn…

Unity一键生成UI代码
type: item
date: 2023-06-09T11:54:00+08:00
description: [tabs][tab name="第一版" active="true"]using System; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; using UnityEngine.UI; using YouYou; namespace JiangHu.Tools { public class CreateUIScriptTool { [MenuItem("Assets/生成UI脚本",false,10000)] //注释看下面Cell public static void CreateUIScript() { var gameObject = Selection.activeGameObject; if (gameObject == null) { GameEntry.LogError("请选中预制体！"); return; } if (!gameObject.name.StartsWith("UI")) { GameEntry.LogError("自动生成代码必须是以UI开头的UI预制体！"); return; } string templateDesignPath = "/Game/YouYouScript/Editor/UITemplateDesign.txt"; string templatePath = "/Game/YouYouScript/Editor/UITemplate.txt"; string prefabPath = "Assets/Download/UI/UIPrefab"; var directoryInfo = new DirectoryInfo(prefabPath); List<GameObject> needBindList = new List<GameObject>(); foreach(var component in gameObject.GetComponentsInChildren<UIBind>()) { needBindList.Add(component.gameObject); } foreach (var file in directoryInfo.GetFileSystemInfos("*", SearchOption.AllDirectories)) { if (file.Extension.Equals(".prefab")) { string name = file.Name.Substring(0, gameObject.name.Length); if (name == gameObject.name) { string scriptDirName = file.FullName.Substring(Application.dataPath.Length + prefabPath.Length - 5); scriptDirName = scriptDirName.Substring(0, scriptDirName.Length - file.Name.Length - 1); string scriptPath = Application.dataPath + "/Game/YouYouScript/UI/" + scriptDirName; if (!Directory.Exists(scriptPath)) { Directory.CreateDirectory(scriptPath); Debug.Log("文件夹创建成功！"); } string scriptStr = GetTemplateDesignText(name, needBindList, templateDesignPath); using (FileStream fs = new FileStream(string.Format("{0}/{1}.Design.cs", scriptPath, name), FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(scriptStr.ToString()); Debug.Log("绑定关系生成成功！"); } } string scriptName = scriptPath + "/" + name + ".cs"; if (!File.Exists(scriptName)) { scriptStr = GetTemplateText(name, needBindList, templatePath);…

Unity一键生成图集
type: item
date: 2023-06-09T11:53:00+08:00
description: 纯代码 没什么好说的using System.IO; using UnityEditor; using UnityEditor.U2D; using UnityEngine; using UnityEngine.U2D; namespace Framework { /// /// 图集生成 /// public class BuildSpriteAtlas : Editor { /// /// 图集磁盘路径 /// public static string SpritePath = Application.dataPath + "/GameData/Art/Sprite"; [MenuItem("YSFramework/Build SpriteAtlas")] public static void BuildAtlas() { Debug.Log("Start Build SpriteAtlas Path=" + SpritePath); var pathDir = new DirectoryInfo(SpritePath); //循环目录下的文件和子目录 但是不包含子目录下的文件和子目录 foreach (var item in pathDir.GetFileSystemInfos("*.*", SearchOption.TopDirectoryOnly)) { var parentDir = item as DirectoryInfo; //空值就代表它是文件 if (parentDir != null) { var file = new FileInfo(parentDir.FullName + "/" + parentDir.Name + ".spriteatlas"); if (file.Exists) { File.Delete(file.FullName); } var atlas = new SpriteAtlas();// 设置参数 可根据项目具体情况进行设置 SpriteAtlasPackingSettings packSetting = new SpriteAtlasPackingSettings() { blockOffset = 1, enableRotation = false, enableTightPacking = false, padding = 2, }; atlas.SetPackingSettings(packSetting); SpriteAtlasTextureSettings textureSetting = new SpriteAtlasTextureSettings() { readable = false, generateMipMaps = false, sRGB = true, filterMode = FilterMode.Bilinear, }; atlas.SetTextureSettings(textureSetting); TextureImporterPlatformSettings platformSetting = new TextureImporterPlatformSettings() { maxTextureSize = 2048, format = TextureImporterFormat.Automatic, crunchedCompression = true, textureCompression = TextureImporterCompression.Compressed, compressionQuality = 50, }; atlas.SetPlatformSettings(platformSetting); var assetPath = "Assets/GameData/Art/Sprite/"; AssetDatabase.CreateAsset(atlas, assetPath + parentDir.Name + "/" + file.Name); // 2、添加文件夹 Object obj = AssetDatabase.LoadAssetAtPath(assetPath + parentDir.Name, typeof(Object)); atlas.Add(new[] { obj }); AssetDatabase.SaveAssets(); } } Debug.Log("Build SpriteAtlas Success!"); //回收资源 System.GC.Collect(); //刷新编辑器 AssetDatabase.Refresh(); } } }

Unity一键打AB包
type: item
date: 2023-06-09T11:51:00+08:00
description: [tabs][tab name="ABConfig：打包配置" active="true"]using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; namespace Framework { // 创建资源AB配置菜单 [CreateAssetMenu(fileName = "ABConfig", menuName = "CreateABConfig", order = 999)] public class ABConfig : ScriptableObject { /// /// 需要打包的文件夹路径，会遍历这个文件夹下的所有文件和文件夹， /// 该目录下的所有文件名和文件夹名不能重复，必须保证名字的唯一性。 /// 直接定位到父目录，用子文件夹名当ab包名，如果根目录有文件用父目录单独打包。 /// [Header("AB包配置根目录（根目录下所有文件夹全部分开打包）")] public List<string> RootABList = new List<string>(); [Header("真正的AB包打包列表(自动生成)")] public List<ABData> TrueABList = new List<ABData>(); [System.Serializable] public class ABData { public string path; public string abName; // ab包名 //public bool must; } /// /// 设置打包名和路径 /// public List<ABData> SetABNameAndPath() { //所有AB包的路径 AB包路径,AB包名 var allABPathList = new List<KeyValuePair<string, string>>(); //需要删除Assets目录之前的Length int needDeleteLength = Application.dataPath.Length - "Assets".Length; //遍历输入的目录 for (int i = 0; i < RootABList.Count; i++) { var rootHasFile = false; var path = RootABList[i]; var pathDir = new DirectoryInfo(path); //循环目录下的文件和子目录 但是不包含子目录下的文件和子目录 foreach (var item in pathDir.GetFileSystemInfos("*.*", SearchOption.TopDirectoryOnly)) { var childDir = item as DirectoryInfo; //空值就代表它是文件 if (childDir == null) { //场景需要单独打包 if (item.Extension == ".unity") { allABPathList.Add(new KeyValuePair<string, string>(item.FullName.Substring(needDeleteLength).Replace("\\", "/"), item.Name.Replace(".unity",""))); } //跳过meta文件 else if (item.Extension != ".meta") { //根目录下也有文件 说明当前路径也需要单独打个包，注意 不要包含其它子文件夹下的文件 rootHasFile = true; } } else //文件夹 { allABPathList.Add(new KeyValuePair<string, string>(item.FullName.Substring(needDeleteLength).Replace("\\", "/"), pathDir.Name + childDir.Name)); } } // 只有在根目录下有文件的时候才会打包根目录 if (rootHasFile) { allABPathList.Add(new KeyValuePair<string, string>(path, pathDir.Name)); } } TrueABList.Clear(); for (int i = 0; i < allABPathList.Count; i++) { var kv = allABPathList[i]; var…