因为RectTransformUtility里面提供的函数是环形结构,所以屏幕坐标,UGUI坐标,世界坐标三者之间可以相互转换。
由于函数都需要用到场景相机和UGUI相机,所以定义两个相机变量。
///
/// 场景相机
///
public static Camera ScenceCamera;
///
/// UGUI相机
///
public static Camera UGUICamera;
下面开始看坐标系之间的转换接口。
世界坐标->屏幕坐标
WorldToScreenPoint
/// <summary>
/// 世界坐标转屏幕坐标
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public static Vector2 WorldToScreenPoint(Vector3 worldPoint)
{
return RectTransformUtility.WorldToScreenPoint(ScenceCamera, worldPoint);
}
/// <summary>
/// 世界坐标转屏幕坐标
/// </summary>
/// <param name="camera"></param>
/// <param name="position"></param>
/// <returns></returns>
public static Vector2 WorldToScreenPoint(Camera cam, Vector3 worldPoint)
{
return RectTransformUtility.WorldToScreenPoint(cam, worldPoint);
}
屏幕坐标->世界坐标
ScreenPointToWorldPointInRectangle
/// <summary>
/// 屏幕坐标转世界坐标
/// </summary>
/// <param name="rect"></param>
/// <param name="screenPoint"></param>
/// <param name="worldPoint"></param>
/// <returns></returns>
public static bool ScreenPointToWorldPointInRectangle(RectTransform rect, Vector2 screenPoint, out Vector3 worldPoint)
{
return RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, screenPoint, UGUICamera, out worldPoint);
}
/// <summary>
/// 屏幕坐标转世界坐标
/// </summary>
/// <param name="rect"></param>
/// <param name="screenPoint"></param>
/// <param name="cam"></param>
/// <param name="worldPoint"></param>
/// <returns></returns>
public static bool ScreenPointToWorldPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector3 worldPoint)
{
return RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, screenPoint, cam, out worldPoint);
}
屏幕坐标->UGUI坐标
ScreenPointToLocalPointInRectangle
/// <summary>
/// 屏幕坐标转某个RectTransform下的localPosition坐标
/// </summary>
/// <param name="rect"></param>
/// <param name="screenPoint"></param>
/// <param name="localPoint"></param>
/// <returns></returns>
public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, out Vector2 localPoint)
{
return RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, screenPoint, UGUICamera, out localPoint);
}
/// <summary>
/// 屏幕坐标转某个RectTransform下的localPosition坐标
/// </summary>
/// <param name="rect"></param>
/// <param name="screenPoint"></param>
/// <param name="cam"></param>
/// <param name="localPoint"></param>
/// <returns></returns>
public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint)
{
return RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, screenPoint, cam, out localPoint);
}
UGUI坐标->世界坐标
transform.position 就是UGUI下面的世界坐标,不用转换
UGUI坐标->屏幕坐标
UGUI坐标->世界坐标->屏幕坐标
世界坐标->UGUI坐标
世界坐标->屏幕坐标-> UGUI坐标
完整代码->TransformUtility.cs
using UnityEngine;
public class TransformUtility
{
/// <summary>
/// 场景相机
/// </summary>
public static Camera ScenceCamera;
/// <summary>
/// UGUI相机
/// </summary>
public static Camera UGUICamera;
#region 世界坐标转屏幕坐标
/// <summary>
/// 世界坐标转屏幕坐标
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public static Vector2 WorldToScreenPoint(Vector3 worldPoint)
{
return RectTransformUtility.WorldToScreenPoint(ScenceCamera, worldPoint);
}
/// <summary>
/// 世界坐标转屏幕坐标
/// </summary>
/// <param name="camera"></param>
/// <param name="position"></param>
/// <returns></returns>
public static Vector2 WorldToScreenPoint(Camera cam, Vector3 worldPoint)
{
return RectTransformUtility.WorldToScreenPoint(cam, worldPoint);
}
#endregion
#region 屏幕坐标转世界坐标
/// <summary>
/// 屏幕坐标转世界坐标
/// </summary>
/// <param name="rect"></param>
/// <param name="screenPoint"></param>
/// <param name="worldPoint"></param>
/// <returns></returns>
public static bool ScreenPointToWorldPointInRectangle(RectTransform rect, Vector2 screenPoint, out Vector3 worldPoint)
{
return RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, screenPoint, UGUICamera, out worldPoint);
}
/// <summary>
/// 屏幕坐标转世界坐标
/// </summary>
/// <param name="rect"></param>
/// <param name="screenPoint"></param>
/// <param name="cam"></param>
/// <param name="worldPoint"></param>
/// <returns></returns>
public static bool ScreenPointToWorldPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector3 worldPoint)
{
return RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, screenPoint, cam, out worldPoint);
}
#endregion
#region 屏幕坐标转UGUI坐标
/// <summary>
/// 屏幕坐标转某个RectTransform下的localPosition坐标
/// </summary>
/// <param name="rect"></param>
/// <param name="screenPoint"></param>
/// <param name="localPoint"></param>
/// <returns></returns>
public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, out Vector2 localPoint)
{
return RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, screenPoint, UGUICamera, out localPoint);
}
/// <summary>
/// 屏幕坐标转某个RectTransform下的localPosition坐标
/// </summary>
/// <param name="rect"></param>
/// <param name="screenPoint"></param>
/// <param name="cam"></param>
/// <param name="localPoint"></param>
/// <returns></returns>
public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint)
{
return RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, screenPoint, cam, out localPoint);
}
#endregion
}