ProjectDDD/Packages/SLUnity/SLUI/SLUI2DPosition.cs
2025-07-08 19:46:31 +09:00

55 lines
1.4 KiB
C#

using UnityEngine;
namespace Superlazy.UI
{
public class SLUI2DPosition : SLUIComponent
{
public string bind = "Position";
public bool screenPos = false;
private RectTransform t;
private Canvas canvas;
private RectTransform parent;
protected override void Validate()
{
t = GetComponent<RectTransform>();
parent = t.parent as RectTransform;
canvas = GetComponentInParent<Canvas>();
}
protected override void Init()
{
}
protected override void Enable()
{
SLGame.AddNotify(bindParent.BindPath.CombinePath(bind), OnChange);
}
protected override void Disable()
{
SLGame.RemoveNotify(bindParent.BindPath.CombinePath(bind), OnChange);
}
private void OnChange()
{
if (bindParent.Active == false)
return;
if (screenPos)
{
var pos = SLGame.SessionGet(bindParent.BindPath).Get(bind).ToVector2();
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, pos, canvas.worldCamera, out var localPoint))
{
t.anchoredPosition = localPoint;
}
}
else
{
t.anchoredPosition = SLGame.SessionGet(bindParent.BindPath).Get(bind).ToVector2();
}
}
}
}