通过柏林噪声算法生成地图并且生成配套的小地图加滚轮缩放和位置显示

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CreatMaps : MonoBehaviour
{
public int w = 100;
public int h = 100;
//public Image ima;
public Texture2D texture;//
public Color tu = Color.yellow;
public Color shui = Color.blue;
public Image playeriamge;
public GameObject player;
public Image map;
// Start is called before the first frame update
void Start()
{
texture = new Texture2D(w, h);
VertexHelper vh = new VertexHelper();
for (int x = 0; x < w; x++)
{
for (int z = 0; z < h; z++)
{
float y = Mathf.PerlinNoise(x * 0.1f, z * 0.1f);
float uvx = (float)x / (float)(w - 1);
float uvy = (float)z / (float)(h - 1);
Color color = Color.Lerp(tu, shui, y);
texture.SetPixel(x, z, color);
vh.AddVert(new Vector3(x-w/2, y * 10, z-h/2), Color.white, new Vector2(uvx, uvy));
if (x != w - 1 && z != h - 1)
{
vh.AddTriangle(x * h + z, x * h + z + 1, (x + 1) * h + z + 1);
vh.AddTriangle(x * h + z, (x + 1) * h + z + 1, (x + 1) * h + z);
}
if (Random.Range(0,100) >= 99)
{
GameObject tree = Instantiate(Resources.Load<GameObject>("Tree"+Random.Range(11,201)),transform);
tree.transform.position = new Vector3(x - w / 2, y * 10, z - h / 2);
}
}
}
texture.Apply();
Mesh mesh = new Mesh();
vh.FillMesh(mesh);
GetComponent<MeshFilter>().mesh = mesh;
GetComponent<MeshCollider>().sharedMesh = mesh;
Material material = new Material(Shader.Find("UI/Default"));
material.mainTexture = texture;
GetComponent<MeshRenderer>().material = material;
map.material = material;
}
// Update is called once per frame
void Update()
{
float pivotx = (player.transform.position.x + w / 2) / (float)w;
float pivoty = (player.transform.position.z + h / 2) / (float)h;
map.rectTransform.pivot = new Vector2(pivotx, pivoty);
playeriamge.rectTransform.eulerAngles = Vector3.back * player.transform.eulerAngles.y;
if ((map.rectTransform.localScale + Vector3.one * Input.GetAxis("Mouse ScrollWheel")).x>= 1)
{
map.rectTransform.localScale += Vector3.one * Input.GetAxis("Mouse ScrollWheel");
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)