整理代码,优化操作

This commit is contained in:
Soviby 2024-11-07 01:45:57 +08:00
parent 919c07be69
commit 97f6cc2a3c
4 changed files with 181 additions and 110 deletions

View File

@ -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>(T ui) where T: MonoBehaviour;
}
public abstract class BaseEntity<T1,T2> : 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>(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<UnityEngine.UI.Image>();
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

View File

@ -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<Image, LayoutInfo.ImageInfo>
namespace UguiToolkit.Editor
{
private Image m_previewImage;
protected override void OnApplyData(Image ui)
public class ImageEntity : BaseEntity<Image, LayoutInfo.ImageInfo>
{
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<Image>(out m_previewImage))
protected override void OnApplyData(Image ui)
{
m_previewImage = gameObject.AddComponent<Image>();
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<Image>(out m_previewImage))
{
m_previewImage = gameObject.AddComponent<Image>();
}
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<Image>();
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));
}
}
}

View File

@ -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<Text, LayoutInfo.TextInfo>
namespace UguiToolkit.Editor
{
private Text m_previewText;
protected override void OnApplyData(Text ui)
public class TextEntity : BaseEntity<Text, LayoutInfo.TextInfo>
{
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<Text>(out m_previewText))
protected override void OnApplyData(Text ui)
{
m_previewText = gameObject.AddComponent<Text>();
}
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<Text>(out m_previewText))
{
m_previewText = gameObject.AddComponent<Text>();
}
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

View File

@ -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<ImageEntity> m_imageEntities;
private List<TextEntity> 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<UnityEngine.UI.Image>(out var image))
{
entity.ApplyData(image);
}
else if (tf.TryGetComponent<UnityEngine.UI.Text>(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<UnityEngine.UI.Text>(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);
}
}
}
}