diff --git a/Assets/Editor/Entity/IEntity.cs b/Assets/Editor/Entity/IEntity.cs index e97ffa3..54b1d51 100644 --- a/Assets/Editor/Entity/IEntity.cs +++ b/Assets/Editor/Entity/IEntity.cs @@ -70,7 +70,7 @@ 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.2f); + m_selectionImg.color = new Color(0, 1, 0, 0.3f); } } } diff --git a/Assets/Editor/Entity/ImageEntity.cs b/Assets/Editor/Entity/ImageEntity.cs index c9d8666..dadf02c 100644 --- a/Assets/Editor/Entity/ImageEntity.cs +++ b/Assets/Editor/Entity/ImageEntity.cs @@ -41,7 +41,7 @@ namespace UguiToolkit.Editor 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.color = new Color(1, 1, 1, 0.5f); m_previewImage.SetNativeSize(); } else diff --git a/Assets/Editor/Entity/TextMeshProEntity.cs b/Assets/Editor/Entity/TextMeshProEntity.cs index ffda10b..e2b1fbc 100644 --- a/Assets/Editor/Entity/TextMeshProEntity.cs +++ b/Assets/Editor/Entity/TextMeshProEntity.cs @@ -1,5 +1,4 @@ #if UNITY_EDITOR -using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using Sirenix.OdinInspector; using Sirenix.Utilities; using System.Linq; diff --git a/Assets/Editor/Manager/EntityManager.cs b/Assets/Editor/Manager/EntityManager.cs index 25875b6..87c6b6e 100644 --- a/Assets/Editor/Manager/EntityManager.cs +++ b/Assets/Editor/Manager/EntityManager.cs @@ -1,10 +1,12 @@ #if UNITY_EDITOR using Sirenix.OdinInspector; using System.Collections.Generic; +using System.Globalization; using System.IO; using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine; +using UnityEngine.UI; namespace UguiToolkit.Editor { @@ -13,6 +15,7 @@ namespace UguiToolkit.Editor { private PanelCache m_panelCache; private Transform m_entityRoot; + private Transform m_background; private GameObject m_lastSelectionGo; private IEntity m_lastSelectionEntity; private GameObject m_curSelectionGo; @@ -26,11 +29,11 @@ namespace UguiToolkit.Editor private StageManager m_stageManager; private HashSet m_checkImgPaths; - private float m_lastCheckTime; + private float m_lastCheckTime = 0f; private bool m_useTMP; - private const float m_checkInterval = 2f; - + private const float m_checkInterval = 3f; + private void OnEnable() { var globalMng = GlobalManager.Instance; @@ -92,19 +95,24 @@ namespace UguiToolkit.Editor } // 检测是否有image变更,及时更新PanelCache - m_lastCheckTime += Time.deltaTime; - if (m_lastCheckTime > m_checkInterval) + m_lastCheckTime -= Time.deltaTime; + CheckPanelCache(); + } + + private void CheckPanelCache() + { + if (m_lastCheckTime <= 0) { - m_lastCheckTime = 0; + m_lastCheckTime = m_checkInterval; if (m_checkImgPaths != null) { var images = m_stageManager.PrefabContentsRoot.GetComponentsInChildren(); foreach (var image in images) { - if (image.transform.parent == m_entityRoot || image.sprite == null) continue; + if (image.transform.parent == m_entityRoot || image.transform.parent == m_background || image.sprite == null) continue; var srcImgPath = AssetDatabase.GetAssetPath(image.sprite); - if (!m_checkImgPaths.Contains(srcImgPath)) + if (!string.IsNullOrEmpty(srcImgPath) && !m_checkImgPaths.Contains(srcImgPath)) { var srcImgDirPath = System.IO.Path.GetDirectoryName(srcImgPath); string projectPath = Directory.GetParent(Application.dataPath).FullName; @@ -122,6 +130,44 @@ namespace UguiToolkit.Editor } } + private void InitBackground() + { + if (m_background) DestroyImmediate(m_background.gameObject); + + var go = new GameObject("__background__", typeof(RectTransform)); + UpdateHierarchyOfEntity(false, go); + m_background = go.transform; + m_background.SetParent(transform); + m_background.localPosition = Vector3.zero; + m_background.localRotation = Quaternion.identity; + m_background.localScale = Vector3.one; + + var canvas = m_background.gameObject.AddComponent(); + canvas.pixelPerfect = true; + canvas.overrideSorting = true; + canvas.sortingOrder = -100; + + var imgGo = new GameObject("_", typeof(RectTransform)); + var imgTf = imgGo.transform; + imgTf.SetParent(m_background); + imgTf.localPosition = Vector3.zero; + imgTf.localRotation = Quaternion.identity; + imgTf.localScale = Vector3.one; + var img = imgGo.AddComponent(); + var imgPath = System.IO.Path.Join(m_panelCache.TargetImgDirPath, "__background__.png"); + if (System.IO.File.Exists(imgPath)) + { + byte[] fileData = System.IO.File.ReadAllBytes(imgPath); + 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)); + img.sprite = sprite; + img.color = new Color(1, 1, 1, 0.4f); + img.SetNativeSize(); + } + } + private void InitCheckImgPaths() { if (m_checkImgPaths == null) m_checkImgPaths = new HashSet(); @@ -273,11 +319,15 @@ namespace UguiToolkit.Editor // 创建所有实例 CreateAllEntity(); InitCheckImgPaths(); + InitBackground(); + + CheckPanelCache(); } private void OnUpdateHierarchyOfEntityAllEntity(bool show) { UpdateHierarchyOfEntity(show, m_entityRoot.gameObject); + UpdateHierarchyOfEntity(show, m_background.gameObject); foreach (var entity in m_imageEntities) UpdateHierarchyOfEntity(show, entity.gameObject); foreach (var entity in m_textEntities) UpdateHierarchyOfEntity(show, entity.gameObject); }