#if UNITY_EDITOR using Sirenix.OdinInspector; using Unity.Mathematics; using UnityEngine; using UnityEngine.UIElements; namespace UguiToolkit.Editor { public interface IEntity { void SetTransform(float rotiation, float2 scale); void ApplyTransform(Transform tf); bool IsInside(Transform tf); } public abstract class BaseEntity : 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 needFillTransform; private UnityEngine.UI.Image m_selectionImg; public T2 ElementInfo => m_elementInfo; public void ShowSelectionImg(bool show) { if (m_selectionImg) { m_selectionImg.gameObject.SetActive(show); } } public bool IsInside(Transform tf) { var rect = ElementInfo.Rect; var pos = StageManager.Instance.PrefabContentsRoot.transform.InverseTransformPoint(new Vector3(tf.position.x, tf.position.y, 0)); return rect.Contains(pos); } public void ApplyTransform(Transform tf) { if (needFillTransform) { tf.rotation = Quaternion.Euler(0, 0, rotiation); 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) { this.rotiation = rotiation; this.scale = scale; this.needFillTransform = true; } // 查找时调用 public void ClearFillTransform() => this.needFillTransform = false; // 创建时调用 public void SetData(T2 elementInfo) { this.m_elementInfo = elementInfo; CreateSelectionImg(); } public void ApplyData(T1 ui) { OnApplyData(ui); } private void CreateSelectionImg() { var go = new GameObject("_selectionImg_", typeof(RectTransform)); EntityHelper.UpdateHierarchyAndSceneVisibilityOfEntity(false, go); var tf = go.transform; tf.SetParent(transform); tf.localPosition = Vector3.zero; tf.localRotation = Quaternion.identity; tf.localScale = Vector3.one; var rtf = go.GetComponent(); rtf.sizeDelta = new Vector2(m_elementInfo.w, m_elementInfo.h); m_selectionImg = go.AddComponent(); m_selectionImg.color = new Color(0, 1, 0, 0.3f); } protected virtual void OnApplyData(T1 ui) { } } } #endif