unity 检测物体是否在相机视野范围内

脚本挂在摄像机要显示的对象上

前提:该对象有 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;

    }
    
}


你可能感兴趣的:(unity)