整合SLSUtilities
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace SLSFramework.General
|
||||
{
|
||||
public static class SpaceConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// 世界坐标转换为屏幕坐标
|
||||
/// </summary>
|
||||
/// <param name="worldPoint">屏幕坐标</param>
|
||||
/// <param name="worldCamera">用于转换的摄像机</param>
|
||||
/// <returns></returns>
|
||||
public static Vector2 WorldPointToScreenPoint(Vector3 worldPoint, Camera worldCamera)
|
||||
{
|
||||
// Camera.main 世界摄像机
|
||||
Vector2 screenPoint = worldCamera.WorldToScreenPoint(worldPoint);
|
||||
return screenPoint;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 世界坐标转换为归一化屏幕坐标 (0-1)
|
||||
/// </summary>
|
||||
/// <param name="worldPoint"></param>
|
||||
/// <param name="worldCamera"></param>
|
||||
/// <returns></returns>
|
||||
public static Vector2 WorldPointToNormalizedScreenPoint(Vector3 worldPoint, Camera worldCamera)
|
||||
{
|
||||
Vector2 screenPoint = WorldPointToScreenPoint(worldPoint, worldCamera);
|
||||
Vector2 normalizedScreenPoint = new Vector2(screenPoint.x / Screen.width, screenPoint.y / Screen.height);
|
||||
return normalizedScreenPoint;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 屏幕坐标转换为世界坐标
|
||||
/// </summary>
|
||||
/// <param name="screenPoint">屏幕坐标</param>
|
||||
/// <param name="planeZ">距离摄像机 Z 平面的距离</param>
|
||||
/// <param name="camera">用于转换的摄像机</param>
|
||||
/// <returns></returns>
|
||||
public static Vector3 ScreenPointToWorldPoint(Vector2 screenPoint, float planeZ, Camera worldCamera)
|
||||
{
|
||||
// Camera.main 世界摄像机
|
||||
Vector3 position = new Vector3(screenPoint.x, screenPoint.y, planeZ);
|
||||
Vector3 worldPoint = worldCamera.ScreenToWorldPoint(position);
|
||||
return worldPoint;
|
||||
}
|
||||
|
||||
|
||||
public static Vector2 WorldPointToUILocalPoint(RectTransform rt, Vector3 worldPosition, Camera worldCamera, Camera uiCamera)
|
||||
{
|
||||
// 将世界坐标转换为屏幕坐标
|
||||
var screenPosition = WorldPointToScreenPoint(worldPosition, worldCamera);
|
||||
// 将屏幕坐标转换为 UGUI 坐标
|
||||
Vector2 localPoint = ScreenPointToUILocalPoint(rt, screenPosition, uiCamera);
|
||||
|
||||
return localPoint;
|
||||
}
|
||||
|
||||
public static Vector3 WorldPointToUIPoint(RectTransform rt, Vector3 worldPosition, Camera worldCamera, Camera uiCamera)
|
||||
{
|
||||
// 将世界坐标转换为屏幕坐标
|
||||
var screenPosition = WorldPointToScreenPoint(worldPosition, worldCamera);
|
||||
// 将屏幕坐标转换为 UGUI 坐标
|
||||
Vector3 uiPoint = ScreenPointToUIPoint(rt, screenPosition, uiCamera);
|
||||
|
||||
return uiPoint;
|
||||
}
|
||||
|
||||
// RectTransformUtility.WorldToScreenPoint
|
||||
// RectTransformUtility.ScreenPointToWorldPointInRectangle
|
||||
// RectTransformUtility.ScreenPointToLocalPointInRectangle
|
||||
// 上面三个坐标转换的方法使用 Camera 的地方
|
||||
// 当 Canvas renderMode 为 RenderMode.ScreenSpaceCamera、RenderMode.WorldSpace 时 传递参数 canvas.worldCamera
|
||||
// 当 Canvas renderMode 为 RenderMode.ScreenSpaceOverlay 时 传递参数 null
|
||||
|
||||
// UI 坐标转换为屏幕坐标
|
||||
public static Vector2 UIPointToScreenPoint(Vector3 worldPoint, Camera uiCamera)
|
||||
{
|
||||
Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(uiCamera, worldPoint);
|
||||
return screenPoint;
|
||||
}
|
||||
|
||||
// 屏幕坐标转换为 UGUI 坐标
|
||||
public static Vector3 ScreenPointToUIPoint(RectTransform rt, Vector2 screenPoint, Camera uiCamera)
|
||||
{
|
||||
//UI屏幕坐标转换为世界坐标
|
||||
// 当 Canvas renderMode 为 RenderMode.ScreenSpaceCamera、RenderMode.WorldSpace 时 uiCamera 不能为空
|
||||
// 当 Canvas renderMode 为 RenderMode.ScreenSpaceOverlay 时 uiCamera 可以为空
|
||||
RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, screenPoint, uiCamera, out Vector3 globalMousePos);
|
||||
// 转换后的 globalMousePos 使用下面方法赋值
|
||||
// target 为需要使用的 UI RectTransform
|
||||
// rt 可以是 target.GetComponent<RectTransform>(), 也可以是 target.parent.GetComponent<RectTransform>()
|
||||
// target.transform.position = globalMousePos;
|
||||
return globalMousePos;
|
||||
}
|
||||
|
||||
// 屏幕坐标转换为 UGUI RectTransform 的 anchoredPosition
|
||||
public static Vector2 ScreenPointToUILocalPoint(RectTransform parentRT, Vector2 screenPoint, Camera uiCamera)
|
||||
{
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRT, screenPoint, uiCamera, out Vector2 localPos);
|
||||
// 转换后的 localPos 使用下面方法赋值
|
||||
// target 为需要使用的 UI RectTransform
|
||||
// parentRT 是 target.parent.GetComponent<RectTransform>()
|
||||
// 最后赋值 target.anchoredPosition = localPos;
|
||||
return localPos;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user