unity 檢測物體是否在相機視野範圍內

NO IMAGE

指令碼掛在攝像機要顯示的物件上

前提:該物件有 render 元件

public class visibleTT : MonoBehaviour
{
public bool isRendering = false;
public float lastTime = 0;
public float curTime = 0;
void Update()
{
if (lastTime != curTime)
{ 
isRendering = true;
Debug.Log("In View");
}
else
{ isRendering = false;
Debug.Log("Not In View");
}
lastTime = curTime;
}
void OnWillRenderObject()
{
curTime = Time.time;
}
}