设置alphamap的核心方法是TerrainData的SetAlphamaps方法,关键参数是alpha三维数组。它的前两维表示了splatmap的大小,最后一维表示layer数目,也就是所使用的splatmap的通道数

using UnityEngine;

public class Example : MonoBehaviour { public Terrain t; // Blend the two terrain textures according to the steepness of // the slope at each point. void Start() { float[,,] map = new float[t.terrainData.alphamapWidth, t.terrainData.alphamapHeight, 2];

// For each point on the alphamap... for (int y = 0; y < t.terrainData.alphamapHeight; y++) { for (int x = 0; x < t.terrainData.alphamapWidth; x++) { map[x, y, 0] = 0; map[x, y, 1] = 1; } } t.terrainData.SetAlphamaps(0, 0, map); } }