2014-12-01 61 views
0

在游戏中,玩家可以砍树。然后,我实例化一棵倒下的树在它的位置。地形刷新滞后统一c# - 如何有效刷新地形?

我从地形列表中移除该树并刷新地形就像这样:

 var treeInstancesToRemove = new List<TreeInstance>(terrain.treeInstances); 
     treeInstancesToRemove.RemoveAt(closestTreeIndex); 
     terrain.treeInstances = treeInstancesToRemove.ToArray(); 

     // I refresh the terrain so the collider gets removed... 
     float[,] heights = terrain.GetHeights(0, 0, 0, 0); 
     terrain.SetHeights(0, 0, heights); 

地形是非常大的。这意味着只要一棵树被砍游戏冻结几秒钟,然后继续(因为它刷新)。有没有更快或更有效的方式我可以看看?每砍掉一棵树后冻结都不太理想?

感谢您的提前!

回答

0

我可以建议的最好的事情是让世界分裂成可以单独更新的块。要么是这样,要么将碰撞器更新为与主碰撞器不同的线程。

+0

我喜欢chunks的想法,我只是没有想法如何在Unity中做到这一点!我会做一些研究... – 2014-12-01 19:09:46

0
float hmWidth = grav.currentTerrain.terrainData.heightmapWidth; 
    float hmHeight = grav.currentTerrain.terrainData.heightmapHeight; 
    // get the normalized position of this game object relative to the terrain 
    Vector3 tempCoord = (transform.position - grav.currentTerrain.gameObject.transform.position); 
    Vector3 coord; 
    coord.x = tempCoord.x/grav.currentTerrain.terrainData.size.x; 
    coord.y = tempCoord.y/grav.currentTerrain.terrainData.size.y; 
    coord.z = tempCoord.z/grav.currentTerrain.terrainData.size.z; 
    // get the position of the terrain heightmap where this game object is 
    int posXInTerrain = (int)(coord.x * hmWidth); 
    int posYInTerrain = (int)(coord.z * hmHeight); 
    // we set an offset so that all the raising terrain is under this game object 
    //int offset = size/2; 

    // get the heights of the terrain under this game object 
    float[,] heights = grav.currentTerrain.terrainData.GetHeights(posXInTerrain, posYInTerrain, 1, 1); 
    grav.currentTerrain.terrainData.SetHeights(posXInTerrain, posYInTerrain, heights); //THIS CHANGES TERRAIN FOR GOOD 
+0

这个答案可以通过添加一些非代码内容来改善。 – Thomas 2015-10-07 19:37:38