Unity中屏幕坐标与世界坐标之间的转换

世界坐标系转换为屏幕坐标
屏幕坐标转世界坐标
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
print(Camera.main.WorldToScreenPoint(this.transform.position));
}

// Update is called once per frame
void Update()
{
    if(Input.GetMouseButtonDown(0))
    {
        GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);

        Vector3 v = Input.mousePosition;
        v.z = 20;

        obj.transform.position = Camera.main.ScreenToWorldPoint(v);
    }
}

}

猜你喜欢

转载自blog.csdn.net/charlsdm/article/details/126467352