// Builds the mesh for the chunk public void BuildMesh() { if (!dirtyMesh) { return; } dirtyMesh = false; Mesh mesh = MeshBuilder.BuildMesh(this, MESH_TYPE); // Mesh can be null if it contained no verticies if (mesh == null) { ReleaseChunkGameObject(); return; } // Get a new game object from the pool if nessecary if (chunkGameObject == null) { chunkGameObject = chunkGameObjectPool.Get(); chunkGameObject.Setup(chunkPos, realPos, voxelEngineManager.gameObject.transform); chunkGameObject.meshRenderer.material = voxelEngineManager.meshMaterial; } chunkGameObject.meshFilter.sharedMesh = mesh; #pragma warning disable 0162 if (GENERATE_COLLIDERS) { chunkGameObject.meshCollider.sharedMesh = mesh; } #pragma warning restore 0162 }
// Try release the Unity game object back into the game object pool private void ReleaseChunkGameObject() { if (chunkGameObject == null) { return; } if (chunkGameObjectPool.Add(chunkGameObject)) { chunkGameObject.Clean(); } else { chunkGameObject.Destroy(); } chunkGameObject = null; }