代码如下
注意要搞清楚当前坐标属于哪种坐标,然后,相对指定的Camera转化
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestCamera : MonoBehaviour
{
public Camera UiCamera;
public Camera WorldCamera;
public Vector3 UIPostion;
public Vector3 WorldPosion;
#region 方法1:统一转化为屏幕上的点,这个操作最快捷
private Vector3 GetScreenPoint(Camera camera ,Vector3 v3)
{
return camera.WorldToScreenPoint(v3);
}
private void Debug1()
{
Debug.Log(GetScreenPoint(UiCamera,UIPostion)+"<>"+GetScreenPoint(WorldCamera,WorldPosion));
}
#endregion
#region 方法2:根据ViewPot转化,也可以达到,虽然繁琐但是原理可以理解下
public Vector3 GetUguiViewPort(Camera camera,Vector3 v3)
{
var point = camera.WorldToViewportPoint(v3);
return point;
}
public Vector3 GetViewPortToPos(Vector2 v2)
{
return new Vector3(v2.x * Screen.width, v2.y * Screen.height);
}
public void Debug2()
{
var v1 = GetUguiViewPort(UiCamera, UIPostion);
var v2 = GetUguiViewPort(WorldCamera, WorldPosion);
Debug.Log(GetViewPortToPos(v1)+"<>"+GetViewPortToPos(v2));
}
#endregion
void Start()
{
}
void Update()
{
}
}
|