52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
using UnityEngine;
|
|
|
|
namespace BF
|
|
{
|
|
public class UICoordinateHelper : MonoBehaviour
|
|
{
|
|
private Camera uiCamera;
|
|
private Camera mainCamera;
|
|
public float LocalPointX { get; private set; }
|
|
public float LocalPointY { get; private set; }
|
|
|
|
public void SetUICamera(Camera camera)
|
|
{
|
|
uiCamera = camera;
|
|
}
|
|
|
|
public void SetMainCamera(Camera camera)
|
|
{
|
|
mainCamera = camera;
|
|
}
|
|
|
|
public void RectTransformScreenPointToLocalPointInRectangle(float x, float y)
|
|
{
|
|
if (ReferenceEquals(uiCamera, null))
|
|
{
|
|
return;
|
|
}
|
|
Vector2 lp;
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(this.transform as RectTransform, new Vector2(x, y), uiCamera, out lp);
|
|
LocalPointX = lp.x;
|
|
LocalPointY = lp.y;
|
|
}
|
|
|
|
public void WorldPointToLocalPointInRectangle(float x, float y, float z)
|
|
{
|
|
if (ReferenceEquals(uiCamera, null))
|
|
{
|
|
return;
|
|
}
|
|
if (ReferenceEquals(mainCamera, null))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var screenPosition = mainCamera.WorldToScreenPoint(new Vector3(x, y, z));
|
|
Vector2 lp;
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(this.transform as RectTransform, new Vector2(screenPosition.x, screenPosition.y), uiCamera, out lp);
|
|
LocalPointX = lp.x;
|
|
LocalPointY = lp.y;
|
|
}
|
|
}
|
|
} |