如何在代码中更改地形纹理

Con*_*ias 2 c# textures renderer terrain unity-game-engine

我想通过代码更改地形纹理的偏移量(2)。我添加了道路图像作为地形上的纹理。我在网上找到了相关代码,但我无法弄清楚渲染器在这种情况下的作用。

除了代码之外,我只想知道通过代码更改纹理应该采取的第一步。(基本设置)。还有请提一下渲染器的作用。

在此输入图像描述

Lud*_*ltz 5

在 Unity Terrains 中,纹理由类处理SplatPrototype查看文档

Splat 原型只是 TerrainData 使用的纹理。

因此,如果您想更改地形纹理,您必须创建一个新的纹理SplatPrototype并将其设置为 的splatPrototype变量TerrainData

您可以在此处设置您选择的metallicnormalMapsmoothnesstexturetileSize的值。tileOffset

您可以使用以下方法:

private void SetTerrainSplatMap(Terrain terrain, Texture2D[] textures)
{
    var terrainData = terrain.terrainData;

    // The Splat map (Textures)
    SplatPrototype[] splatPrototype = new SplatPrototype[terrainData.splatPrototypes.Length];
    for (int i = 0; i < terrainData.splatPrototypes.Length; i++)
    {
        splatPrototype[i] = new SplatPrototype();
        splatPrototype[i].texture = textures[i];    //Sets the texture
        splatPrototype[i].tileSize = new Vector2(terrainData.splatPrototypes[i].tileSize.x, terrainData.splatPrototypes[i].tileSize.y);    //Sets the size of the texture
        splatPrototype[i].tileOffset = new Vector2(terrainData.splatPrototypes[i].tileOffset.x, terrainData.splatPrototypes[i].tileOffset.y);    //Sets the size of the texture
    }
    terrainData.splatPrototypes = splatPrototype;
}
Run Code Online (Sandbox Code Playgroud)