using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; namespace Typhoon.PublishTool { public class PublishMenuWindow : EditorWindow { public List Settings = new List(); public List SettingsNames = new List(); private int _selectIndex = 0; private Editor _editor; private Vector2 _scroll; [MenuItem("发布工具包/发布窗口")] private static void Open() { var win = GetWindow(); win.minSize = new Vector2(800, 800); win.Show(); } private void OnEnable() { LoadSettings(); } private void LoadSettings() { Settings = new List(); SettingsNames = new List(); //遍历所有的发布配置 var guids = AssetDatabase.FindAssets("t:PublishSetting"); foreach (var guid in guids) { var path = AssetDatabase.GUIDToAssetPath(guid); var setting = AssetDatabase.LoadAssetAtPath(path); if (setting != null) { Settings.Add(setting); SettingsNames.Add(setting.name); } } } private void OnGUI() { if (Settings.Count <= 0) { GUILayout.Label("暂无无发布设置,创建方式>Create>Typhoon>PublishTool>发布设置"); } if (Settings != null && Settings.Count > 0 && SettingsNames != null && SettingsNames.Count == Settings.Count) { GUILayout.Label("发布设置:", EditorStyles.boldLabel); GUILayout.Label("", GUI.skin.horizontalSlider, GUILayout.Height(16)); _selectIndex = Mathf.Clamp(_selectIndex, 0, Settings.Count); _selectIndex = GUILayout.SelectionGrid(_selectIndex, SettingsNames.ToArray(), 2); if (_selectIndex < Settings.Count) { var select = Settings[_selectIndex]; GUILayout.Space(10); GUILayout.BeginHorizontal(); var temEnable = GUI.enabled; GUI.enabled = false; GUILayout.Label($"当前选择:{select.name}", EditorStyles.boldLabel); EditorGUILayout.ObjectField(select, typeof(Object), false); GUI.enabled = temEnable; GUILayout.EndHorizontal(); _scroll = GUILayout.BeginScrollView(_scroll); GUILayout.Label("", GUI.skin.horizontalSlider, GUILayout.Height(16)); if (_editor != null) { if (_editor.target != select) { DestroyImmediate(_editor, true); } } if (_editor == null) { _editor = Editor.CreateEditor(select, typeof(PublishSettingInspector)); } if (_editor != null) { _editor.OnInspectorGUI(); } GUILayout.EndScrollView(); } } } private void OnFocus() { LoadSettings(); } public static TextAsset CreatePackageJson(string savePath) { //转成本地路径 savePath = FullPathToAssetDataPath(savePath); var package = new PackageJson(); package.name = "com.typhoon.unknown"; package.displayName = "未命名"; package.version = "1.0.0"; package.description = "描述..."; package.unity = "2018.1"; package.type = "tool"; package.hideInEditor = false; package.author = new Author() { name = "Jan Zhang", email = string.Empty, url = string.Empty, }; package.changelogUrl = string.Empty; package.documentationUrl = string.Empty; package.keywords = new[] { "typhoon", }; package.license = string.Empty; package.licensesUrl = string.Empty; File.WriteAllText(savePath, JsonUtility.ToJson(package)); AssetDatabase.Refresh(); var target = AssetDatabase.LoadAssetAtPath(savePath); if (target != null) { EditorGUIUtility.PingObject(target); Debug.Log($"创建成功:{savePath} "); } return target; } [MenuItem("Assets/Create/Typhoon/PublishTool/创建 package.json")] private static void CreatePackageJson() { var path = string.Empty; foreach (var g in Selection.assetGUIDs) { path = AssetDatabase.GUIDToAssetPath(g); break; } if (!string.IsNullOrWhiteSpace(path)) { var savePath = $"{path}/package.json"; CreatePackageJson(savePath); } } public static string FullPathToAssetDataPath(string path) { path = path.Replace("\\", "/"); path = path.Replace(Application.dataPath, "Assets"); return path; } } }