Hi,推荐文件给你 "小地图相关素材.zip" http://vdisk.weibo.com/s/LdtOl
using UnityEngine;
using System.Collections;
public class Main : MonoBehaviour {
GameObject plane;
GameObject cube;
float mapWidth;
float mapHeight;
float widthCheck;
float heightCheck;
float mapCube_x = 0;
float mapCube_y = 0;
bool keyUp;
bool keyDown;
bool keyLeft;
bool keyRight;
public Texture map;
public Texture map_cube;
// Use this for initialization
void Start ()
{
plane = GameObject.Find("Plane");
cube = GameObject.Find("Cube");
//得到大地图默认宽度
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;
mapHeight = mapHeight / 2;
Check();
}
void OnGUI()
{
keyUp = GUILayout.RepeatButton("Up");
keyDown = GUILayout.RepeatButton("Down");
keyLeft = GUILayout.RepeatButton("Left");
keyRight = GUILayout.RepeatButton("Right");
//绘制小地图背景
GUI.DrawTexture(new Rect(Screen.width - map.width,0,map.width,map.height),map);
//绘制小地图上的“主角”
GUI.DrawTexture(new Rect(mapCube_x,mapCube_y,map_cube.width,map_cube.height),map_cube);
}
void FixedUpdate()
{
if(keyUp)
{
//向前移动
cube.transform.Translate(new Vector3(0,0,5 * Time.deltaTime));
Check();
}
if(keyDown)
{
//向后移动
cube.transform.Translate(-Vector3.forward * Time.deltaTime * 5);
Check();
}
if(keyLeft)
{
//向左移动
cube.transform.Translate(Vector3.left * Time.deltaTime * 5);
Check();
}
if(keyRight)
{//向右移动
cube.transform.Translate(Vector3.right * Time.deltaTime * 5);
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;
}
cube.transform.position = new Vector3(x,cube.transform.position.y,z);
//根据比例计算小地图“主角”的坐标
mapCube_x = (map.width/mapWidth * x) + ((map.width / 2) - (map_cube.width / 2)) + (Screen.width - map.width);
mapCube_y = map.height - ((map.height / mapHeight * z) + (map.height / 2));
}
// Update is called once per frame
void Update () {
}
}