地形刷新滞后统一c# - 如何有效刷新地形?

时间:2014-12-01 18:38:50

标签: c# unity3d refresh terrain

在游戏中,玩家可以砍伐树木。然后我在其中实例化了一棵落树。

我从地形列表中删除树并刷新地形,如下所示:

        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);

地形非常大......这意味着每当砍伐一棵树时,游戏会冻结几秒钟然后恢复(因为它会刷新)。有没有更快或更有效的方式我可以看一看?在你砍伐的每棵树之后冻结是不是很理想?

感谢很多事情!

2 个答案:

答案 0 :(得分:0)

我能建议的最好的事情就是让世界分成几块,你可以单独更新。要么是这样,要么在与主要线程分开的线程中更新对撞机。

答案 1 :(得分: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