com.soviby.unity.ui.ugui-to.../Assets/Editor/Entity/IEntity.cs
2024-12-03 01:47:50 +08:00

77 lines
2.3 KiB
C#

#if UNITY_EDITOR
using System;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.UIElements;
namespace UguiToolkit.Editor
{
public interface IEntity
{
void ApplyTransform(Transform tf);
bool IsInside(Transform tf);
void ApplyData<T>(T ui) where T: MonoBehaviour;
void InitPreview();
void ShowSelectionImg(bool show);
GameObject gameObject { get; }
}
public abstract class BaseEntity<T1,T2> : MonoBehaviour, IEntity where T1 : MonoBehaviour where T2 : LayoutInfo.ElementInfo
{
// ElementInfo
private T2 m_elementInfo;
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 abstract void ApplyTransform(Transform tf);
public abstract void InitPreview();
protected abstract void OnApplyData(T1 ui);
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 SetData(T2 elementInfo)
{
this.m_elementInfo = elementInfo;
CreateSelectionImg();
}
public void ApplyData<T>(T ui) where T : MonoBehaviour
{
OnApplyData(ui as T1);
}
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<RectTransform>();
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);
}
}
}
#endif