unity 中的3d物体都是以世界坐标显示在场景中的,通过transform.position获取。
unity 屏幕坐标(0.0)点在左下角以像素为单位,屏幕宽高分别是Screen.width, Screen.height。
ngui坐标(0.0)点在屏幕中心,通过两种方式缩放Pixel Perfect 和Fixed Size。
屏幕坐标和ngui坐标有两种转换方式
1.当Scaling Style 是 PixelPerfect 时仅计算像素的偏移即可
t.localPosition = (Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2, 0))
2.当Scaling Style 是 Fixed Size时还需计算坐标系的缩放值
float rate = System.Convert.ToSingle(uir.manualHeight) / Screen.height;
t.localPosition = (Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2, 0)) * rate;
当然unity也提供了两个转换方法
世界坐标和屏幕坐标转换通过screenpos = Camera.main.WorldToScreenPoint(worldpos)
屏幕坐标和ngui坐标转换通过trans.position = UICamera.curre