纳金网
标题:
判断物体摄像机是否可见
[打印本页]
作者:
烟雨
时间:
2015-12-19 06:58
标题:
判断物体摄像机是否可见
/// <summary>
/// 判断一个物体是否可见
/// </summary>
/// <returns><c>true</c> if this instance is visible the specified go; otherwise, <c>false</c>.</returns>
/// <param name="go">如果不可见返回0,可见=1.</param>
int IsVisible(GameObject go)
{
int ret = -1;
if(go!=null && Camera.main!=null)
{
Vector3 pos = Camera.main.WorldToViewportPoint(go.transform.position);
bool isVisible = ( Camera.main.orthographic || pos.z > 0f)
&& ( (pos.x > 0f && pos.x < 1f && pos.y > 0f && pos.y < 1f));
ret = isVisible==true ?1:0;
return ret;
}
return ret;
}
//这个是通过render来判断,但是感觉没上面那个好用
bool ReaderIsVisible(GameObject gameObjcet)
{
bool isVisbel = false;
MeshRenderer[] renders = gameObject.GetComponentsInChildren<MeshRenderer> ();
foreach(MeshRenderer render in renders)
{
if( render.isVisible ==true)
{
isVisbel =true;
}
}
return isVisbel;
}
复制代码
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5