编辑器扩展
编辑骑扩展,之EditorWindow ,改变一下前面说的assetbundle打包脚本,我们让他弹出来一个弹窗,在这里选择各个选项,先来看一下实例,看了网上其他人的一些,还有自己琢磨的,EditorGUILayout.XXXX() ,有多重重载
用的最多的是EditorGUILayout.XXXX(“tile”,value),返回值是value ,第一个参数是标头,第二个参数是你要改变的值,并返回,还有一个重载是 EditorGUILayout.XXXX(GUIContent,value) ,返回值还是value,GUIContent,也有多重重载,我们可以选个图片,再加字体,可以实现这样的效果
public class MyExt : EditorWindow { private int param1 = -1; private float param2 = 0; private new string name = ""; private int id = 0; private Texture2D texture; private Vector2Int vector2; private string puker; [MenuItem("MyExt/Edit", false, 1)] private static void Init() { MyExt editor = GetWindowWithRect<MyExt>(new Rect(0f, 0f, 500f, 500f)); editor.Show(); } void OnGUI() { vector2= EditorGUILayout.Vector2IntField("sss",vector2); GUILayout.Label("标记1", EditorStyles.boldLabel); //单独的字体,不能输入 param1 = EditorGUILayout.IntField("param1 int", param1); if (GUILayout.Button("button")) { Debug.LogError("点下按钮"); texture = Resources.Load<Texture2D>("puker/puker00"); //这里填写你自己的图片 } GUILayout.Label("标记2", EditorStyles.boldLabel); param2 = EditorGUILayout.FloatField("float", param2); name = EditorGUILayout.TextField("Name", name); id = EditorGUILayout.IntSlider("id 0~8", id, 0, 100); GUILayout.Box(texture); // GUILayout.Label(texture); GUIContent content= new GUIContent(texture,"扑克"); puker = EditorGUILayout.TextField(content, puker); } }
有了上边这个测试,我们可以尝试自己写个小插件了
这是功能脚本
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; using System.Text; using System; using System.Security.Cryptography; public class BuilderAssetsBunlds { public static string GetOutAssetsDirecotion() //打包输出地址,后续会再完善 { string assetBundleDirectory = Application.streamingAssetsPath; if (!Directory.Exists(assetBundleDirectory)) { Directory.CreateDirectory(assetBundleDirectory); } return assetBundleDirectory; } #region 设置AssetLabels /// <summary> /// 检查目标文件下的文件系统 /// </summary> public static void CheckFileSystemInfo() //检查目标目录下的文件系统 { AssetDatabase.RemoveUnusedAssetBundleNames(); //移除没有用的assetbundlename UnityEngine.Object obj = Selection.activeObject; //选中的物体 string path = AssetDatabase.GetAssetPath(obj);//选中的文件夹 //目录结构 Assets/Resources 没有Application.datapath CoutineCheck(path); } public static void CheckFileOrDirectory(FileSystemInfo fileSystemInfo, string path) //判断是文件还是文件夹 { FileInfo fileInfo = fileSystemInfo as FileInfo; if (fileInfo != null) { SetBundleName(path); } else { CoutineCheck(path); } } public static void CoutineCheck(string path) //是文件,继续向下 { DirectoryInfo directory = new DirectoryInfo(@path); FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos(); foreach (var item in fileSystemInfos) { // Debug.Log(item); int idx = item.ToString().LastIndexOf(@"\"); string name = item.ToString().Substring(idx + 1); if (!name.Contains(".meta")) { CheckFileOrDirectory(item, path + "/" + name); //item 文件系统,加相对路径 } } } public static void SetBundleName(string path) //设置assetbundle名字 { // Debug.LogError(path); var importer = AssetImporter.GetAtPath(path); string[] strs = path.Split('.'); string[] dictors = strs[0].Split('/'); string name = ""; for (int i = 1; i < dictors.Length; i++) { if (i < dictors.Length - 1) { name += dictors[i] + "/"; } else { name += dictors[i]; } } if (importer != null) { importer.assetBundleName = name; importer.assetBundleVariant = "bytes"; // importer.SetAssetBundleNameAndVariant(name, "bytes"); // importer.assetBundleName = GetGUID(path); //两种设置方式 } else Debug.Log("importer是空的"); } public static string GetGUID(string path) { return AssetDatabase.AssetPathToGUID(path); } #endregion //*****************配置文件设置***********************//////////// static Dictionary<string, string> dicversoion = new Dictionary<string, string>(); //版本 static Dictionary<string, string> dicurl = new Dictionary<string, string>(); //下载地址 public static string GetAssetBundleStringPath() //得到存放打包资源的文件路径 { string path = Application.streamingAssetsPath; //Debug.Log(path); string[] strs = path.Split('/'); int idx = 0; for (int i = 0; i < strs.Length; i++) { if (strs[i] == "Assets") idx = i; } // Debug.Log(idx); //path = strs[strs.Length - 2] + "/" + strs[strs.Length - 1]; string str = ""; for (int i = idx; i < strs.Length; i++) { if (i != strs.Length - 1) str += strs[i] + "/"; else if (i == strs.Length - 1) str += strs[i]; //Debug.Log(i); } path = str; return path; //Debug.Log(path); } public static void CheckAssetBundleDir(string path) //是文件,继续向下 { DirectoryInfo directory = new DirectoryInfo(@path); FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos(); foreach (var item in fileSystemInfos) { // Debug.Log(item); int idx = item.ToString().LastIndexOf(@"\"); string name = item.ToString().Substring(idx + 1); if (!name.Contains(".meta")) { CheckAssetBundleFileOrDirectory(item, path + "/" + name); //item 文件系统,加相对路径 } } } public static void CheckAssetBundleFileOrDirectory(FileSystemInfo fileSystemInfo, string path) //判断是文件还是文件夹 { FileInfo fileInfo = fileSystemInfo as FileInfo; if (fileInfo != null) { Debug.Log("不为空,MD5值==>" + GetMD5(path)); // string guid = AssetDatabase.AssetPathToGUID(path); // Debug.Log("不为空,文件名字是==>>"+guid); Addconfigdic(fileInfo.Name, GetMD5(path)); } else { CheckAssetBundleDir(path); } } public static string GetMD5(string path) { FileStream fs = new FileStream(path, FileMode.Open); MD5 md5 = new MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(fs); fs.Close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); } public static void Addconfigdic(string name, string verMd5) { dicversoion.Add(name, verMd5); } public static void CreateConfigFile(string path) //创建配置文件 { } // -------------------------------我是分割线------------------------------------ // 割............................. static List<AssetBundleBuild> listassets = new List<AssetBundleBuild>(); //第二种打包方式用 static List<DirectoryInfo> listfileinfo = new List<DirectoryInfo>(); static bool isover = false; //是否检查完成,可以打包 public static bool GetState() { return isover; } public static AssetBundleBuild[] GetAssetBundleBuilds() { return listassets.ToArray(); } public static void SetAssetBundleBuilds() { UnityEngine.Object obj = Selection.activeObject; //选中的物体 string path = AssetDatabase.GetAssetPath(obj);//选中的文件夹 //目录结构 Assets/Resources 没有Application.datapath SearchFileAssetBundleBuild(path); } public static void SearchFileAssetBundleBuild(string path) //是文件,继续向下 { DirectoryInfo directory = new DirectoryInfo(@path); FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos(); listfileinfo.Clear(); foreach (var item in fileSystemInfos) { int idx = item.ToString().LastIndexOf(@"\"); string name = item.ToString().Substring(idx + 1); if ((item as DirectoryInfo) != null) listfileinfo.Add(item as DirectoryInfo); if (!name.Contains(".meta")) { CheckFileOrDirectoryReturnBundleName(item, path + "/" + name); } } if (listfileinfo.Count == 0) isover = true; else { Debug.LogError(listfileinfo.Count); } } public static string CheckFileOrDirectoryReturnBundleName(FileSystemInfo fileSystemInfo, string path) //判断是文件还是文件夹 { FileInfo fileInfo = fileSystemInfo as FileInfo; if (fileInfo != null) { string[] strs = path.Split('.'); string[] dictors = strs[0].Split('/'); string name = ""; for (int i = 1; i < dictors.Length; i++) { if (i < dictors.Length - 1) { name += dictors[i] + "/"; } else { name += dictors[i]; } } AssetBundleBuild assetBundleBuild = new AssetBundleBuild(); assetBundleBuild.assetBundleName = name; assetBundleBuild.assetBundleVariant = "bytes"; assetBundleBuild.assetNames = new string[] { path }; listassets.Add(assetBundleBuild); return name; } else { SearchFileAssetBundleBuild(path); return null; } } //****************************删除AssetLabels***************************** public static void DeleteSelectedFolderAssetLabels() //删除选中文件夹下的AssetLabels { CheckSelectedDeleteFolder(); } public static void CheckSelectedDeleteFolder() //检查目标目录下的文件系统 { AssetDatabase.RemoveUnusedAssetBundleNames(); //移除没有用的assetbundlename UnityEngine.Object obj = Selection.activeObject; //选中的物体 string path = AssetDatabase.GetAssetPath(obj);//选中的文件夹 //目录结构 Assets/Resources 没有Application.datapath DeleteAssetLabelsCheckout(path); } public static void DeleteAssetLabelsCheckout(string path) //是文件,继续向下 { DirectoryInfo directory = new DirectoryInfo(@path); FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos(); foreach (var item in fileSystemInfos) { // Debug.Log(item); int idx = item.ToString().LastIndexOf(@"\"); string name = item.ToString().Substring(idx + 1); if (!name.Contains(".meta")) { CheckDeleteFileOrDirectory(item, path + "/" + name); //item 文件系统,加相对路径 } } } public static void CheckDeleteFileOrDirectory(FileSystemInfo fileSystemInfo, string path) //判断是文件还是文件夹 { FileInfo fileInfo = fileSystemInfo as FileInfo; if (fileInfo != null) DeleteBundleName(path); else DeleteAssetLabelsCheckout(path); } public static void DeleteBundleName(string path) //设置assetbundle名字 { // Debug.LogError(path); var importer = AssetImporter.GetAtPath(path); string[] strs = path.Split('.'); string[] dictors = strs[0].Split('/'); string name = ""; for (int i = 1; i < dictors.Length; i++) { if (i < dictors.Length - 1) { name += dictors[i] + "/"; } else { name += dictors[i]; } } if (importer != null) { importer.SetAssetBundleNameAndVariant(string.Empty, string.Empty); } AssetDatabase.RemoveUnusedAssetBundleNames(); } }
这是调用脚本,我们现在做了更改
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; public class Constant { public static string SD_DIR = "SD_DIR"; } public class BundleSystem { //打包、、、、、、、、、、、、、、、、、、、、、、 //[MenuItem("Tools/打包所有设置AssetLable项目")] public static void BundlerAssets() { // Debug.LogError("打包Assets"); BuildPipeline.BuildAssetBundles(BuilderAssetsBunlds.GetOutAssetsDirecotion(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows); } //[MenuItem("Tools/打包选中文件夹下所有项目")] public static void BundSelectionAssets() { BuilderAssetsBunlds.SetAssetBundleBuilds(); while (true) { if (BuilderAssetsBunlds.GetState()) break; else Debug.LogError("等待....."); } BuildPipeline.BuildAssetBundles(BuilderAssetsBunlds.GetOutAssetsDirecotion(), BuilderAssetsBunlds.GetAssetBundleBuilds(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows); } // [MenuItem("Tools/设置Assetbundle名字")] public static void SetAssetBundellabls() { BuilderAssetsBunlds.CheckFileSystemInfo(); } // [MenuItem("Tools/设置配置文件")] public static void SetAssetConfig() { BuilderAssetsBunlds.CheckAssetBundleDir(BuilderAssetsBunlds.GetAssetBundleStringPath()); } public static void DeleteSelectedFolderAssetLabels() //删除选中项下的AssetLabels { BuilderAssetsBunlds. DeleteSelectedFolderAssetLabels(); } }
这里是编辑器扩展类,最终入口
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; public class EditorManager : EditorWindow { [MenuItem("Tools/工具管理")] static void ShowToolsWindow() { EditorManager bundleSystem = EditorWindow.GetWindow<EditorManager>(true, "AssetBundle管理", true); bundleSystem.Show(); } private void OnGUI() { if (GUILayout.Button("打包选中文件夹下所有项目", GUILayout.Height(30f))) { BundleSystem.BundSelectionAssets(); } if (GUILayout.Button("打包所有设置AssetLable项目", GUILayout.Height(30f))) { BundleSystem.BundlerAssets(); } if (GUILayout.Button("设置AssetLabels", GUILayout.Height(30f))) { BundleSystem.SetAssetBundellabls(); } if (GUILayout.Button("设置配置文件", GUILayout.Height(30f))) { BundleSystem.SetAssetConfig(); } if (GUILayout.Button("删除选定文件AssetLabel", GUILayout.Height(30f))) { BundleSystem.DeleteSelectedFolderAssetLabels(); } //文本框显示鼠标在窗口的位置 //EditorGUILayout.LabelField("鼠标在窗口的位置", Event.current.mousePosition.ToString()); } }