using UnityEngine;
using System.Collections;
using System;
namespace Helper
{
public class ScreenBounding
{
Vector3[] boundingBoxes;
Camera mainCamera;
//UV坐标转对应世界坐标
Vector3[] viewportPoint =
{
new Vector3(0.5f,1),//top middle
new Vector3(0.5f,0),//bottom middle
new Vector3(1,0.5f),//middle right
new Vector3(0,0.5F),//middle left
};
public Vector3 topPos { get { return boundingBoxes[0]; } }
public Vector3 bottomPos { get { return boundingBoxes[1]; } }
public Vector3 rightPos { get { return boundingBoxes[2]; } }
public Vector3 leftPos { get { return boundingBoxes[3]; } }
public float height { get { return boundingBoxes[0].y - boundingBoxes[1].y; } }
public ScreenBounding()
{
mainCamera = Camera.main;
CreateBoundingBox();
}
void CreateBoundingBox()
{
boundingBoxes = new Vector3[viewportPoint.Length];
for (int i = 0; i < boundingBoxes.Length; i++)
{
//转换坐标
Vector3 origin = mainCamera.ViewportToWorldPoint(viewportPoint[i]);
origin.z = 0;
boundingBoxes[i] = origin;
}
#if UNITY_EDITOR
//编辑器下可视化
BoundBoxs();
#endif
}
void BoundBoxs()
{
GameObject allBox = new GameObject("BoundingBoxs");
for (int i = 0; i < boundingBoxes.Length; i++)
{
GameObject edgeBox = new GameObject();
edgeBox.transform.SetParent(allBox.transform);
edgeBox.transform.position = boundingBoxes[i];
}
}
}
}
【Unity】屏幕边界的世界坐标及可视化
猜你喜欢
转载自blog.csdn.net/qq_39108767/article/details/91574631
今日推荐
周排行