unity-game-engine - 检查对象是否在屏幕外

标签 unity-game-engine

我需要知道相机何时停止看到某个对象以将其返回到池中。 我尝试了一些方法,但没有成功,我想指出的是,相机仅在 X 轴上跟随玩家,它是 3D,但它是一款横向卷轴 View 游戏。 顺便说一句,我没有使用 MonoBehaviour,所以避免建议 OnBecameInvisible 和亲戚。

这是我的两次失败的尝试。

internal static bool IsVisible(this Renderer renderer)
{
    Plane[] planes = GeometryUtility.CalculateFrustumPlanes(Camera.main);
    return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
}

internal static void IsVisible(this GameObject go)
{
    var screenPoint = Camera.main.WorldToScreenPoint(go.transform.position);
    screenPoint.x = Mathf.Clamp01(screenPoint.x);

    if (screenPoint.x > .05f)
    {
        Debug.Log("Visible");
    }
    else Debug.Log("Invisible");
}

最佳答案

简单:

if(!GetComponent<Renderer>().isVisible){
//Whatever you want to do here
}

如果您遇到问题,请参阅:

https://docs.unity3d.com/ScriptReference/Renderer-isVisible.html

https://forum.unity.com/threads/how-do-i-use-renderer-isvisible.377388/

关于unity-game-engine - 检查对象是否在屏幕外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54505849/

相关文章:

c# - 如何在Unity3d中截取透明背景的屏幕截图?

unity-game-engine - Unity 中可以存在这个着色器吗?

.net - 我可以将 Azure 移动服务与 Unity 3d 结合使用吗?

c# - Unity : Find out, 如果用户点击了广告

android - 在游戏引擎之上构建音乐制作应用程序是否有意义?

unity-game-engine - 统一移动到另一个场景,当前场景暂停

c# - RayCastHit 找不到错误消息 *Unity*

unity-game-engine - Unity : Having the player face the mouse direction. 为什么这段代码可以工作?

android - 在插件中使用 AAssetManager_fromJava 不直接从 Java VM 调用(从 Unity 调用)

android - ARCore应用程序在构建后无法运行