com.soviby.unity.ui.ugui-to.../Assets/Editor/Entity/IEntity.cs

77 lines
2.3 KiB
C#
Raw Normal View History

2024-10-23 17:30:59 +00:00
#if UNITY_EDITOR
2024-11-06 17:45:57 +00:00
using System;
2024-10-21 16:20:39 +00:00
using Unity.Mathematics;
using UnityEngine;
2024-10-29 17:51:06 +00:00
using UnityEngine.UIElements;
2024-10-21 16:20:39 +00:00
namespace UguiToolkit.Editor
{
public interface IEntity
{
2024-10-29 17:51:06 +00:00
void ApplyTransform(Transform tf);
bool IsInside(Transform tf);
2024-11-06 17:45:57 +00:00
void ApplyData<T>(T ui) where T: MonoBehaviour;
2024-12-01 16:33:27 +00:00
void InitPreview();
void ShowSelectionImg(bool show);
GameObject gameObject { get; }
2024-10-21 16:20:39 +00:00
}
public abstract class BaseEntity<T1,T2> : MonoBehaviour, IEntity where T1 : MonoBehaviour where T2 : LayoutInfo.ElementInfo
{
// ElementInfo
private T2 m_elementInfo;
2024-10-29 17:51:06 +00:00
private UnityEngine.UI.Image m_selectionImg;
2024-10-21 16:20:39 +00:00
2024-10-29 17:51:06 +00:00
public T2 ElementInfo => m_elementInfo;
public void ShowSelectionImg(bool show)
{
if (m_selectionImg)
{
m_selectionImg.gameObject.SetActive(show);
}
}
2024-12-01 16:33:27 +00:00
public abstract void ApplyTransform(Transform tf);
public abstract void InitPreview();
protected abstract void OnApplyData(T1 ui);
2024-10-29 17:51:06 +00:00
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);
}
2024-10-21 16:20:39 +00:00
// 创建时调用
public void SetData(T2 elementInfo)
{
this.m_elementInfo = elementInfo;
2024-10-29 17:51:06 +00:00
CreateSelectionImg();
2024-10-21 16:20:39 +00:00
}
2024-11-06 17:45:57 +00:00
public void ApplyData<T>(T ui) where T : MonoBehaviour
2024-10-21 16:20:39 +00:00
{
2024-11-06 17:45:57 +00:00
OnApplyData(ui as T1);
2024-10-21 16:20:39 +00:00
}
2024-10-29 17:51:06 +00:00
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>();
2024-12-01 16:33:27 +00:00
m_selectionImg.color = new Color(0, 1, 0, 0.15f);
2024-10-29 17:51:06 +00:00
}
2024-10-21 16:20:39 +00:00
}
2024-10-23 17:30:59 +00:00
}
#endif