准备灰度图 IGray.png及草地贴图 IGrass.jpg ,放入Assets下StreamingAssets文件夹中。


创建空材质,用作参数传入脚本。

脚本如下,挂载并传入材质球即可根据灰度图生成mesh。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//根据灰度图创建mesh地形
public class MeshTerrainCreate : MonoBehaviour
{
private Texture textureGray;//灰度图
private Texture textureGrass;//草地贴图
private int tGrayWidth = 0, tGrayHeight = 0;//灰度图的宽和高
private bool bCreate = false;//是否完成创建
private List<GameObject> meshList;//mesh对象集合
private Texture2D texture2dGray;
public float zScale=100;//高度参数
[Tooltip("传入mesh使用的材质")]
public Material meshMaterial;
void Start()
{
StartCoroutine(loadImage("IGray.png", (t) => textureGray = t));
StartCoroutine(loadImage("IGrass.jpg", (t) => textureGrass = t));
meshList = new List<