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(); } protected override void Init() { var canvas = GetComponentInParent(); canvasCamera = canvas.worldCamera; canvasTrasnfrom = canvas.GetComponent(); } 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; } } } }