using UnityEngine; using System.Collections; public class minMap : MonoBehaviour { public Texture backGround;//小地图背景 public Texture playerMiniLogo;//玩家标记(可旋转) public Texture NpcMiniLogo;//NPC标记 如建筑 public Texture DirectionArrow; public Transform Player;//玩家所在位置 public float arrowAngle=0; //real map size(3d world units) public float mapWidth=2000;//场景实际宽 public float mapHeight=2000;//场景实际高 //minimap size(texture) public float miniMapWidth=256;//小地图宽 public float miniMapHeight=256;//小地图高 // private float backAlpha=0.9f;//背景透明度 public string NpcTags=“NPC”; private GameObject[] DrawNpcs; // Use this for initialization void Start () { DrawNpcs = GameObject.FindGameObjectsWithTag(NpcTags); } // Update is called once per frame void Update () { } void OnGUI() { DrawMiniMap(Screen.width - miniMapWidth, Screen.height - miniMapHeight, 2); } void DrawMiniMap(float LeftX,float LeftY,int PointSize) { GUI.depth=-10; GUI.color=new Color(1,1,1,backAlpha); GUI.DrawTexture(new Rect(LeftX,LeftY,miniMapWidth,miniMapHeight),backGround); //draw npcs if(DrawNpcs != null) { for (int i = 0; i < DrawNpcs.Length;i++ ) { GameObject npc = DrawNpcs[i]; GUI.color = new Color(1, 1, 1, 1); GUI.DrawTexture(new Rect(LeftX + (npc.transform.position.x / mapWidth) * miniMapWidth - (PointSize / 2), LeftY + (miniMapHeight - (npc.transform.position.z / mapHeight) * miniMapHeight - (PointSize / 2)), PointSize, PointSize), NpcMiniLogo); } } //draw direction arrow 绘制玩家图标可旋转箭头 if (DirectionArrow != null) { GUI.depth = 20; GUI.color = new Color(1, 1, 1, 1); GUIUtility.RotateAroundPivot(Player.eulerAngles.y, new Vector2(LeftX + (Player.position.x / mapWidth) * miniMapWidth, LeftY + (miniMapHeight - (Player.position.z / mapHeight) * miniMapHeight))); GUI.DrawTexture(new Rect(LeftX + (Player.position.x / mapWidth) * miniMapWidth - (DirectionArrow.width / 2), LeftY + (miniMapHeight - (Player.position.z / mapHeight) * miniMapHeight - (DirectionArrow.height / 2)), DirectionArrow.width, DirectionArrow.height), DirectionArrow); GUI.matrix = Matrix4x4.identity; } } }