1.可以用柏林算法生成上下起伏的2d地表(自己改下就行)
https://blog.csdn.net/u010019717/article/details/72673225
2.看起来不错
https://indienova.com/indie-game-development/procedural-content-generation-tile-based-random-cave-map/
作者改进 https://blog.csdn.net/lly20000/article/details/76764629
3.unity提供的
https://blog.csdn.net/l773575310/article/details/72803191
5.随机多边形地图生成
https://blog.csdn.net/liqiang981/article/details/76522508
4.2d游戏水流动算法
https://indienova.com/indie-game-development/grid-based-water-simulation/
【基于2自己写了个】
(用之前文章代码改的 https://www.cnblogs.com/sanyejun/p/9298592.html)
(代码没整理比较乱,复制粘贴就能用)
鼠标左键销毁格子
右键生成格子
Q键平滑洞穴
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Tilemaps; public class CreateTileMap : MonoBehaviour { public Tilemap tilemap;//引用的Tilemap public Tile baseTile;//使用的最基本的Tile,我这里是白色块,然后根据数据设置不同颜色生成不同Tile Tile[] arrTiles;//生成的Tile数组 int[,] mapArray; void Awake() { //ins = this; } void Start() { StartCoroutine(InitData()); } void Update() { //销毁墙体 if (Input.GetMouseButtonDown(0)) { Vector3 mousePosition = Input.mousePosition; Vector3 wordPosition = Camera.main.ScreenToWorldPoint(mousePosition); Vector3Int cellPosition = tilemap.WorldToCell(wordPosition); //tilemap.SetTile(cellPosition, gameUI.GetSelectColor().colorData.mTile); TileBase tb = tilemap.GetTile(cellPosition); if (tb == null) { return; } //tb.hideFlags = HideFlags.None; Debug.Log("鼠标坐标" + mousePosition + "世界" + wordPosition + "cell" + cellPosition + "tb" + tb.name); //某个地方设置为空,就是把那个地方小格子销毁了 tilemap.SetTile(cellPosition, null); //tilemap.RefreshAllTiles(); } //空白地方创造墙体 if (Input.GetMouseButtonDown(1)) { Vector3 mousePosition = Input.mousePosition; Vector3 wordPosition = Camera.main.ScreenToWorldPoint(mousePosition); Vector3Int cellPosition = tilemap.WorldToCell(wordPosition); //tilemap.SetTile(cellPosition, gameUI.GetSelectColor().colorData.mTile); TileBase tb = tilemap.GetTile(cellPosition); if (tb != null) { return; } //tb.hideFlags = HideFlags.None; //Debug.Log("鼠标坐标" + mousePosition + "世界" + wordPosition + "cell" + cellPosition + "tb" + tb.name); //格子填充 tilemap.SetTile(cellPosition, baseTile); //tilemap.RefreshAllTiles(); } //再次平滑洞穴 if (Input.GetKeyDown(KeyCode.Q)) { StartCoroutine(PingHua(levelW, levelH)); } } //大地图宽高 int levelW = 100; int levelH = 100; /// <summary> /// 地图生成 /// </summary> /// <returns></returns> IEnumerator InitData() { mapArray = new int[levelW, levelH]; int colorCount = 6; arrTiles = new Tile[colorCount]; for (int i = 0; i < colorCount; i++) { //想做生命墙,需要自己做个数据层,对应索引id就行 arrTiles[i] = ScriptableObject.CreateInstance<Tile>();//创建Tile,注意,要使用这种方式 arrTiles[i].sprite = baseTile.sprite; arrTiles[i].color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1); } for (int i = 0; i < levelH; i++) {//这里就是设置每个Tile的信息了 for (int j = 0; j < levelW; j++) { //50%概率 if (Random.value < 0.5f) { tilemap.SetTile(new Vector3Int(j, i, 0), arrTiles[Random.Range(0, arrTiles.Length)]); mapArray[i, j] = 1; } else { tilemap.SetTile(new Vector3Int(j, i, 0), null); mapArray[i, j] = 0; } //tilemap.SetTile(new Vector3Int(j, i, 0), arrTiles[Random.Range(0, arrTiles.Length)]); } yield return null; } StartCoroutine(PingHua(levelH, levelW)); while (true) { yield return new WaitForSeconds(2); // int colorIdx = Random.Range(0, colorCount);//前面这个是随机将某个块的颜色改变,然后让Tilemap更新,主要用来更新Tile的变化 // arrTiles[colorIdx].color = new Color(Random.Range(0f, 1f), Random.Range(0f,1f), Random.Range(0f, 1f), 1); // tilemap.RefreshAllTiles(); Color c = tilemap.color;//这里是改变Tilemap的颜色,尝试是否可以整体变色 c.a -= Time.deltaTime; tilemap.color = c; } } /// <summary> /// 生成平滑洞穴 /// </summary> /// <param name="levelH"></param> /// <param name="levelW"></param> /// <returns></returns> public IEnumerator PingHua(int levelH, int levelW) { for (int i = 0; i < levelH; i++) {//这里就是设置每个Tile的信息了 for (int j = 0; j < levelW; j++) { if (i !=0 && j != 0 && i != levelH -1 && j != levelW -1) { int wuti = mapArray[i, j]; int c = GetCount(i, j); if (wuti == 1) { mapArray[i, j] = (c >= 4) ? 1 : 0; } else if (wuti == 0) { mapArray[i, j] = (c >= 5) ? 1 : 0; } //draw if (mapArray[i, j] == 1) { tilemap.SetTile(new Vector3Int(j, i, 0), baseTile); } else { tilemap.SetTile(new Vector3Int(j, i, 0), null); } } } yield return null; } } public int GetCount(int i ,int j) { int x1 = mapArray[i - 1, j - 1]; int x2 = mapArray[i - 1, j]; int x3 = mapArray[i, j - 1]; int x4 = mapArray[i + 1, j +1]; int x5 = mapArray[i + 1, j]; int x6 = mapArray[i, j + 1]; int x7 = mapArray[i - 1, j + 1]; int x8 = mapArray[i + 1, j - 1]; return x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8; } }
---------------------------2018年12月05日13:27:12--------------------------
随机地图生成器,非常强大
WaveFunctionCollapse
https://github.com/mxgmn/WaveFunctionCollapse
https://cowlevel.net/article/1964746
-------------------------------2018年12月05日13:32:28------------------------------
体素地图编辑器:
Voxel Terrain ,自带网格合并
https://blog.csdn.net/qq_37125419/article/details/78339771
=========================2d随机地图生成========================
https://github.com/UnityTechnologies/ProceduralPatterns2D