AlicizaX/Client/Packages/com.alicizax.unity.ui/Runtime/UI/UIBase/UIWidget.cs

56 lines
1.4 KiB
C#
Raw Normal View History

2025-03-04 18:40:14 +08:00
using System;
using UnityEngine;
using UnityEngine.UI;
namespace AlicizaX.UI.Runtime
{
public abstract class UIWidget : UIBase
{
private UIBase _parent;
internal UIBase Parent => _parent;
internal void BindUIHolder(GameObject obj, UIBase parent)
{
if (_state != UIState.CreatedUI)
throw new InvalidOperationException("UI already Created");
var holder = (UIHolderObjectBase)obj.GetComponent(UIHolderType);
if (holder == null)
{
throw new InvalidCastException($"资源{obj.name}上不存在{UIHolderType.FullName}");
}
Holder = holder;
_parent = parent;
_canvas = Holder.transform.GetComponent<Canvas>();
_raycaster = Holder.transform.GetComponent<GraphicRaycaster>();
Depth = parent.Depth + 5;
_state = UIState.Loaded;
}
public void Open()
{
InternalOpen();
}
public void Close()
{
InternalClose();
}
public void Destroy()
{
Parent.RemoveWidget(this);
}
}
public abstract class UIWidget<T> : UIWidget where T : UIHolderObjectBase
{
protected T baseui => (T)Holder;
internal override Type UIHolderType => typeof(T);
}
}