Version: 2021.3
public bool isVisible ;

描述

该渲染器是否在任何摄像机中可见?(只读)

注意,当需要在场景中渲染某个对象时,则认为该对象可见。例如, 有的对象实际上可能不会被任何摄像机看到,但由于阴影原因仍需要渲染。 在 Editor 中运行时,Scene 视图摄像机也会导致该值为 true。

另请参阅:OnBecameVisibleOnBecameInvisible

//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"); } }