From dc8b45964ce5fa44e8cb6ceb809af21ca037366a Mon Sep 17 00:00:00 2001 From: biaosong Date: Tue, 3 Dec 2024 13:05:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96tmp=E5=AD=97=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Editor/Entity/IEntity.cs | 4 +- Assets/Editor/Entity/TextMeshProEntity.cs | 21 +++++----- Assets/Editor/Manager/EntityManager.cs | 39 ++++++++++++++++++- Assets/Editor/Manager/GlobalManager.cs | 10 +++++ Assets/Editor/Manager/StageManager.cs | 7 +++- .../ScriptObject/SettingScriptObject.cs | 16 ++++++++ Assets/Editor/Windows/EditWindow.cs | 25 +++++++++++- 7 files changed, 106 insertions(+), 16 deletions(-) diff --git a/Assets/Editor/Entity/IEntity.cs b/Assets/Editor/Entity/IEntity.cs index 54b1d51..299a012 100644 --- a/Assets/Editor/Entity/IEntity.cs +++ b/Assets/Editor/Entity/IEntity.cs @@ -10,7 +10,7 @@ namespace UguiToolkit.Editor { void ApplyTransform(Transform tf); bool IsInside(Transform tf); - void ApplyData(T ui) where T: MonoBehaviour; + void ApplyData(T ui) where T: Component; void InitPreview(); void ShowSelectionImg(bool show); GameObject gameObject { get; } @@ -51,7 +51,7 @@ namespace UguiToolkit.Editor CreateSelectionImg(); } - public void ApplyData(T ui) where T : MonoBehaviour + public void ApplyData(T ui) where T : Component { OnApplyData(ui as T1); } diff --git a/Assets/Editor/Entity/TextMeshProEntity.cs b/Assets/Editor/Entity/TextMeshProEntity.cs index e2b1fbc..2436280 100644 --- a/Assets/Editor/Entity/TextMeshProEntity.cs +++ b/Assets/Editor/Entity/TextMeshProEntity.cs @@ -74,6 +74,10 @@ namespace UguiToolkit.Editor private static bool GetTextFontPreset(LayoutInfo.TextInfo textInfo, out TextFontPresetOfUnity textFontPreset) { + float CalcColorDifference(in Color color1, in Color color2) + { + return math.abs(color1.r - color2.r) * 255 + math.abs(color1.g - color2.g) * 255 + math.abs(color1.b - color2.b) * 255; + } textFontPreset = null; var setting = GlobalManager.Instance.setting; if (setting != null && setting.textFontList != null) @@ -84,18 +88,15 @@ namespace UguiToolkit.Editor { if (textFontInfoOfPsd.stroke) { - if (!textInfo.IsStroke) return false; - if (!textFontInfoOfPsd.allStrokeColor && textFontInfoOfPsd.strokeColor != textInfo.strokeColor) return false; - textFontPreset = textFontInfoOfPsd; - return true; - } - else - { - if (!textInfo.IsStroke) { - textFontPreset = textFontInfoOfPsd; - return true; + if (!textInfo.IsStroke) continue; + if (!textFontInfoOfPsd.allStrokeColor) + { + if (CalcColorDifference(textFontInfoOfPsd.strokeColor ,textInfo.strokeColor) > textFontInfoOfPsd.strokeColorThreshold) continue; } } + + textFontPreset = textFontInfoOfPsd; + return true; } } } diff --git a/Assets/Editor/Manager/EntityManager.cs b/Assets/Editor/Manager/EntityManager.cs index 87c6b6e..e7eaccd 100644 --- a/Assets/Editor/Manager/EntityManager.cs +++ b/Assets/Editor/Manager/EntityManager.cs @@ -1,8 +1,10 @@ #if UNITY_EDITOR using Sirenix.OdinInspector; +using System; using System.Collections.Generic; using System.Globalization; using System.IO; +using TMPro; using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine; @@ -27,6 +29,7 @@ namespace UguiToolkit.Editor [LabelText("脱离选择控制"), ShowInInspector] private bool m_noSelection; + [SerializeField, HideInInspector] private StageManager m_stageManager; private HashSet m_checkImgPaths; private float m_lastCheckTime = 0f; @@ -38,6 +41,8 @@ namespace UguiToolkit.Editor { var globalMng = GlobalManager.Instance; globalMng.showHierarchyOfEntityChanged += OnUpdateHierarchyOfEntityAllEntity; + globalMng.showBackgroundChanged += OnUpdateBackgroundShow; + globalMng.createAllTextEntity += CreateAllTextEntity; m_useTMP = globalMng.setting.useTMP; Selection.selectionChanged += OnSelectionChanged; @@ -47,7 +52,10 @@ namespace UguiToolkit.Editor private void OnDisable() { - GlobalManager.Instance.showHierarchyOfEntityChanged -= OnUpdateHierarchyOfEntityAllEntity; + var globalMng = GlobalManager.Instance; + globalMng.showHierarchyOfEntityChanged -= OnUpdateHierarchyOfEntityAllEntity; + globalMng.showBackgroundChanged -= OnUpdateBackgroundShow; + globalMng.createAllTextEntity -= CreateAllTextEntity; Selection.selectionChanged -= OnSelectionChanged; } @@ -130,6 +138,26 @@ namespace UguiToolkit.Editor } } + private void CreateAllTextEntity() + { + var root = m_stageManager.PrefabContentsRoot; + var textsTf = root.transform.Find("__texts__"); + if (textsTf) DestroyImmediate(textsTf.gameObject); + + if (m_textEntities == null) return; + var textsGo = new GameObject("__texts__", typeof(RectTransform)); + textsGo.transform.parent = root.transform; + + Type textType = m_useTMP ? typeof(TextMeshProUGUI) : typeof(Text); + foreach (var textEntity in m_textEntities) + { + var newText = new GameObject(textEntity.gameObject.name, typeof(RectTransform), textType); + newText.transform.parent = textsGo.transform; + textEntity.ApplyData(newText.GetComponent(textType)); + textEntity.ApplyTransform(newText.transform); + } + } + private void InitBackground() { if (m_background) DestroyImmediate(m_background.gameObject); @@ -315,6 +343,7 @@ namespace UguiToolkit.Editor public void InitAllEntity(PanelCache panelCache) { this.m_panelCache = panelCache; + this.m_stageManager = StageManager.Instance; // 创建所有实例 CreateAllEntity(); @@ -324,6 +353,14 @@ namespace UguiToolkit.Editor CheckPanelCache(); } + private void OnUpdateBackgroundShow(bool show) + { + if (m_background) + { + m_background.gameObject.SetActive(show); + } + } + private void OnUpdateHierarchyOfEntityAllEntity(bool show) { UpdateHierarchyOfEntity(show, m_entityRoot.gameObject); diff --git a/Assets/Editor/Manager/GlobalManager.cs b/Assets/Editor/Manager/GlobalManager.cs index e022ea6..80b73cb 100644 --- a/Assets/Editor/Manager/GlobalManager.cs +++ b/Assets/Editor/Manager/GlobalManager.cs @@ -31,6 +31,16 @@ namespace UguiToolkit /// public Action showHierarchyOfEntityChanged; + /// + /// 背景显示发生改变 + /// + public Action showBackgroundChanged; + + /// + /// 创建所有TextEntity + /// + public Action createAllTextEntity; + public void SaveCache(GameObject asset, PanelCache panelCache) { cache.panelCaches[asset] = panelCache; diff --git a/Assets/Editor/Manager/StageManager.cs b/Assets/Editor/Manager/StageManager.cs index c819b3a..e112577 100644 --- a/Assets/Editor/Manager/StageManager.cs +++ b/Assets/Editor/Manager/StageManager.cs @@ -10,15 +10,20 @@ namespace UguiToolkit [ExecuteAlways] public class StageManager : MonoBehaviour { + [SerializeField, HideInInspector] private Scene m_scene; public Scene Scene => m_scene; + [SerializeField, HideInInspector] private GameObject m_prefabContentsRoot; public GameObject PrefabContentsRoot => m_prefabContentsRoot; + [SerializeField, HideInInspector] private Dictionary m_mngMap = new Dictionary(); public GameObject PrefabAsset => m_prefabAsset; + [SerializeField, HideInInspector] private GameObject m_prefabAsset; - + + [SerializeField, HideInInspector] private static StageManager m_instance; public static StageManager Instance { diff --git a/Assets/Editor/ScriptObject/SettingScriptObject.cs b/Assets/Editor/ScriptObject/SettingScriptObject.cs index ba7ffc9..064d8ae 100644 --- a/Assets/Editor/ScriptObject/SettingScriptObject.cs +++ b/Assets/Editor/ScriptObject/SettingScriptObject.cs @@ -1,7 +1,10 @@ #if UNITY_EDITOR using Sirenix.OdinInspector; using System; +using System.Collections; using System.Collections.Generic; +using System.Linq; +using TMPro.EditorUtilities; using Unity.Mathematics; using UnityEngine; @@ -28,15 +31,28 @@ namespace UguiToolkit.Editor public List fontNameOfPsd; public bool stroke; // 使用描边 public Color strokeColor; + public float strokeColorThreshold; public bool allStrokeColor; public bool shadow; // 使用阴影 public Color shadowColor; + public float shadowColorThreshold; public bool allShadowColor; [Title("Unity属性")] public float2 sizeOffset; public TMPro.TMP_FontAsset tmpAsset; + [ShowIf(nameof(ShowMaterialPreset)), ValueDropdown(nameof(GetAllMaterialPreset), IsUniqueList = true)] public string materialPreset; + private bool ShowMaterialPreset => tmpAsset; + + private IEnumerable GetAllMaterialPreset() + { + if (tmpAsset != null) + return TMP_EditorUtility.FindMaterialReferences(tmpAsset) + .Select(x => new ValueDropdownItem(x.name, x.name)); + + return null; + } } } #endif \ No newline at end of file diff --git a/Assets/Editor/Windows/EditWindow.cs b/Assets/Editor/Windows/EditWindow.cs index 7d07ed9..5fc8d1e 100644 --- a/Assets/Editor/Windows/EditWindow.cs +++ b/Assets/Editor/Windows/EditWindow.cs @@ -9,8 +9,11 @@ namespace UguiToolkit.Editor.Windows [SerializeField, HideInInspector] private bool m_showHierarchyOfEntityChange = false; - [LabelText("ʵHierarchyǷʾ"), ShowInInspector] - public bool ShowHierarchyOfEntity + [SerializeField, HideInInspector] + private bool m_showBackground = true; + + [LabelText("HierarchyǷʾ"), ShowInInspector] + private bool ShowHierarchyOfEntity { get => m_showHierarchyOfEntityChange; set @@ -21,6 +24,24 @@ namespace UguiToolkit.Editor.Windows } } + [LabelText("Ƿʾ"), ShowInInspector] + private bool ShowBackground + { + get => m_showBackground; + set + { + m_showBackground = value; + + GlobalManager.Instance.showBackgroundChanged?.Invoke(m_showBackground); + } + } + + [Button("Text")] + private void CreateAllTextEntity() + { + GlobalManager.Instance.createAllTextEntity?.Invoke(); + } + public override string GettitleContent() { return "ֱ༭";