ProjectDDD/Packages/SLUnity/SLUI/SLUIPositionToScreen.cs
2025-06-25 11:33:17 +09:00

47 lines
1.3 KiB (Stored with Git LFS)
C#

using UnityEngine;
namespace Superlazy.UI
{
public class SLUIPositionToScreen : SLUIComponent
{
public string bind = "Position";
private RectTransform t;
private Camera canvasCamera;
private RectTransform canvasTrasnfrom;
protected override void Validate()
{
t = GetComponent<RectTransform>();
}
protected override void Init()
{
var canvas = GetComponentInParent<Canvas>();
canvasCamera = canvas.worldCamera;
canvasTrasnfrom = canvas.GetComponent<RectTransform>();
}
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;
Vector3 screenPosition = SLGame.Camera.WorldToScreenPoint(SLGame.SessionGet(bindParent.BindPath).Get(bind).ToVector3());
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasTrasnfrom, screenPosition, canvasCamera, out Vector2 localPoint))
{
t.anchoredPosition = localPoint;
}
}
}
}