重庆北站:小地图范例
开始学习Unity3D ,因项目的原因找需要小地图的表现形式。找了很多实例代码,但是感觉这个比较简洁。
GameObject.FindGameObjectsWithTag(NpcTags)中的的参数,是需要在在UNITY3D 编辑中设定(must be declared in the tag manager before using them.)
设置步骤:Edit-ProjectSettings-Tags
view plaincopy to clipboardprint?
01.using UnityEngine;
02.using System.Collections;
03.public class minMap : MonoBehaviour {
04. public Texture backGround;//小地图背景
05. public Texture playerMiniLogo;//玩家标记(可旋转)
06. public Texture NpcMiniLogo;//NPC标记 如建筑
07. public Texture DirectionArrow;
08. public Transform Player;//玩家所在位置
09. public float arrowAngle=0;
10. //real map size(3d world units)
11. public float mapWidth=2000;//场景实际宽
12. public float mapHeight=2000;//场景实际高
13. //minimap size(texture)
14. public float miniMapWidth=256;//小地图宽
15. public float miniMapHeight=256;//小地图高
16. //
17. private float backAlpha=0.9f;//背景透明度
18. public string NpcTags="NPC";
19. private GameObject[] DrawNpcs;
20. // Use this for initialization
21. void Start () {
22. DrawNpcs = GameObject.FindGameObjectsWithTag(NpcTags);
23. }
24.
25. // Update is called once per frame
26. void Update () {
27.
28. }
29. void OnGUI()
30. {
31. DrawMiniMap(Screen.width - miniMapWidth, Screen.height - miniMapHeight, 2);
32. }
33. void DrawMiniMap(float LeftX,float LeftY,int PointSize)
34. {
35. GUI.depth=-10;
36. GUI.color=new Color(1,1,1,backAlpha);
37. GUI.DrawTexture(new Rect(LeftX,LeftY,miniMapWidth,miniMapHeight),backGround);
38.
39.
40. //draw npcs
41. if(DrawNpcs != null)
42. {
43. for (int i = 0; i < DrawNpcs.Length;i++ )
44. {
45. GameObject npc = DrawNpcs[i];
46. GUI.color = new Color(1, 1, 1, 1);
47. 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);
48. }
49. }
50.
51. //draw direction arrow 绘制玩家图标可旋转箭头
52. if (DirectionArrow != null)
53. {
54. GUI.depth = 20;
55. GUI.color = new Color(1, 1, 1, 1);
56. GUIUtility.RotateAroundPivot(Player.eulerAngles.y, new Vector2(LeftX + (Player.position.x / mapWidth) * miniMapWidth, LeftY + (miniMapHeight - (Player.position.z / mapHeight) * miniMapHeight)));
57. 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);
58. GUI.matrix = Matrix4x4.identity;
59. }
60. }
61.}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/tjgjp/archive/2010/11/11/6002557.aspx