unity3D之物体可见与不可见时的回调方法的用法

Renderer.isVisible

public bool isVisible;

Description

Is this renderer visible in any camera? (Read Only)

Note that the object is considered visible when it needs to be rendered in the Scene. For example, it might not actually be visible by any camera but still need to be rendered for shadows. When running in the editor, the Scene view cameras will also cause this value to be true.

//Attach this script to a GameObject with a Renderer component attached
//If the GameObject is visible to the camera, the message is output to the console

using UnityEngine;

public class IsVisible : MonoBehaviour
{
    Renderer m_Renderer;
    // Use this for initialization
    void Start()
    {
        m_Renderer = GetComponent<Renderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if (m_Renderer.isVisible)
        {
            Debug.Log("Object is visible");
        }
        else Debug.Log("Object is no longer visible");
    }
}

Renderer.OnBecameVisible()

Description

OnBecameVisible is called when the object became visible by any camera.

This message is sent to all scripts attached to the renderer. OnBecameVisible and OnBecameInvisible are useful to avoid computations that are only necessary when the object is visible.

public class Example : MonoBehaviour
{
    // Disable the behaviour when it becomes invisible...
    void OnBecameInvisible()
    {
        enabled = false;
    }

    // ...and enable it again when it becomes visible.
    void OnBecameVisible()
    {
        enabled = true;
    }
}

Note that object is considered visible when it needs to be rendered in the Scene. It might not be actually visible by any camera, but still need to be rendered for shadows for example. Also, when running in the editor, the Scene view cameras will also cause this function to be called.

Renderer.OnBecameInvisible()

Description

OnBecameInvisible is called when the object is no longer visible by any camera.

This message is sent to all scripts attached to the renderer. OnBecameVisible and OnBecameInvisible are useful to avoid computations that are only necessary when the object is visible.

using UnityEngine;

public class Example : MonoBehaviour
{
    // Disable the behaviour when it becomes invisible...
    void OnBecameInvisible()
    {
        enabled = false;
    }

    // ...and enable it again when it becomes visible.
    void OnBecameVisible()
    {
        enabled = true;
    }
}

Note that object is considered visible when it needs to be rendered in the Scene. It might not be actually visible by any camera, but still need to be rendered for shadows for example. Also, when running in the editor, the Scene view cameras will also cause this function to be called.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值