【问题标题】:Terrain refresh lag unity c# - How effectively refresh the terrain?Terrain refresh lag unity c# - 如何有效地刷新地形?
【发布时间】:2014-12-01 18:38:49
【问题描述】:

在游戏中,玩家可以砍树。然后我在它的位置实例化一棵倒下的树。

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

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

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

非常感谢!

【问题讨论】:

    标签: c# unity3d refresh terrain


    【解决方案1】:

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

    【讨论】:

    • 我喜欢块的想法,我只是不知道如何在 Unity 中做到这一点!我会做一些研究...
    【解决方案2】:
        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
    

    【讨论】:

    • 这个答案可以通过添加一些非代码内容来改进。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多