我和我的朋友正在尝试在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"); } } }更多推荐
发布评论