Unity中的坐标与绘制准心

xiaoxiao2021-02-27  369

unity3d中的屏幕坐标系 是以 屏幕  左下角为(0,0)点 右上角为(Screen.Width,Screen.Height)

鼠标位置坐标与屏幕坐标系一致

视口坐标是以摄像机为准  以屏幕的左下角为(0,0)点 右上角为(1,1)点

绘制GUI界面时使用的坐标是以  屏幕  的左上角为(0,0)点 右下角为(Screen.width,Screen,Height)

经常会用到 某个物体的世界坐标到屏幕坐标的转化然后再屏幕上绘制出这个物体的代表性图片

是这样做的

1、Vector3 ScreenPos=Camera.WorldToScreenPoint(trans.Position);

2、GUIPos=new Vector3(ScreenPos.x,Screen.height-ScreenPos.y,0);

然后按照这个坐标绘制图片就可以了

下面是绘制准心的代码,这里用到了坐标的相关知识

 

using UnityEngine; using System.Collections; public class AimPoint : MonoBehaviour { public Texture texture; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnGUI() { //相当于除以2。(x >> 1) 和 (x / 2) 的结果是一样的 Rect rect = new Rect(Input.mousePosition.x - (texture.width >> 1), Screen.height - Input.mousePosition.y - (texture.height >> 1), texture.width, texture.height); GUI.DrawTexture(rect, texture); } }

 

 

 

 

 

转载请注明原文地址: https://www.6miu.com/read-1605.html

最新回复(0)