仅当它们靠近控制器/摄像机时才渲染GameObject(Render GameObject only when they are near to the controller/camera)

编程入门 行业动态 更新时间:2024-10-25 20:27:03
仅当它们靠近控制器/摄像机时才渲染GameObject(Render GameObject only when they are near to the controller/camera)

我和我的朋友正在尝试在Android上开发游戏,我们正在尽力减少延迟。

是否可以仅在靠近控制器时渲染对象?

我们有这个岛,所以现场有很多树。

我们仍然是Unity的新手。 如果有可能,怎么样? 谢谢。

Me and my friends are trying to develop a game on the android and we are trying our best to reduce the lag.

Is it possible to render objects only when they are near to the controller?

We have this island so it has a lot of trees in the scene.

We are still pretty new to Unity. If it is possible, how? Thanks.

最满意答案

你可以用两种方式做到这一点。

1.Unity的内置细节层次 (LOD)是你应该去的第一个解决方案。为那些GameObject创建多个3D模型。 高详细对象,中间细节,低细节和真正真正行详细的多边形。

当你远离那个物体时,Unity只会显示低细节的3D模型。 一旦您靠近物体,Unity就会自动将3D模型与高详细的3D模型交换。 这改善了游戏的记忆和速度。 要了解如何设置Google“细节水平统一”并观看视频或关注Unity 教程 。 它需要视觉才能理解

2.另一种解决方案是使用Vector3.Distance检查摄像机和要隐藏的Vector3.Distance之间的距离,然后通过使Renderer.enabled为false来禁用Renderer Renderer.enabled来隐藏它。

下面是此方法的完整工作代码。 只需将下面的代码附加到远离相机时要隐藏的每个树或网格中。 您可以调整distanceToAppear变量以满足您的需要。

public class MeshMonitor : MonoBehaviour { Transform mainCamTransform; //Stores the FPS camera transform private bool visible = true; public float distanceToAppear = 8; Renderer objRenderer; void Start() { mainCamTransform = Camera.main.transform;//Get camera transform reference objRenderer = gameObject.GetComponent<Renderer>(); //Get render reference } // Update is called once per frame void Update() { disappearChecker(); } void disappearChecker() { float distance = Vector3.Distance(mainCamTransform.position, transform.position); //We have reached the distance to Enable Object if (distance < distanceToAppear) { if (!visible) { objRenderer.enabled = true;// Show Object visible = true; Debug.Log("Visible"); } } else { if (visible) { objRenderer.enabled = false;// Hide Object visible = false; Debug.Log("InVisible"); } } } }

You can do rthis in two ways.

1. Unity's built in Level of Detail (LOD) is the first solution you should go for.Create multiple 3D models for those GameObject. High detailed object, mid detail, low detailed and really really row detailed polygon.

When you are far from that object, Unity would only show the low detailed 3D model. As soon as you move closer to the object, Unity would automatically swap the 3D model with a high detailed 3D Model. This improves the memory and the speed of the game. To see how to set this up Google "Level of Detail unity" and watch the videos or follow Unity tutorial. It requires visual to understand

2. Another solution is to check the distance between the camera and the GameObject you want to hide with Vector3.Distance, then hide it by disable the Renderer by makingRenderer.enabled to false.

Below is a complete working code for this method. Just attach the code below to each of the trees or mesh you want to hide when far away from the camera. You can adjust the distanceToAppear variable to fit your needs.

public class MeshMonitor : MonoBehaviour { Transform mainCamTransform; // Stores the FPS camera transform private bool visible = true; public float distanceToAppear = 8; Renderer objRenderer; private void Start() { mainCamTransform = Camera.main.transform;//Get camera transform reference objRenderer = gameObject.GetComponent<Renderer>(); //Get render reference } private void Update() { disappearChecker(); } private void disappearChecker() { float distance = Vector3.Distance(mainCamTransform.position, transform.position); // We have reached the distance to Enable Object if (distance < distanceToAppear) { if (!visible) { objRenderer.enabled = true; // Show Object visible = true; Debug.Log("Visible"); } } else if (visible) { objRenderer.enabled = false; // Hide Object visible = false; Debug.Log("InVisible"); } } }

更多推荐

本文发布于:2023-08-04 03:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408811.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时才   控制器   摄像机   GameObject   controller

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!