整理代码,优化操作

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 #if UNITY_EDITOR
using Sirenix.OdinInspector; using System;
using Unity.Mathematics; using Unity.Mathematics;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
@ -8,28 +8,22 @@ namespace UguiToolkit.Editor
{ {
public interface IEntity public interface IEntity
{ {
void SetTransform(float rotiation, float2 scale, bool similarityCalc);
void ApplyTransform(Transform tf); void ApplyTransform(Transform tf);
bool IsInside(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 public abstract class BaseEntity<T1,T2> : MonoBehaviour, IEntity where T1 : MonoBehaviour where T2 : LayoutInfo.ElementInfo
{ {
// ElementInfo // ElementInfo
private T2 m_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; private UnityEngine.UI.Image m_selectionImg;
public T2 ElementInfo => m_elementInfo; public T2 ElementInfo => m_elementInfo;
public abstract void ApplyTransform(Transform tf);
protected abstract void OnApplyData(T1 ui);
public void ShowSelectionImg(bool show) public void ShowSelectionImg(bool show)
{ {
if (m_selectionImg) if (m_selectionImg)
@ -45,42 +39,6 @@ namespace UguiToolkit.Editor
return rect.Contains(pos); 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) public void SetData(T2 elementInfo)
{ {
@ -88,9 +46,9 @@ namespace UguiToolkit.Editor
CreateSelectionImg(); CreateSelectionImg();
} }
public void ApplyData(T1 ui) public void ApplyData<T>(T ui) where T : MonoBehaviour
{ {
OnApplyData(ui); OnApplyData(ui as T1);
} }
private void CreateSelectionImg() private void CreateSelectionImg()
@ -107,10 +65,8 @@ 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.3f); m_selectionImg.color = new Color(0, 1, 0, 0.2f);
} }
protected virtual void OnApplyData(T1 ui) { }
} }
} }
#endif #endif

View File

@ -1,46 +1,104 @@
#if UNITY_EDITOR #if UNITY_EDITOR
using Sirenix.OdinInspector;
using UguiToolkit.Editor; using UguiToolkit.Editor;
using Unity.Mathematics;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
public class ImageEntity : BaseEntity<Image, LayoutInfo.ImageInfo> namespace UguiToolkit.Editor
{ {
private Image m_previewImage; public class ImageEntity : BaseEntity<Image, LayoutInfo.ImageInfo>
protected override void OnApplyData(Image ui)
{ {
ApplyTransform(ui.transform); [ShowInInspector]
} private float rotiation;
[ShowInInspector]
private float2 scale;
[ShowInInspector]
private bool similarityCalc;
[ShowInInspector]
private bool needFillTransform;
public void InitPreviewImage() { private Image m_previewImage;
if (ElementInfo == null) return;
if (!TryGetComponent<Image>(out m_previewImage)) protected override void OnApplyData(Image ui)
{ {
m_previewImage = gameObject.AddComponent<Image>(); ApplyTransform(ui.transform);
} }
LoadImageFromFile(ElementInfo.imgPath); public void InitPreviewImage()
ApplyTransform(transform);
}
void LoadImageFromFile(string path)
{
if (System.IO.File.Exists(path))
{ {
byte[] fileData = System.IO.File.ReadAllBytes(path); if (ElementInfo == null) return;
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)); if (!TryGetComponent<Image>(out m_previewImage))
m_previewImage.sprite = sprite; {
m_previewImage.color = new Color(1, 1, 1, 0.7f); m_previewImage = gameObject.AddComponent<Image>();
m_previewImage.SetNativeSize(); }
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 #if UNITY_EDITOR
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UguiToolkit;
using UguiToolkit.Editor; using UguiToolkit.Editor;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
public class TextEntity : BaseEntity<Text, LayoutInfo.TextInfo> namespace UguiToolkit.Editor
{ {
private Text m_previewText; public class TextEntity : BaseEntity<Text, LayoutInfo.TextInfo>
protected override void OnApplyData(Text ui)
{ {
ui.text = ElementInfo.text; private Text m_previewText;
ui.fontSize = (int)ElementInfo.size;
ui.color = ElementInfo.color;
ui.alignment = TextAnchor.MiddleCenter;
var rectTransform = ui.rectTransform; protected override void OnApplyData(Text ui)
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>(); ui.text = ElementInfo.text;
} ui.fontSize = (int)ElementInfo.size;
OnApplyData(m_previewText); 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 #endif

View File

@ -16,6 +16,7 @@ namespace UguiToolkit.Editor
private Transform m_entityRoot; private Transform m_entityRoot;
private GameObject m_lastSelectionGo; private GameObject m_lastSelectionGo;
private IEntity m_lastSelectionEntity; private IEntity m_lastSelectionEntity;
private GameObject m_curSelectionGo;
private List<ImageEntity> m_imageEntities; private List<ImageEntity> m_imageEntities;
private List<TextEntity> m_textEntities; private List<TextEntity> m_textEntities;
@ -47,9 +48,9 @@ namespace UguiToolkit.Editor
private void Update() 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)) if (m_lastSelectionEntity != null && !m_lastSelectionEntity.IsInside(m_lastSelectionGo.transform))
{ {
@ -59,24 +60,28 @@ namespace UguiToolkit.Editor
return; return;
} }
bool isApplyTransform = false;
foreach (var entity in m_selectionEntities) foreach (var entity in m_selectionEntities)
{ {
var tf = Selection.activeGameObject.transform; var tf = m_curSelectionGo.transform;
if (entity.IsInside(tf)) if (entity.IsInside(tf))
{ {
entity.ApplyTransform(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; m_lastSelectionEntity = entity;
Selection.activeGameObject = null; Selection.activeGameObject = null;
isApplyTransform = true;
break; break;
} }
} }
if (isApplyTransform) m_selectionEntities.Clear();
} }
// 检测是否有image变更及时更新PanelCache // 检测是否有image变更及时更新PanelCache
@ -151,10 +156,11 @@ namespace UguiToolkit.Editor
private void OnSelectionChanged() private void OnSelectionChanged()
{ {
if (m_noSelection) return; m_curSelectionGo = null;
m_entityRoot.gameObject.SetActive(false);
m_selectionEntities.Clear(); m_selectionEntities.Clear();
m_entityRoot.gameObject.SetActive(false);
if (m_noSelection) return;
if (Selection.activeGameObject != null && m_panelCache != null) if (Selection.activeGameObject != null && m_panelCache != null)
{ {
var activeGameObject = Selection.activeGameObject; var activeGameObject = Selection.activeGameObject;
@ -170,6 +176,7 @@ namespace UguiToolkit.Editor
m_entityRoot.gameObject.SetActive(true); m_entityRoot.gameObject.SetActive(true);
bool isFind; bool isFind;
bool IsInside = false;
foreach (var imgEntity in m_imageEntities) foreach (var imgEntity in m_imageEntities)
{ {
isFind = false; isFind = false;
@ -189,6 +196,8 @@ namespace UguiToolkit.Editor
imgEntity.ShowSelectionImg(true); imgEntity.ShowSelectionImg(true);
m_selectionEntities.Add(imgEntity); m_selectionEntities.Add(imgEntity);
if (!IsInside && imgEntity.IsInside(activeGameObject.transform)) IsInside = true;
isFind = true; isFind = true;
break; break;
} }
@ -196,6 +205,17 @@ namespace UguiToolkit.Editor
imgEntity.gameObject.SetActive(isFind); 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) foreach (var textEntity in m_textEntities)
{ {
textEntity.gameObject.SetActive(false); textEntity.gameObject.SetActive(false);
@ -203,8 +223,34 @@ namespace UguiToolkit.Editor
} }
else if (activeGameObject.TryGetComponent<UnityEngine.UI.Text>(out var text)) 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);
}
} }
} }
} }