1. 程式人生 > >判斷物體是否在攝像機視野中

判斷物體是否在攝像機視野中

void Start(){
    Rect screenRect  = new Rect(0, 0, Screen.width, Screen.Height); 
}

void Update{
    Vector3 screenPos = targetCamera.WorldToScreenPoint(objTrans.position);
    if(!screenRect.Contains(screenPos)){
           //do what you want
           Debug.log("Obj has gone out of screen...");
    }
}

方法一


public var isRendering:boolean=false;
private var lastTime:float=0;
private var curtTime:float=0;

function Update()
{
     isRendering=curtTime!=lastTime?true:false;
          lastTime=curtTime;
}

function OnWillRenderObject()
{
     curtTime=Time.time;
}

方法二