From 285bd5511d198977e03a5c927b0c3abffc6fb363 Mon Sep 17 00:00:00 2001 From: Soviby <936858871@qq.com> Date: Tue, 29 Oct 2024 00:31:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E4=BE=8B=E8=8A=82=E7=82=B9=E7=9A=84?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E9=9A=90=E8=97=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Editor/Entity/IEntity.cs | 4 +- Assets/Editor/Entity/ImageEntity.cs | 4 +- Assets/Editor/Entity/TextEntity.cs | 27 ++++++- Assets/Editor/Helper/EntityHelper.cs | 24 ++++++ Assets/Editor/Helper/EntityHelper.cs.meta | 11 +++ Assets/Editor/Helper/LayoutParser.cs | 2 +- Assets/Editor/Helper/PanelHelper.cs | 12 +-- Assets/Editor/Manager/EntityManager.cs | 75 ++++++++++++++++--- Assets/Editor/Manager/GlobalManager.cs | 7 ++ Assets/Editor/Manager/StageManager.cs | 19 ++++- .../Editor/ScriptObject/CacheScriptObject.cs | 1 + Assets/Editor/Windows/EditWindow.cs | 34 +++++++++ Assets/Editor/Windows/EditWindow.cs.meta | 11 +++ Assets/Editor/Windows/IWindow.cs | 47 ++++++++++++ Assets/Editor/Windows/IWindow.cs.meta | 11 +++ Assets/Editor/Windows/PanelCacheWindow.cs | 43 +++++------ 16 files changed, 280 insertions(+), 52 deletions(-) create mode 100644 Assets/Editor/Helper/EntityHelper.cs create mode 100644 Assets/Editor/Helper/EntityHelper.cs.meta create mode 100644 Assets/Editor/Windows/EditWindow.cs create mode 100644 Assets/Editor/Windows/EditWindow.cs.meta create mode 100644 Assets/Editor/Windows/IWindow.cs create mode 100644 Assets/Editor/Windows/IWindow.cs.meta diff --git a/Assets/Editor/Entity/IEntity.cs b/Assets/Editor/Entity/IEntity.cs index 99c07e5..4cc6155 100644 --- a/Assets/Editor/Entity/IEntity.cs +++ b/Assets/Editor/Entity/IEntity.cs @@ -52,10 +52,10 @@ namespace UguiToolkit.Editor public void ApplyData(T1 ui) { - OnApplyData(ui, m_elementInfo); + OnApplyData(ui); } - protected virtual void OnApplyData(T1 ui, T2 elementInfo) { } + protected virtual void OnApplyData(T1 ui) { } } } #endif \ No newline at end of file diff --git a/Assets/Editor/Entity/ImageEntity.cs b/Assets/Editor/Entity/ImageEntity.cs index 6db922c..e19ef4b 100644 --- a/Assets/Editor/Entity/ImageEntity.cs +++ b/Assets/Editor/Entity/ImageEntity.cs @@ -8,9 +8,9 @@ public class ImageEntity : BaseEntity { private Image m_previewImage; - protected override void OnApplyData(Image ui, LayoutInfo.ImageInfo elementInfo) + protected override void OnApplyData(Image ui) { - + ApplyTransform(ui.transform); } public void InitPreviewImage() { diff --git a/Assets/Editor/Entity/TextEntity.cs b/Assets/Editor/Entity/TextEntity.cs index d4dfd2b..1eb90b5 100644 --- a/Assets/Editor/Entity/TextEntity.cs +++ b/Assets/Editor/Entity/TextEntity.cs @@ -7,11 +7,30 @@ using UnityEngine.UI; public class TextEntity : BaseEntity { - protected override void OnApplyData(Text ui, LayoutInfo.TextInfo elementInfo) + private Text m_previewText; + + protected override void OnApplyData(Text ui) { - ui.text = elementInfo.text; - ui.fontSize = (int)elementInfo.size; - ui.color = elementInfo.color; + ui.text = ElementInfo.text; + ui.fontSize = (int)ElementInfo.size; + ui.color = ElementInfo.color; + ui.alignment = TextAnchor.MiddleCenter; + + var rectTransform = ui.rectTransform; + rectTransform.sizeDelta = new Vector2(ElementInfo.w + 10, ElementInfo.h + 10); + } + + public void InitPreviewText() + { + if (ElementInfo == null) return; + + if (!TryGetComponent(out m_previewText)) + { + m_previewText = gameObject.AddComponent(); + } + OnApplyData(m_previewText); + + ApplyTransform(transform); } } #endif \ No newline at end of file diff --git a/Assets/Editor/Helper/EntityHelper.cs b/Assets/Editor/Helper/EntityHelper.cs new file mode 100644 index 0000000..be4c15a --- /dev/null +++ b/Assets/Editor/Helper/EntityHelper.cs @@ -0,0 +1,24 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; + +public static class EntityHelper +{ + public static void UpdateHierarchyOfEntity(in bool show, in GameObject entity) + { + entity.hideFlags = show ? HideFlags.DontSave : HideFlags.HideAndDontSave; + + } + + public static void UpdateSceneVisibilityOfEntity(in bool show, in GameObject entity) + { + if (show) + { + SceneVisibilityManager.instance.EnablePicking(entity, true); + } + else { + SceneVisibilityManager.instance.DisablePicking(entity, true); + } + } +} diff --git a/Assets/Editor/Helper/EntityHelper.cs.meta b/Assets/Editor/Helper/EntityHelper.cs.meta new file mode 100644 index 0000000..3e70703 --- /dev/null +++ b/Assets/Editor/Helper/EntityHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1dd0298466fd10844a9ab92180a42e18 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/Helper/LayoutParser.cs b/Assets/Editor/Helper/LayoutParser.cs index bb110d1..c086f2b 100644 --- a/Assets/Editor/Helper/LayoutParser.cs +++ b/Assets/Editor/Helper/LayoutParser.cs @@ -36,7 +36,7 @@ namespace UguiToolkit.Editor } else if (layoutElementJsonData.type == "Text") { - ColorUtility.TryParseHtmlString(layoutElementJsonData.color, out var color); + ColorUtility.TryParseHtmlString("#" + layoutElementJsonData.color, out var color); elementInfos.Add(new LayoutInfo.TextInfo() { diff --git a/Assets/Editor/Helper/PanelHelper.cs b/Assets/Editor/Helper/PanelHelper.cs index 6682921..7ff77b0 100644 --- a/Assets/Editor/Helper/PanelHelper.cs +++ b/Assets/Editor/Helper/PanelHelper.cs @@ -1,4 +1,5 @@ #if UNITY_EDITOR +using Sirenix.OdinInspector.Editor; using UguiToolkit.Editor.Windows; using UnityEditor; using UnityEditor.SceneManagement; @@ -13,24 +14,25 @@ namespace UguiToolkit.Editor { PrefabStage.prefabStageOpened += OnPrefabStageOpened; PrefabStage.prefabStageClosing += OnPrefabStageClosing; - } private static void OnPrefabStageOpened(PrefabStage stage) { var setting = GlobalManager.Instance.setting; - Debug.Log(stage.assetPath); - Debug.Log(setting.prefabForUIDirPath); if (!stage.assetPath.StartsWith(setting.prefabForUIDirPath)) return; // 打开配置界面 - PanelCacheWindow.ShowWindow(stage); + PanelCacheWindow.ShowWindow(new PanelCacheWindow.PanelCacheWindowArgs + { + stage = stage, + }); } private static void OnPrefabStageClosing(PrefabStage stage) { - // 关闭配置界面 + // 关闭界面 PanelCacheWindow.CloseWindow(); + EditWindow.CloseWindow(); } } } diff --git a/Assets/Editor/Manager/EntityManager.cs b/Assets/Editor/Manager/EntityManager.cs index 22f8808..992d906 100644 --- a/Assets/Editor/Manager/EntityManager.cs +++ b/Assets/Editor/Manager/EntityManager.cs @@ -1,13 +1,28 @@ #if UNITY_EDITOR +using System.Collections.Generic; using UnityEngine; namespace UguiToolkit.Editor { + [ExecuteAlways] public class EntityManager : MonoBehaviour, IManager { private PanelCache m_panelCache; private Transform entityRoot; + private List imageEntities; + private List textEntities; + + private void Start() + { + GlobalManager.Instance.OnShowHierarchyOfEntityChange += UpdateHierarchyOfEntityAllEntity; + } + + private void OnDestroy() + { + GlobalManager.Instance.OnShowHierarchyOfEntityChange -= UpdateHierarchyOfEntityAllEntity; + } + public void InitAllEntity(PanelCache panelCache) { this.m_panelCache = panelCache; @@ -16,29 +31,67 @@ namespace UguiToolkit.Editor CreateAllEntity(); } + private void UpdateHierarchyOfEntityAllEntity(bool show) + { + UpdateHierarchyOfEntity(show, entityRoot.gameObject); + foreach (var entity in imageEntities) UpdateHierarchyOfEntity(show, entity.gameObject); + foreach (var entity in textEntities) UpdateHierarchyOfEntity(show, entity.gameObject); + } + + private void UpdateHierarchyOfEntity(in bool show, in GameObject entity) + { + EntityHelper.UpdateHierarchyOfEntity(show, entity.gameObject); + EntityHelper.UpdateSceneVisibilityOfEntity(show, entity.gameObject); + } + private void CreateAllEntity() { if (this.m_panelCache == null) return; var go = new GameObject(); + UpdateHierarchyOfEntity(false, go); entityRoot = go.transform; entityRoot.SetParent(transform); entityRoot.localPosition = Vector3.zero; entityRoot.localRotation = Quaternion.identity; entityRoot.localScale = Vector3.one; - - // Image - foreach (var elementInfo in m_panelCache.GetLayoutElementInfos()) + + imageEntities = new(m_panelCache.layoutInfo.Count); + textEntities = new(m_panelCache.layoutInfo.Count); + + foreach (var elementInfo in m_panelCache.GetLayoutElementInfos()) { - go = new GameObject(); - var entity = go.AddComponent(); - entity.transform.SetParent(entityRoot); - entity.transform.SetSiblingIndex(0); + var imgInfo = elementInfo as LayoutInfo.ImageInfo; + if (imgInfo != null) // Image + { + go = new GameObject(); + var entity = go.AddComponent(); + entity.transform.SetParent(entityRoot); + entity.transform.SetSiblingIndex(0); + + entity.SetData(imgInfo); + entity.InitPreviewImage(); + + imageEntities.Add(entity); + UpdateHierarchyOfEntity(false, entity.gameObject); + continue; + } + var textInfo = elementInfo as LayoutInfo.TextInfo; + if (textInfo != null) // Text + { + go = new GameObject(); + var entity = go.AddComponent(); + entity.transform.SetParent(entityRoot); + entity.transform.SetSiblingIndex(0); + + entity.SetData(textInfo); + entity.InitPreviewText(); + + textEntities.Add(entity); + UpdateHierarchyOfEntity(false, entity.gameObject); + continue; + } - entity.SetData(elementInfo); - entity.InitPreviewImage(); } - - // Text } } diff --git a/Assets/Editor/Manager/GlobalManager.cs b/Assets/Editor/Manager/GlobalManager.cs index 7fa09f8..9e2daba 100644 --- a/Assets/Editor/Manager/GlobalManager.cs +++ b/Assets/Editor/Manager/GlobalManager.cs @@ -1,5 +1,6 @@ #if UNITY_EDITOR +using System; using UguiToolkit.Editor; using UnityEditor; @@ -24,6 +25,12 @@ namespace UguiToolkit public SettingScriptObject setting; public CacheScriptObject cache; + /// + /// 当是否显示预览实例发生改变时 + /// + public Action OnShowHierarchyOfEntityChange; + + public void SaveCache() { EditorUtility.SetDirty(cache); diff --git a/Assets/Editor/Manager/StageManager.cs b/Assets/Editor/Manager/StageManager.cs index 987be43..7fc4cd2 100644 --- a/Assets/Editor/Manager/StageManager.cs +++ b/Assets/Editor/Manager/StageManager.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; namespace UguiToolkit { + [ExecuteAlways] public class StageManager : MonoBehaviour { private Scene m_scene; @@ -29,7 +30,22 @@ namespace UguiToolkit return m_instance; } } - + + private void Start() + { + GlobalManager.Instance.OnShowHierarchyOfEntityChange += UpdateHierarchy; + } + + private void OnDestroy() + { + GlobalManager.Instance.OnShowHierarchyOfEntityChange -= UpdateHierarchy; + } + + private void UpdateHierarchy(bool show) + { + EntityHelper.UpdateHierarchyOfEntity(show, m_instance.gameObject); + } + public static StageManager CreateStageManager(Scene scene, GameObject prefabContentsRoot) { var go = new GameObject(); @@ -45,6 +61,7 @@ namespace UguiToolkit go.hideFlags = HideFlags.DontSave; + m_instance.UpdateHierarchy(false); return m_instance; } diff --git a/Assets/Editor/ScriptObject/CacheScriptObject.cs b/Assets/Editor/ScriptObject/CacheScriptObject.cs index 64b6adb..f386bd2 100644 --- a/Assets/Editor/ScriptObject/CacheScriptObject.cs +++ b/Assets/Editor/ScriptObject/CacheScriptObject.cs @@ -86,6 +86,7 @@ namespace UguiToolkit.Editor public float W => m_w; public float H => m_h; + public int Count => m_elementInfos == null ? 0 : m_elementInfos.Count; public LayoutInfo(List elementInfos, float w, float h) { diff --git a/Assets/Editor/Windows/EditWindow.cs b/Assets/Editor/Windows/EditWindow.cs new file mode 100644 index 0000000..f22acfa --- /dev/null +++ b/Assets/Editor/Windows/EditWindow.cs @@ -0,0 +1,34 @@ +using Sirenix.OdinInspector; +using Sirenix.OdinInspector.Editor; +using UnityEngine; + +namespace UguiToolkit.Editor.Windows +{ + public class EditWindow : BaseWindow + { + [SerializeField, HideInInspector] + private bool m_showHierarchyOfEntityChange = false; + + [LabelText("ʵHierarchyǷʾ"), ShowInInspector] + public bool ShowHierarchyOfEntity + { + get => m_showHierarchyOfEntityChange; + set + { + m_showHierarchyOfEntityChange = value; + + GlobalManager.Instance.OnShowHierarchyOfEntityChange?.Invoke(m_showHierarchyOfEntityChange); + } + } + + public override string GettitleContent() + { + return "ֱ༭"; + } + + public override void Init(WindowArgs args = null) + { + + } + } +} diff --git a/Assets/Editor/Windows/EditWindow.cs.meta b/Assets/Editor/Windows/EditWindow.cs.meta new file mode 100644 index 0000000..bd44a47 --- /dev/null +++ b/Assets/Editor/Windows/EditWindow.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f1d07ee52aac57747b133eff2f1972b8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/Windows/IWindow.cs b/Assets/Editor/Windows/IWindow.cs new file mode 100644 index 0000000..c2d2a63 --- /dev/null +++ b/Assets/Editor/Windows/IWindow.cs @@ -0,0 +1,47 @@ +using Sirenix.OdinInspector.Editor; +using UnityEditor; +using UnityEngine; + +namespace UguiToolkit.Editor.Windows +{ + public abstract class BaseWindow : OdinEditorWindow, IWindow where T : OdinEditorWindow, IWindow + { + private static T m_window; + public static T Window => m_window; + + public static T ShowWindow(WindowArgs args = null) + { + if (m_window) m_window.Close(); + m_window = CreateWindow(); + m_window.titleContent = new(m_window.GettitleContent()); + + m_window.Show(); + m_window.Init(args); + + return m_window; + } + + public static void CloseWindow() + { + if (m_window) + { + m_window.Close(); + m_window = null; + } + } + + public virtual string GettitleContent() => ""; + public virtual void Init(WindowArgs args = null) { } + } + + public interface IWindow + { + string GettitleContent(); + void Init(WindowArgs args = null); + } + + public class WindowArgs + { + + } +} diff --git a/Assets/Editor/Windows/IWindow.cs.meta b/Assets/Editor/Windows/IWindow.cs.meta new file mode 100644 index 0000000..89dc68f --- /dev/null +++ b/Assets/Editor/Windows/IWindow.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 86f25c7cb7d8d6446bdc67f8081fd688 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/Windows/PanelCacheWindow.cs b/Assets/Editor/Windows/PanelCacheWindow.cs index f761463..c6b944c 100644 --- a/Assets/Editor/Windows/PanelCacheWindow.cs +++ b/Assets/Editor/Windows/PanelCacheWindow.cs @@ -11,10 +11,8 @@ using UnityEngine.Serialization; namespace UguiToolkit.Editor.Windows { - public class PanelCacheWindow : OdinEditorWindow + public class PanelCacheWindow : BaseWindow { - private static PanelCacheWindow _window; - [LabelText("源图片文件夹路径"), FolderPath, SerializeField] private string m_srcImgDirPath; @@ -27,7 +25,7 @@ namespace UguiToolkit.Editor.Windows private PanelCache panelCache; [Button("开启助手", ButtonSizes.Medium)] - private void Start() + private void DoStart() { panelCache.srcImgDirPath = m_srcImgDirPath; panelCache.layoutInfoFilePath = m_layoutInfoFilePath; @@ -45,6 +43,9 @@ namespace UguiToolkit.Editor.Windows var entityManager = stageManager.CreateSubManager(); entityManager.InitAllEntity(panelCache); CloseWindow(); + + // 打开编辑界面 + EditWindow.ShowWindow(null); } [Button("解析Layout.txt", ButtonSizes.Medium), ShowIf(nameof(m_cacheExist))] @@ -99,8 +100,17 @@ namespace UguiToolkit.Editor.Windows } } - private void Init() + public override string GettitleContent() { + return "请选择信息后开启助手"; + } + + public override void Init(WindowArgs args = null) + { + var _args = args as PanelCacheWindowArgs; + m_prefabStage = _args.stage; + m_prefab = AssetDatabase.LoadAssetAtPath(_args.stage.assetPath); + var cache = GlobalManager.Instance.cache; if (cache.panelCaches.TryGetValue(m_prefab, out panelCache)) { @@ -115,29 +125,10 @@ namespace UguiToolkit.Editor.Windows } } - public static PanelCacheWindow ShowWindow(PrefabStage stage) + public class PanelCacheWindowArgs : WindowArgs { - if (_window) _window.Close(); - _window = CreateWindow(); - _window.titleContent = new ("请选择信息后开启助手"); - _window.m_prefabStage = stage; - _window.m_prefab = AssetDatabase.LoadAssetAtPath(stage.assetPath); - - _window.Show(); - _window.Init(); - - return _window; + public PrefabStage stage; } - - public static void CloseWindow() - { - if (_window) - { - _window.Close(); - _window = null; - } - } - } } #endif \ No newline at end of file