首页 文章

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

提问于
浏览
0

在游戏中,玩家可以砍伐树木 . 然后我在它的位置实例化一棵落树 .

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

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

相关问题