Unity中的坐标与绘制准心

来源:互联网 发布:百度地图坐标纠偏算法 编辑:程序博客网 时间:2024/04/27 22:23

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 initializationvoid Start () {}// Update is called once per framevoid Update () {}    void OnGUI()    {        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);    }}


0 0