Unity OnBecameVisible 与 OnBecameInvisible 用法

来源:互联网 发布:linux命令大全下载 编辑:程序博客网 时间:2024/05/21 18:47

OnBecameVisible() : 这个的意思是当物体在/进入摄像机会调用一次,类似触发器OnTriggerEnter();
OnBecameInvisible() : 这个的意思是当物体离开摄像机会调用一次,类似触发器OnTriggerExit();
如下是简单代码:添加给物体,不是摄像机

using UnityEngine;using System.Collections;public class Test : MonoBehaviour {    public AnimalType animType;    public void OnBecameVisible()    {        switch (animType)        {            case AnimalType.hema:            Debug.Log("hema");                break;            case AnimalType.rabbit:            Debug.Log("rabbit");                break;        }    }    public void OnBecameInvisible()    {        switch (animType)        {            case AnimalType.hema:             Debug.Log("hema");                break;            case AnimalType.rabbit:             Debug.Log("rabbit");                break;        }    }}public enum AnimalType{    rabbit,    hema }

。如果发现并没有触发的话,是因为你脚本所在的物体没有渲染组件,所以没有调用,这一点很重要。

0 0