Hi,推荐文件给你 "小地图相关素材.zip" http://vdisk.weibo.com/s/LdtOl
本文代码例子 http://vdisk.weibo.com/s/BDn59yfnC5Xt3
using UnityEngine;
using System.Collections;
public class Scene : MonoBehaviour
{
GameObject plane;
GameObject cube;
float mapWidth;
float mapHeight;
float widthCheck;
float heightCheck;
float mapcube_x = 0;
float mapcube_y = 0;
public Texture map;
public Texture map_cube;
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.y;
mapWidth = size_x * scal_x;
mapHeight = size_z * scal_z;
Check();
}
void OnGUI()
{
if(GUILayout.RepeatButton("left",GUILayout.Height(50)))
{
cube.transform.Translate(Vector3.left * Time.deltaTime * 5);
Check();
}
if(GUILayout.RepeatButton("right",GUILayout.Height(50)))
{
cube.transform.Translate(Vector3.right * Time.deltaTime * 5);
Check();
}
if(GUILayout.RepeatButton("forward",GUILayout.Height(50)))
{
cube.transform.Translate(Vector3.forward * Time.deltaTime * 5);
Check();
}
if(GUILayout.RepeatButton("back",GUILayout.Height(50)))
{
cube.transform.Translate(Vector3.back * Time.deltaTime * 5);
Check();
}
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 Check()
{
float x = cube.transform.position.x;
float z = cube.transform.position.z;
mapcube_x = (map.width / mapWidth * x) + ((map.width / 2) - (map_cube.width / 2)) + (Screen.width - map.width) ;
mapcube_y = map.height/ 2 - (map.height / mapHeight * z) ;
}
}