[Unity3d]小地圖的制作


繼續今天的學習心得,unity中小地圖的制作,實現了小地圖中紅色小箭頭代表場景中的主角,然后人物方向的轉變,小地圖中箭頭也隨之改變方向。

效果圖                                                                                                                         

 
 
右上角就是小地圖,上面有個紅色小箭頭就是代表主角人物所在場景中的位置,箭頭的方向就代表人物所面向的方向。
 

實現步驟                                                                                                                     

1.俯視圖的制作

首先,我們將Scene場景調成俯視的角度

然后在創建一個Plane,然后點擊該對象,在Inspector屬性窗口將MeshRenderer取消,

 

並且在場景中會發現有綠色的網格,

 


然后記錄網格所在的位置,並且取消MeshCollider,用截圖工具截取這網格所在的位置,截出一張一模一樣大小的圖片,這就是我們小地圖的來源。截取好了之后記得將MeshCollider勾選上,后面會在代碼中需要計算人物所在的位置,也就正是通過這網格文件來計算的。

2.代碼的編寫

[csharp]  view plain copy print ? 在CODE上查看代碼片 派生到我的代碼片
 
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class map : MonoBehaviour {  
  5.   
  6.     public Texture map1;//小地形圖    
  7.     public Texture jueseTexture;//標識角色的圖片    
  8.     float juesePosX = 0;  
  9.     float juesePosY = 0;  
  10.     public GameObject player;//角色    
  11.     public GameObject plane;//地形    
  12.     float planeWidth;//地形的寬    
  13.     float planeHeight;//地形的高    
  14.   
  15.     float angle = 0; //人物旋轉的角度  
  16.   
  17.     void Start()  
  18.     {  
  19.         //獲取地形的寬高    
  20.         planeWidth = plane.GetComponent<MeshFilter>().mesh.bounds.size.x * plane.transform.localScale.x;  
  21.         planeHeight = plane.GetComponent<MeshFilter>().mesh.bounds.size.z * plane.transform.localScale.z;  
  22.         print("width+heith:"+planeWidth + ", " + planeHeight);  
  23.         print("bounds:" + plane.GetComponent<MeshFilter>().mesh.bounds);  
  24.     }  
  25.     void OnGUI()  
  26.     {  
  27.         GUI.DrawTexture(new Rect(Screen.width-map1.width, 0, map1.width, map1.height), map1);  
  28.   
  29.         GUIUtility.RotateAroundPivot(angle, new Vector2((Screen.width - map1.width)+juesePosX + 5, juesePosY + 5));  
  30.   
  31.         GUI.DrawTexture(new Rect((Screen.width - map1.width)+juesePosX, juesePosY, 10, 10), jueseTexture);  
  32.     }  
  33.   
  34.   
  35.     void Update()  
  36.     {  
  37.         print("people:" + player.transform.position.x + "," + player.transform.position.y);  
  38.         print(1);  
  39.         //根據palyer在plane的比例關系,映射到對應地圖位置。    
  40.         juesePosX = map1.width * player.transform.position.x / planeWidth + map1.width / 2;  
  41.         juesePosY = map1.height * (-player.transform.position.z) / planeHeight + map1.height / 2;  
  42.   
  43.         print("x:" + juesePosX + "y:" + juesePosY);  
  44.   
  45.         angle = player.transform.eulerAngles.y-90;  
  46.         print("angle:" + angle);  
  47.     }   
  48. }  

將該腳本拖放到Plane上,參數說明:JueseTexture是指小地圖中箭頭的圖片,Player是人物模型的Controller,Plane是指當前帶網格的Plane,Map1是指小地圖的圖片。
 
當然還有一種KGFMapSystem的插件,用來制作小地圖就更炫更專業了,這里只是一個粗糙的小地圖。你也可以嘗試用一下更專業的插件來開發。
更專業的插件開發小地圖請看:http://blog.csdn.net/dingxiaowei2013/article/details/18669919
 

==================== 迂者 丁小未 CSDN博客專欄=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:858550         cocos2dx QQ群:280818155

====================== 相互學習,共同進步 ===================

 

轉載請注明出處:http://blog.csdn.net/dingxiaowei2013/article/details/18571083


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM