整理代码,优化操作
This commit is contained in:
parent
919c07be69
commit
97f6cc2a3c
@ -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
|
@ -1,11 +1,24 @@
|
|||||||
#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
|
||||||
{
|
{
|
||||||
|
public class ImageEntity : BaseEntity<Image, LayoutInfo.ImageInfo>
|
||||||
|
{
|
||||||
|
[ShowInInspector]
|
||||||
|
private float rotiation;
|
||||||
|
[ShowInInspector]
|
||||||
|
private float2 scale;
|
||||||
|
[ShowInInspector]
|
||||||
|
private bool similarityCalc;
|
||||||
|
[ShowInInspector]
|
||||||
|
private bool needFillTransform;
|
||||||
|
|
||||||
private Image m_previewImage;
|
private Image m_previewImage;
|
||||||
|
|
||||||
protected override void OnApplyData(Image ui)
|
protected override void OnApplyData(Image ui)
|
||||||
@ -13,7 +26,8 @@ public class ImageEntity : BaseEntity<Image, LayoutInfo.ImageInfo>
|
|||||||
ApplyTransform(ui.transform);
|
ApplyTransform(ui.transform);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void InitPreviewImage() {
|
public void InitPreviewImage()
|
||||||
|
{
|
||||||
if (ElementInfo == null) return;
|
if (ElementInfo == null) return;
|
||||||
|
|
||||||
if (!TryGetComponent<Image>(out m_previewImage))
|
if (!TryGetComponent<Image>(out m_previewImage))
|
||||||
@ -43,5 +57,49 @@ public class ImageEntity : BaseEntity<Image, LayoutInfo.ImageInfo>
|
|||||||
Debug.LogError("File not found at path: " + path);
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
@ -1,12 +1,15 @@
|
|||||||
#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
|
||||||
{
|
{
|
||||||
|
public class TextEntity : BaseEntity<Text, LayoutInfo.TextInfo>
|
||||||
|
{
|
||||||
private Text m_previewText;
|
private Text m_previewText;
|
||||||
|
|
||||||
protected override void OnApplyData(Text ui)
|
protected override void OnApplyData(Text ui)
|
||||||
@ -32,5 +35,13 @@ public class TextEntity : BaseEntity<Text, LayoutInfo.TextInfo>
|
|||||||
|
|
||||||
ApplyTransform(transform);
|
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
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user