服务电话:0531-81180830 | 24小时服务:13176691830
公司新闻

[Unity3d]小地图的制作 .

  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的插件,用来制作小地图就更炫更专业了,这里只是一个粗糙的小地图。你也可以尝试用一下更专业的插件来开发。

上一篇:Java语言与C++语言的差异总结 . 下一篇:【安卓笔记】android客户端与服务端交互的三种方式 .