unity的recttransformutility函式裡面提供了常用的座標系之間的轉換關係。
因為recttransformutility裡面提供的函式是環形結構,所以螢幕座標,ugui座標,世界座標三者之間可以相互轉換。
由於函式都需要用到場景相機和ugui相機,所以定義兩個相機變數。
/// /// 場景相機
///
public static camera scencecamera;
/// /// ugui相機
///
public static camera uguicamera;
下面開始看座標系之間的轉換介面。
worldtoscreenpoint
/// /// 世界座標轉螢幕座標
///
///
///
public static vector2 worldtoscreenpoint(vector3 worldpoint)
/// /// 世界座標轉螢幕座標
///
///
///
///
public static vector2 worldtoscreenpoint(camera cam, vector3 worldpoint)
screenpointtoworldpointinrectangle
/// /// 螢幕座標轉世界座標
///
///
///
///
///
public static bool screenpointtoworldpointinrectangle(recttransform rect, vector2 screenpoint, out vector3 worldpoint)
/// /// 螢幕座標轉世界座標
///
///
///
///
///
///
public static bool screenpointtoworldpointinrectangle(recttransform rect, vector2 screenpoint, camera cam, out vector3 worldpoint)
screenpointtolocalpointinrectangle
/// /// 螢幕座標轉某個recttransform下的localposition座標
///
///
///
///
///
public static bool screenpointtolocalpointinrectangle(recttransform rect, vector2 screenpoint, out vector2 localpoint)
/// /// 螢幕座標轉某個recttransform下的localposition座標
///
///
///
///
///
///
public static bool screenpointtolocalpointinrectangle(recttransform rect, vector2 screenpoint, camera cam, out vector2 localpoint)
transform.position 就是ugui下面的世界座標,不用轉換
ugui座標->世界座標->螢幕座標
世界座標->螢幕座標-> ugui座標
using unityengine;
public class transformutility
/// /// 世界座標轉螢幕座標
///
///
///
///
public static vector2 worldtoscreenpoint(camera cam, vector3 worldpoint)
#endregion
#region 螢幕座標轉世界座標
/// /// 螢幕座標轉世界座標
///
///
///
///
///
public static bool screenpointtoworldpointinrectangle(recttransform rect, vector2 screenpoint, out vector3 worldpoint)
/// /// 螢幕座標轉世界座標
///
///
///
///
///
///
public static bool screenpointtoworldpointinrectangle(recttransform rect, vector2 screenpoint, camera cam, out vector3 worldpoint)
#endregion
#region 螢幕座標轉ugui座標
/// /// 螢幕座標轉某個recttransform下的localposition座標
///
///
///
///
///
public static bool screenpointtolocalpointinrectangle(recttransform rect, vector2 screenpoint, out vector2 localpoint)
/// /// 螢幕座標轉某個recttransform下的localposition座標
///
///
///
///
///
///
public static bool screenpointtolocalpointinrectangle(recttransform rect, vector2 screenpoint, camera cam, out vector2 localpoint)
#endregion
}
Unity Ugui射線座標轉換總結
世界空間中的點座標轉換到螢幕座標 screenpos recttransformutility.worldtoscreenpoint cam,worldpos.transform.position ugui物體的座標轉換到螢幕座標 screenpos recttransformutility.wor...
Unity UGUI場景座標轉換成UI座標
所用到的api camerahelper.maincamera.worldtoscreenpoint vector3 position recttransformutility.screenpointtolocalpointinrectangle recttransform rect,vector2...
螢幕座標向OpenGL座標轉換
很多人用opengl繪圖會遇到乙個問題即螢幕座標向opengl座標轉換,在網上流傳著如下類似的 glint viewport 4 gldouble modelview 16 gldouble projection 16 glfloat winx,winy,winz gldouble posx,pos...