From 97f6cc2a3c96d055690fb286ce58f18855b4c39e Mon Sep 17 00:00:00 2001 From: Soviby <936858871@qq.com> Date: Thu, 7 Nov 2024 01:45:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Editor/Entity/IEntity.cs | 60 ++------------ Assets/Editor/Entity/ImageEntity.cs | 110 +++++++++++++++++++------ Assets/Editor/Entity/TextEntity.cs | 53 +++++++----- Assets/Editor/Manager/EntityManager.cs | 68 ++++++++++++--- 4 files changed, 181 insertions(+), 110 deletions(-) diff --git a/Assets/Editor/Entity/IEntity.cs b/Assets/Editor/Entity/IEntity.cs index de6c293..deeedd3 100644 --- a/Assets/Editor/Entity/IEntity.cs +++ b/Assets/Editor/Entity/IEntity.cs @@ -1,5 +1,5 @@ #if UNITY_EDITOR -using Sirenix.OdinInspector; +using System; using Unity.Mathematics; using UnityEngine; using UnityEngine.UIElements; @@ -8,28 +8,22 @@ namespace UguiToolkit.Editor { public interface IEntity { - void SetTransform(float rotiation, float2 scale, bool similarityCalc); void ApplyTransform(Transform tf); bool IsInside(Transform tf); + void ApplyData(T ui) where T: MonoBehaviour; } public abstract class BaseEntity : MonoBehaviour, IEntity where T1 : MonoBehaviour where T2 : LayoutInfo.ElementInfo { // ElementInfo private T2 m_elementInfo; - - [ShowInInspector] - private float rotiation; - [ShowInInspector] - private float2 scale; - [ShowInInspector] - private bool similarityCalc; - [ShowInInspector] - private bool needFillTransform; private UnityEngine.UI.Image m_selectionImg; public T2 ElementInfo => m_elementInfo; + public abstract void ApplyTransform(Transform tf); + protected abstract void OnApplyData(T1 ui); + public void ShowSelectionImg(bool show) { if (m_selectionImg) @@ -45,42 +39,6 @@ namespace UguiToolkit.Editor return rect.Contains(pos); } - public void ApplyTransform(Transform tf) - { - if (needFillTransform) - { - if (similarityCalc) - { - if (m_selectionImg) - { - var rt = tf as RectTransform; - var rtSelectionImg = m_selectionImg.transform as RectTransform; - rt.anchorMax = rt.anchorMin; - rt.sizeDelta = new Vector2(rtSelectionImg.sizeDelta.x, rtSelectionImg.sizeDelta.y); - } - } - else { - tf.rotation = Quaternion.Euler(0, 0, rotiation * -1); - tf.localScale = new Vector3(scale.x, scale.y, 1); - } - } - var position = m_elementInfo.Position; - tf.position = StageManager.Instance.PrefabContentsRoot.transform.TransformPoint(new Vector3(position.x, position.y, 0)); - } - - // 查找时调用 - public void SetTransform(float rotiation, float2 scale, bool similarityCalc) - { - this.rotiation = rotiation; - this.scale = scale; - this.similarityCalc = similarityCalc; - - this.needFillTransform = true; - } - - // 查找时调用 - public void ClearFillTransform() => this.needFillTransform = false; - // 创建时调用 public void SetData(T2 elementInfo) { @@ -88,9 +46,9 @@ namespace UguiToolkit.Editor CreateSelectionImg(); } - public void ApplyData(T1 ui) + public void ApplyData(T ui) where T : MonoBehaviour { - OnApplyData(ui); + OnApplyData(ui as T1); } private void CreateSelectionImg() @@ -107,10 +65,8 @@ namespace UguiToolkit.Editor rtf.sizeDelta = new Vector2(m_elementInfo.w, m_elementInfo.h); m_selectionImg = go.AddComponent(); - m_selectionImg.color = new Color(0, 1, 0, 0.3f); + m_selectionImg.color = new Color(0, 1, 0, 0.2f); } - - 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 62db126..f9b869b 100644 --- a/Assets/Editor/Entity/ImageEntity.cs +++ b/Assets/Editor/Entity/ImageEntity.cs @@ -1,46 +1,104 @@ #if UNITY_EDITOR +using Sirenix.OdinInspector; using UguiToolkit.Editor; +using Unity.Mathematics; using UnityEditor; using UnityEngine; using UnityEngine.UI; -public class ImageEntity : BaseEntity +namespace UguiToolkit.Editor { - private Image m_previewImage; - - protected override void OnApplyData(Image ui) + public class ImageEntity : BaseEntity { - ApplyTransform(ui.transform); - } + [ShowInInspector] + private float rotiation; + [ShowInInspector] + private float2 scale; + [ShowInInspector] + private bool similarityCalc; + [ShowInInspector] + private bool needFillTransform; - public void InitPreviewImage() { - if (ElementInfo == null) return; + private Image m_previewImage; - if (!TryGetComponent(out m_previewImage)) + protected override void OnApplyData(Image ui) { - m_previewImage = gameObject.AddComponent(); + ApplyTransform(ui.transform); } - LoadImageFromFile(ElementInfo.imgPath); - ApplyTransform(transform); - } - - void LoadImageFromFile(string path) - { - if (System.IO.File.Exists(path)) + public void InitPreviewImage() { - byte[] fileData = System.IO.File.ReadAllBytes(path); - Texture2D texture = new Texture2D(2, 2); - texture.LoadImage(fileData); // ͼƬݵTexture2D + if (ElementInfo == null) return; - Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); - m_previewImage.sprite = sprite; - m_previewImage.color = new Color(1, 1, 1, 0.7f); - m_previewImage.SetNativeSize(); + if (!TryGetComponent(out m_previewImage)) + { + m_previewImage = gameObject.AddComponent(); + } + + LoadImageFromFile(ElementInfo.imgPath); + ApplyTransform(transform); } - else + + void LoadImageFromFile(string path) { - Debug.LogError("File not found at path: " + path); + if (System.IO.File.Exists(path)) + { + byte[] fileData = System.IO.File.ReadAllBytes(path); + Texture2D texture = new Texture2D(2, 2); + texture.LoadImage(fileData); // ͼƬݵTexture2D + + Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); + m_previewImage.sprite = sprite; + m_previewImage.color = new Color(1, 1, 1, 0.7f); + m_previewImage.SetNativeSize(); + } + else + { + Debug.LogError("File not found at path: " + path); + } + } + + // ʱ + public void SetTransform(float rotiation, float2 scale, bool similarityCalc) + { + this.rotiation = rotiation; + this.scale = scale; + this.similarityCalc = similarityCalc; + + this.needFillTransform = true; + } + + // ʱ + public void ClearFillTransform() => this.needFillTransform = false; + + public override void ApplyTransform(Transform tf) + { + if (needFillTransform) + { + if (similarityCalc) + { + var rt = tf as RectTransform; + rt.anchorMax = rt.anchorMin; + rt.sizeDelta = new Vector2(ElementInfo.w, ElementInfo.h); + + rt.rotation = Quaternion.identity; + } + else + { + var rt = tf as RectTransform; + // ĵΪ0.5 0.5 + rt.pivot = new Vector2(0.5f, 0.5f); + rt.rotation = Quaternion.Euler(0, 0, rotiation * -1); + + // size + var img = tf.GetComponent(); + img.SetNativeSize(); + rt.anchorMax = rt.anchorMin; + rt.sizeDelta = new Vector2(rt.sizeDelta.x * scale.x, rt.sizeDelta.y * scale.y); + } + } + var position = ElementInfo.Position; + tf.position = StageManager.Instance.PrefabContentsRoot.transform.TransformPoint(new Vector3(position.x, position.y, 0)); } } } diff --git a/Assets/Editor/Entity/TextEntity.cs b/Assets/Editor/Entity/TextEntity.cs index 1eb90b5..38981d2 100644 --- a/Assets/Editor/Entity/TextEntity.cs +++ b/Assets/Editor/Entity/TextEntity.cs @@ -1,36 +1,47 @@ #if UNITY_EDITOR using System.Collections; using System.Collections.Generic; +using UguiToolkit; using UguiToolkit.Editor; using UnityEngine; using UnityEngine.UI; -public class TextEntity : BaseEntity +namespace UguiToolkit.Editor { - private Text m_previewText; - - protected override void OnApplyData(Text ui) + public class TextEntity : BaseEntity { - ui.text = ElementInfo.text; - ui.fontSize = (int)ElementInfo.size; - ui.color = ElementInfo.color; - ui.alignment = TextAnchor.MiddleCenter; + private Text m_previewText; - 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)) + protected override void OnApplyData(Text ui) { - m_previewText = gameObject.AddComponent(); - } - OnApplyData(m_previewText); + ui.text = ElementInfo.text; + ui.fontSize = (int)ElementInfo.size; + ui.color = ElementInfo.color; + ui.alignment = TextAnchor.MiddleCenter; - ApplyTransform(transform); + 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); + } + + public override void ApplyTransform(Transform tf) + { + var position = ElementInfo.Position; + tf.position = StageManager.Instance.PrefabContentsRoot.transform.TransformPoint(new Vector3(position.x, position.y, 0)); + tf.rotation = Quaternion.identity; + } } } #endif \ No newline at end of file diff --git a/Assets/Editor/Manager/EntityManager.cs b/Assets/Editor/Manager/EntityManager.cs index 6aeb89d..9932dca 100644 --- a/Assets/Editor/Manager/EntityManager.cs +++ b/Assets/Editor/Manager/EntityManager.cs @@ -16,6 +16,7 @@ namespace UguiToolkit.Editor private Transform m_entityRoot; private GameObject m_lastSelectionGo; private IEntity m_lastSelectionEntity; + private GameObject m_curSelectionGo; private List m_imageEntities; private List m_textEntities; @@ -47,9 +48,9 @@ namespace UguiToolkit.Editor private void Update() { // 检测是否到达可选实例矩形内部 - if (m_selectionEntities != null && Selection.activeGameObject != null) + if (m_selectionEntities != null && m_curSelectionGo) { - if (m_lastSelectionGo && m_lastSelectionGo == Selection.activeGameObject) + if (m_lastSelectionGo && m_lastSelectionGo == m_curSelectionGo) { if (m_lastSelectionEntity != null && !m_lastSelectionEntity.IsInside(m_lastSelectionGo.transform)) { @@ -59,24 +60,28 @@ namespace UguiToolkit.Editor return; } - bool isApplyTransform = false; foreach (var entity in m_selectionEntities) { - var tf = Selection.activeGameObject.transform; + var tf = m_curSelectionGo.transform; if (entity.IsInside(tf)) { entity.ApplyTransform(tf); + if (tf.TryGetComponent(out var image)) + { + entity.ApplyData(image); + } + else if (tf.TryGetComponent(out var text)) + { + entity.ApplyData(text); + } - m_lastSelectionGo = Selection.activeGameObject; + m_lastSelectionGo = m_curSelectionGo; m_lastSelectionEntity = entity; Selection.activeGameObject = null; - - isApplyTransform = true; break; } } - if (isApplyTransform) m_selectionEntities.Clear(); } // 检测是否有image变更,及时更新PanelCache @@ -151,10 +156,11 @@ namespace UguiToolkit.Editor private void OnSelectionChanged() { - if (m_noSelection) return; - m_entityRoot.gameObject.SetActive(false); + m_curSelectionGo = null; m_selectionEntities.Clear(); + m_entityRoot.gameObject.SetActive(false); + if (m_noSelection) return; if (Selection.activeGameObject != null && m_panelCache != null) { var activeGameObject = Selection.activeGameObject; @@ -170,6 +176,7 @@ namespace UguiToolkit.Editor m_entityRoot.gameObject.SetActive(true); bool isFind; + bool IsInside = false; foreach (var imgEntity in m_imageEntities) { isFind = false; @@ -189,6 +196,8 @@ namespace UguiToolkit.Editor imgEntity.ShowSelectionImg(true); m_selectionEntities.Add(imgEntity); + if (!IsInside && imgEntity.IsInside(activeGameObject.transform)) IsInside = true; + isFind = true; break; } @@ -196,6 +205,17 @@ namespace UguiToolkit.Editor imgEntity.gameObject.SetActive(isFind); } + if (IsInside) + { + if (m_lastSelectionGo && m_lastSelectionGo == activeGameObject) + { + m_curSelectionGo = activeGameObject; + } + } + else { + m_curSelectionGo = activeGameObject; + } + foreach (var textEntity in m_textEntities) { textEntity.gameObject.SetActive(false); @@ -203,8 +223,34 @@ namespace UguiToolkit.Editor } else if (activeGameObject.TryGetComponent(out var text)) { - + bool IsInside = false; + m_entityRoot.gameObject.SetActive(true); + foreach (var textEntity in m_textEntities) + { + textEntity.ShowSelectionImg(true); + textEntity.gameObject.SetActive(true); + m_selectionEntities.Add(textEntity); + + if (!IsInside && textEntity.IsInside(activeGameObject.transform)) IsInside = true; + } + + if (IsInside) + { + if (m_lastSelectionGo && m_lastSelectionGo == activeGameObject) + { + m_curSelectionGo = activeGameObject; + } + } + else + { + m_curSelectionGo = activeGameObject; + } + + foreach (var imgEntity in m_imageEntities) + { + imgEntity.gameObject.SetActive(false); + } } } }