using UnityEngine; using System.Collections; /* * 小地图制作 */ public class MiniMap : MonoBehaviour { //大地图地形对象 GameObject plane; //大地图主角对象 GameObject cube; //大地图的宽度 float mapWidth; //大地图的高度 float mapHeight; //地图边界的检测数值 float widthCheck; float heightCheck; //小地图主角的位置 float mapcube_x = 0; float mapcube_y = 0; float mapcube1_x; float mapcube1_y; //GUI按钮是否被按下 bool keyUp; bool keyDown; bool keyLeft; bool keyRight; //小地图的背景贴图 public Texture map; //小地图的主角贴图 public Texture map_cube; GameObject[] cubes; public float speedMove = 20; private void Awake() { cubes = GameObject.FindGameObjectsWithTag("cube"); Debug.Log(cubes[0].name); } void Start () { //得到大地图对象 plane = GameObject.Find("Plane"); //得到大地图主角对象 cube = GameObject.Find("Player"); //得到大地图默认宽度 float size_x = plane.GetComponent<MeshFilter>().mesh.bounds.size.x; //得到大地图宽度的缩放比例 float scal_x = plane.transform.localScale.x; //得到大地图默认高度 float size_z = plane.GetComponent<MeshFilter>().mesh.bounds.size.z; //得到大地图高度缩放地理 float scal_z = plane.transform.localScale.z; //原始宽度乘以缩放比例计算出真实宽度 mapWidth = size_x * scal_x; mapHeight = size_z * scal_z; //越界监测的宽度 widthCheck = mapWidth / 2; heightCheck = mapHeight / 2; check(); } void OnGUI() { keyUp = GUI.RepeatButton(new Rect(500,10,100,30),"向前移动"); keyDown = GUI.RepeatButton(new Rect(500,40,100,30),"向后移动"); keyLeft = GUI.RepeatButton(new Rect(500,70,100,30),"向左移动"); keyRight = GUI.RepeatButton(new Rect(500,100,100,30),"向右移动"); //绘制小地图背景 GUI.DrawTexture(new Rect(0, 0, map.width, map.height), map); //绘制小地图上的“主角” GUI.DrawTexture(new Rect(mapcube_x, mapcube_y, map_cube.width, map_cube.height),map_cube); //绘制地面上的Cube物体 //foreach (var obj in cubes) //{ // float x1 = obj.transform.position.x; // float z1 = obj.transform.position.z; // mapcube1_x = (map.width / mapWidth * x1) + map.width / 2; // mapcube1_y = ((map.height / mapHeight * z1) + map.height / 2); // GUI.DrawTexture(new Rect(mapcube1_x, mapcube1_y, map_cube.width, map_cube.height), map_cube); //} } void FixedUpdate() { if(keyUp) { //向前移动 cube.transform.Translate(Vector3.forward* Time.deltaTime *speedMove); check(); } if(keyDown) { //向后移动 cube.transform.Translate(-Vector3.forward* Time.deltaTime * speedMove); check(); } if(keyLeft) { //向左移动 cube.transform.Translate(-Vector3.right* Time.deltaTime * speedMove); check(); } if(keyRight) { //向右移动 cube.transform.Translate(Vector3.right* Time.deltaTime * speedMove); check(); } } //越界检测 void check() { //得到当前主角在地图中的坐标 float x = cube.transform.position.x; float z = cube.transform.position.z; //当控制主角超过地图范围时,重新计算主角坐标 if(x >= widthCheck) { x = widthCheck; } if(x <= -widthCheck) { x = -widthCheck; } if(z >= heightCheck) { z = heightCheck; } if(z <= -heightCheck) { z = -heightCheck; } cube.transform.position = new Vector3(x, cube.transform.position.y, z); //根据比例计算小地图“主角”的坐标 mapcube_x = (map.width / mapWidth * x) + map.width / 2; mapcube_y = ((map.height / mapHeight * z) + map.height / 2); //Debug.Log (mapcube_x+","+mapcube_y); } }
按图中图中要求创建GameObject
将MiniMap.cs脚本挂在MainCamera摄像机上
按下图放入对应的贴图,设合理的移动速度
4.最终效果图如下所示
左上角小地图中的一个黑色小点表示Player红色方块的位置。
**参考来源:http://www.cnblogs.com/wuzhang/p/wuzhang20141111.html