准备灰度图 IGray.png及草地贴图 IGrass.jpg ,放入Assets下StreamingAssets文件夹中。
![](https://i-blog.csdnimg.cn/blog_migrate/73caa13ffd3619a55f026ae6d2859da5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/0ab62ab7654a9f06e6b03f3080d4be21.jpeg)
创建空材质,用作参数传入脚本。
![](https://i-blog.csdnimg.cn/blog_migrate/e14219e9d0a68e8dea37730a2694f0dd.png)
脚本如下,挂载并传入材质球即可根据灰度图生成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<GameObject>();
}
void Update()
{
if (textureGray != null &&