柏林噪声算法制作地图+小地图

 

 以上是效果展示

1,柏林噪声算法做地图代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GridMap : MonoBehaviour
{
public Image mapMin;//这里是方便获得小地图从而获得脚本去赋值
public Texture2D texture;//纹理
int w = 128;
int h = 128;

int wnum;//宽分块
int hnum;//高分块

public Vector3 v3;

// Start is called before the first frame update
void Start()
{
int w2 = 128 + 2;
int h2 = 128 + 2;

wnum = texture.width / 128;
hnum = texture.height / 128;

for (int i = 0; i < wnum; i++)
{
for (int j = 0; j < hnum; j++)
{
GameObject game = new GameObject();
game.transform.parent = transform;
//顶点助手
VertexHelper vh = new VertexHelper();
for (int x = 0; x < w2; x++)
{
for (int z = 0; z < h2; z++)
{
//柏林噪算法,传入两个坐标返回一个0到1的值(传入两个相同的值,坐标一定相同)
float y = Mathf.PerlinNoise((i * w + x) * 0.1f, (j * h + z) * 0.1f);
//计算UV坐标
float uvx = (float)(w * i + x) / (texture.width - 1);
float uvy = (float)(h * j + z) / (texture.height - 1);
vh.AddVert(new Vector3(w * i + x - texture.width / 2, y * 5, h * j + z - texture.height / 2), Color.white, new Vector2(uvx, uvy));
if (x < w2 - 1 && z < h2 - 1)
{
vh.AddTriangle(x * h2 + z, x * h2 + z + 1, (x + 1) * h2 + z + 1);
vh.AddTriangle(x * h2 + z, (x + 1) * h2 + z + 1, (x + 1) * h2 + z);
}
}
}

Mesh mesh = new Mesh();
vh.FillMesh(mesh);
game.AddComponent<MeshFilter>().mesh = mesh;
Material material = new Material(Shader.Find("Standard"));//材质
material.mainTexture = texture; //设置纹理
game.AddComponent<MeshRenderer>().material = material;//渲染纹理
game.AddComponent<MeshCollider>().sharedMesh = mesh;//设置网格碰器
}

}
//保存文理的修改
texture.Apply();
Material material1 = new Material(Shader.Find("UI/Default"));
material1.mainTexture = texture;
mapMin.material = material1;//此处是给小地图贴图

 

}

}

2,小地图代码(放大小地图或者缩小通过鼠标滑轮)

效果图

 

 //代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class MapsMove : MonoBehaviour, IPointerEnterHandler,IPointerExitHandler
{
public Image map;
public bool sure;//做判断鼠标是否在小地图上面
public void OnPointerEnter(PointerEventData eventData)
{
sure = true;
}

public void OnPointerExit(PointerEventData eventData)
{
sure = false;
}

 

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
if (sure)
{

if (Input.mouseScrollDelta.y > 0)//滚轮向上滑动
{

map.transform.localScale += Vector3.one;//放大image小地图

}
if (Input.mouseScrollDelta.y < 0)//滚轮向下滑动
{

if (map.transform.localScale.x - 1 > 0)//防止缩小超过最大值导致地图反转
{
map.transform.localScale -= Vector3.one;//缩小image小地图
}


}

}

}
}

 3,通过玩家位置改变锚点和地图中玩家转向(此处代码应该写在小地图中,然后获取玩家脚本得到玩家移动数值,偷懒了QWQ)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerGod : MonoBehaviour
{
CharacterController character;
Vector3 leftv3;//左下角位置(世界坐标生成的地图左下角的位置)
Vector3 v3;//玩家的当前位置
public Image map;//小地图
public Image player;//玩家在小地图的image

public
// Start is called before the first frame update
void Start()
{
character = GetComponent<CharacterController>();//获取玩家控制器 此处是为了给玩家加重力
leftv3 = new Vector3(-540,0,-540);//地图在世界坐标中左下角位置
}

// Update is called once per frame
void Update()
{
v3 = transform.position;//玩家位置
float x = (v3.x - leftv3.x) / 1025f;//玩家位置的X-世界中地图的左下角位置求得差值x然后比宽的值 求得玩家在地图中的比值从而给锚点赋值
float z = (v3.z - leftv3.z) / 1025f;//同上
map.rectTransform.pivot = new Vector2(x,z);//给锚点重新赋值
map.transform.localPosition = Vector3.zero;//锚点清零使其移动
player.transform.eulerAngles = new Vector3(0,0, -transform.eulerAngles.y);//根据玩家旋转小地图中的玩家也旋转
character.Move(new Vector3(0, -Time.deltaTime * 3, 0));//模拟重力
transform.position += transform.forward * 10 * Time.deltaTime * Input.GetAxis("Vertical");//玩家移动代码 (此处为瞬移)
transform.eulerAngles += transform.up * 130 * Time.deltaTime * Input.GetAxis("Horizontal");//玩家移动代码

 

}
}

posted @ 2023-03-03 13:48  天分丶张三  阅读(237)  评论(0编辑  收藏  举报