绘制地图代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlanePlane : MonoBehaviour
{
Texture2D texture;
public int w = 100;
public int h = 100;
public int width = 0;
public int height = 0;
public Color up = Color.green;
public Color down = Color.yellow;
// Start is called before the first frame update
void Start()
{
texture = new Texture2D(w, h);
Mesh mesh = new Mesh();
VertexHelper vh = new VertexHelper();
for (int x = 0; x < w; x++)
{
for (int z = 0; z < h; z++)
{
float y = Mathf.PerlinNoise(x * 0.05f, z * 0.05f);
texture.SetPixel(x, z, Color.Lerp(up, down, y));
float uvx = (float)width*w+(float)x / (float)w;
float uvy = (float)height*h+(float)z / (float)h;
vh.AddVert(new Vector3(width*w+x-w/2, y*10, height*h+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);
}
}
}
texture.Apply();
vh.FillMesh(mesh);
gameObject.AddComponent<MeshFilter>().mesh = mesh;
gameObject.AddComponent<MeshRenderer>();
Material mat = new Material(Shader.Find("Standard"));
mat.mainTexture = texture;
GetComponent<MeshRenderer>().material = mat;
gameObject.AddComponent<MeshCollider>().sharedMesh = mesh;
}
// Update is called once per frame
void Update()
{
}
}
效果
小地图
结构
小地图代码(在绘制地形脚本中处理)
有代码注释的为小地图处理代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CreatePlane : MonoBehaviour
{
Texture2D texture;
public int w = 100;
public int h = 100;
public int width = 0;
public int height = 0;
public Color up = Color.green;
public Color down = Color.yellow;
public Image map;
public GameObject player;
// Start is called before the first frame update
void Start()
{
texture = new Texture2D(w, h);
Mesh mesh = new Mesh();
VertexHelper vh = new VertexHelper();
for (int x = 0; x < w; x++)
{
for (int z = 0; z < h; z++)
{
float y = Mathf.PerlinNoise((width * w + x) * 0.05f, (height * h + z) * 0.05f);
texture.SetPixel(x, z, Color.Lerp(down, up, y));
float uvx = (float)width * w + (float)x / (float)w;
float uvy = (float)height * h + (float)z / (float)h;
vh.AddVert(new Vector3(width * w + x - w / 2, y * 10, height * h + 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);
}
}
}
texture.Apply();
vh.FillMesh(mesh);
gameObject.AddComponent<MeshFilter>().mesh = mesh;
gameObject.AddComponent<MeshRenderer>();
Material mat = new Material(Shader.Find("Standard"));
mat.mainTexture = texture;
GetComponent<MeshRenderer>().material = mat;
gameObject.AddComponent<MeshCollider>().sharedMesh=mesh;
gameObject.AddComponent<NavMeshSourceTag>();
Material mater = new Material(Shader.Find("UI/Default"));//制作小地图材质球
mater.mainTexture = texture;//小地图材质球赋值上方地形所用材质
map.material = mater;//赋值给小地图材质球
}
// Update is called once per frame
void Update()
{
if (Input.GetAxis("Mouse ScrollWheel")!=0 && transform.localScale.x+Input.GetAxis("Mouse ScrollWheel")>=1)//处理放大小地图
{
map.transform.localScale += Vector3.one * Input.GetAxis("Mouse ScrollWheel");
}
map.rectTransform.pivot = new Vector2((w / 2 + player.transform.position.x) / w - width, (h / 2 + player.transform.position.z) / h - height);//处理小地图位置
}
}
代码中的map是
小地图红点角度改变
在人物移动脚本中公开拖入红点
代码(有注释的为小地图人物角度处理代码)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PlayerMoveMove : MonoBehaviour
{
NavMeshAgent nav;
Animator ani;
public Transform myplayer;//小地图中人物代表的图片
// Start is called before the first frame update
void Start()
{
nav = GetComponent<NavMeshAgent>();
ani = GetComponent<Animator>();
myplayer = GameObject.Find("Canvas/Image/my").gameObject.transform;//查找对象
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 pos = new Vector3(x, 0, z);
myplayer.localEulerAngles = Vector3.back * gameObject.transform.eulerAngles.y;//是小地图中图片跟人物移动角度一样
ani.SetBool("run", false);
if (pos!=Vector3.zero)
{
ani.SetBool("run", true);
nav.SetDestination(pos + transform.position);
}
}
}
自动烘焙地形插件的使用
导入后有两个脚本
人物挂载第一个脚本
绘制地图的脚本上添加tig脚本
注意点:
1、NavMeshSourceTag在脚本中最后添加不可外部挂载
2、地形中MeshCollider的convex关闭掉