開(kāi)始學(xué)習(xí)Unity3D ,因項(xiàng)目的原因找需要小地圖的表現(xiàn)形式。找了很多實(shí)例代碼,但是感覺(jué)這個(gè)比較簡(jiǎn)潔。
GameObject.FindGameObjectsWithTag(NpcTags)中的的參數(shù),是需要在在UNITY3D 編輯中設(shè)定(must be declared in the tag manager before using them.)
設(shè)置步驟: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;//玩家標(biāo)記(可旋轉(zhuǎn)) 06. public Texture NpcMiniLogo;//NPC標(biāo)記 如建筑 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;//場(chǎng)景實(shí)際寬 12. public float mapHeight=2000;//場(chǎng)景實(shí)際高 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 繪制玩家圖標(biāo)可旋轉(zhuǎn)箭頭 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.}
本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/tjgjp/archive/2010/11/11/6002557.aspx
|