添加背景

This commit is contained in:
Soviby 2024-12-03 01:47:50 +08:00
parent 6b9f045749
commit 7906c0c87c
4 changed files with 60 additions and 11 deletions

View File

@ -70,7 +70,7 @@ namespace UguiToolkit.Editor
rtf.sizeDelta = new Vector2(m_elementInfo.w, m_elementInfo.h); rtf.sizeDelta = new Vector2(m_elementInfo.w, m_elementInfo.h);
m_selectionImg = go.AddComponent<UnityEngine.UI.Image>(); m_selectionImg = go.AddComponent<UnityEngine.UI.Image>();
m_selectionImg.color = new Color(0, 1, 0, 0.2f); m_selectionImg.color = new Color(0, 1, 0, 0.3f);
} }
} }
} }

View File

@ -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)); 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.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(); m_previewImage.SetNativeSize();
} }
else else

View File

@ -1,5 +1,4 @@
#if UNITY_EDITOR #if UNITY_EDITOR
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using Sirenix.OdinInspector; using Sirenix.OdinInspector;
using Sirenix.Utilities; using Sirenix.Utilities;
using System.Linq; using System.Linq;

View File

@ -1,10 +1,12 @@
#if UNITY_EDITOR #if UNITY_EDITOR
using Sirenix.OdinInspector; using Sirenix.OdinInspector;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.IO; using System.IO;
using UnityEditor; using UnityEditor;
using UnityEditor.SceneManagement; using UnityEditor.SceneManagement;
using UnityEngine; using UnityEngine;
using UnityEngine.UI;
namespace UguiToolkit.Editor namespace UguiToolkit.Editor
{ {
@ -13,6 +15,7 @@ namespace UguiToolkit.Editor
{ {
private PanelCache m_panelCache; private PanelCache m_panelCache;
private Transform m_entityRoot; private Transform m_entityRoot;
private Transform m_background;
private GameObject m_lastSelectionGo; private GameObject m_lastSelectionGo;
private IEntity m_lastSelectionEntity; private IEntity m_lastSelectionEntity;
private GameObject m_curSelectionGo; private GameObject m_curSelectionGo;
@ -26,11 +29,11 @@ namespace UguiToolkit.Editor
private StageManager m_stageManager; private StageManager m_stageManager;
private HashSet<string> m_checkImgPaths; private HashSet<string> m_checkImgPaths;
private float m_lastCheckTime; private float m_lastCheckTime = 0f;
private bool m_useTMP; private bool m_useTMP;
private const float m_checkInterval = 2f; private const float m_checkInterval = 3f;
private void OnEnable() private void OnEnable()
{ {
var globalMng = GlobalManager.Instance; var globalMng = GlobalManager.Instance;
@ -92,19 +95,24 @@ namespace UguiToolkit.Editor
} }
// 检测是否有image变更及时更新PanelCache // 检测是否有image变更及时更新PanelCache
m_lastCheckTime += Time.deltaTime; m_lastCheckTime -= Time.deltaTime;
if (m_lastCheckTime > m_checkInterval) CheckPanelCache();
}
private void CheckPanelCache()
{
if (m_lastCheckTime <= 0)
{ {
m_lastCheckTime = 0; m_lastCheckTime = m_checkInterval;
if (m_checkImgPaths != null) if (m_checkImgPaths != null)
{ {
var images = m_stageManager.PrefabContentsRoot.GetComponentsInChildren<UnityEngine.UI.Image>(); var images = m_stageManager.PrefabContentsRoot.GetComponentsInChildren<UnityEngine.UI.Image>();
foreach (var image in images) 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); 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); var srcImgDirPath = System.IO.Path.GetDirectoryName(srcImgPath);
string projectPath = Directory.GetParent(Application.dataPath).FullName; 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>();
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<Image>();
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() private void InitCheckImgPaths()
{ {
if (m_checkImgPaths == null) m_checkImgPaths = new HashSet<string>(); if (m_checkImgPaths == null) m_checkImgPaths = new HashSet<string>();
@ -273,11 +319,15 @@ namespace UguiToolkit.Editor
// 创建所有实例 // 创建所有实例
CreateAllEntity(); CreateAllEntity();
InitCheckImgPaths(); InitCheckImgPaths();
InitBackground();
CheckPanelCache();
} }
private void OnUpdateHierarchyOfEntityAllEntity(bool show) private void OnUpdateHierarchyOfEntityAllEntity(bool show)
{ {
UpdateHierarchyOfEntity(show, m_entityRoot.gameObject); 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_imageEntities) UpdateHierarchyOfEntity(show, entity.gameObject);
foreach (var entity in m_textEntities) UpdateHierarchyOfEntity(show, entity.gameObject); foreach (var entity in m_textEntities) UpdateHierarchyOfEntity(show, entity.gameObject);
} }