慕田峪9158850
返回的数据由GetAlphamaps返回的数组是三维的 - 前两个维度表示地图上的 x 和 y 坐标,而第三个维度表示应用 alphamap 的 splatmap 纹理。或者简单来说就是“float[x, y, l]哪里”x= 宽度(以像素为单位)y= 高度(以像素为单位)l= 纹理层假设您想将其设置为该像素坐标处的某个纹理,您要做的就是将纹理层的权重设置为1将所有其他层权重设置为0假设您有 3 层,并且您希望第二层(=索引1)是完全加权的纹理:float[,,] splatmapData = terrainData.GetAlphamaps(mapX, mapZ, 15, 15);// Iterate over x-y coordinates within the arrayfor(var y = 0; i < 15; y++){ for(var x = 0; x < 15; x++) { // Set first layers weight to 0 splatmapData[x, y, 0] = 0; // Set second layer's weight to 1 splatmapData[x, y, 1] = 1; // Set third layer's weight to 0 splatmapData[x, y, 2] = 0; }}terrainData.SetAlphamaps(mapX, mapZ, splatmapData);然后我会enum为各层实现一个,比如public enum TerrainLayer{ Default = 0, Green, Red}因此您可以简单地将相应的图层索引作为参数传递 - 比传递值本身更安全int:private void ChangeTexture(Vector3 worldPos, TerrainLayer toLayer){ print ("changeTexture"); int mapX = (int)(((worldPos.x - terrainPos.x) / terrainData.size.x) * terrainData.alphamapWidth); int mapZ = (int)(((worldPos.z - terrainPos.z) / terrainData.size.z) * terrainData.alphamapHeight); float[,,] splatmapData = terrainData.GetAlphamaps(mapX, mapZ, 15, 15); for(var z = 0; z < 15; z++) { for(var x = 0; x < 15; x++) { // This ofcourse would be more efficient if you do this only once // e.g. in Awake since the enum won't change on runtime var values = (TerrainLAyer[])Enum.GetValues(typeof(TerrainLayer)); // Iterate through the enum and for(var l = 0; l < values.Length; l++) { // set all layers to 0 except the toLayer splatmapData[x, z, l] = values[l] == toLayer ? 1 : 0; } } } terrainData.SetAlphamaps (mapX, mapZ, splatmapData); terrain.Flush ();}现在你可以简单地称它为例如ChangeTexture(somePosition, TerrainLayer.Green);