com.alicizax.unity.ui.exten.../Runtime/RecyclerView/ViewHolder/ViewHolder.cs

82 lines
1.9 KiB
C#
Raw Normal View History

2025-03-12 20:59:12 +08:00
using System;
using UnityEngine;
namespace AlicizaX.UI
2025-03-12 20:59:12 +08:00
{
public abstract class ViewHolder : MonoBehaviour
{
private RectTransform rectTransform;
private Action clickAction;
private Action pointerEnterAction;
private Action pointerExitAction;
internal event Action<ViewHolder> Destroyed;
2025-03-28 13:34:33 +08:00
2025-03-12 20:59:12 +08:00
public RectTransform RectTransform
{
get
{
if (rectTransform == null)
{
rectTransform = GetComponent<RectTransform>();
}
2025-03-28 13:34:33 +08:00
2025-03-12 20:59:12 +08:00
return rectTransform;
}
private set => rectTransform = value;
2025-03-12 20:59:12 +08:00
}
2025-05-30 13:43:08 +08:00
public string Name { get; internal set; }
2026-03-11 14:18:07 +08:00
2025-05-30 13:43:08 +08:00
public int Index { get; internal set; }
2025-03-12 20:59:12 +08:00
public Vector2 SizeDelta => RectTransform.sizeDelta;
2026-03-11 14:18:07 +08:00
2025-05-30 13:43:08 +08:00
protected internal virtual void OnRecycled()
{
}
2025-03-12 20:59:12 +08:00
internal void SetInteractionCallbacks(
Action clickAction = null,
Action pointerEnterAction = null,
Action pointerExitAction = null)
{
this.clickAction = clickAction;
this.pointerEnterAction = pointerEnterAction;
this.pointerExitAction = pointerExitAction;
}
public void ClearInteractionCallbacks()
{
clickAction = null;
pointerEnterAction = null;
pointerExitAction = null;
}
2025-05-30 13:43:08 +08:00
2026-03-30 16:00:20 +08:00
protected virtual void OnClearInteractionCallbacks()
{
}
protected void InvokeClickAction()
2025-03-12 20:59:12 +08:00
{
clickAction?.Invoke();
2025-03-12 20:59:12 +08:00
}
protected void InvokePointerEnterAction()
{
pointerEnterAction?.Invoke();
}
2026-03-27 14:38:26 +08:00
protected void InvokePointerExitAction()
2025-03-28 13:34:33 +08:00
{
pointerExitAction?.Invoke();
2025-03-28 13:34:33 +08:00
}
2025-04-01 15:21:02 +08:00
protected virtual void OnDestroy()
2025-04-02 14:53:08 +08:00
{
Destroyed?.Invoke(this);
Destroyed = null;
2025-04-02 14:53:08 +08:00
}
2025-03-12 20:59:12 +08:00
}
}