制作小地圖:使用Camera渲染出來Render Texture
原理:使用攝像機從上到下獲得場景游戲物體,攝像機Culling Mask渲染層級可設置是否需要在小地圖上展示游戲物體,將攝像機獲得的場景實時在NGUI上Texture中
游戲項目已托管到Github上: 傳送門
小地圖自刷新制作Minimap小地圖: 傳送門
小地圖效果:
(不足:當玩家旋轉方向的時候,並未對玩家UI進行角度轉換~)
預制場景
創建一個場景Gary_map
調整場景燈光亮度Intensity為0.3
添加一個Plane地面,給地面添加材質模擬地圖場景
添加一個Capsule物體作為玩家Player,為Player綁定PlayerMove腳本控制其移動

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMove : MonoBehaviour { public float speed = 4; // Use this for initialization void Start () { } // Update is called once per frame void Update () { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); transform.Translate(new Vector3(h,0,v)*speed*Time.deltaTime); } }
添加幾個Capsule物體作為敵人Enemy,給Enemy添加腳本使其隨機移動
添加材質給Player,區別於敵人
添加Ground標簽給地面,Human標簽給玩家和敵人(目的:只用來作為攝像機Culling Mask渲染層級,不做玩家和敵人區分)

using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyMove : MonoBehaviour { public float speed =4; private float timer = 0; private float dirx = 0; private float dirz = 0; // Update is called once per frame void Update () { timer += Time.deltaTime; if (timer > 4) { dirx = Random.Range(-1f, 1f); dirz = Random.Range(-1f, 1f); timer = 0; } transform.Translate(new Vector3(dirx, 0, dirz) * speed * Time.deltaTime); } }
實現過程
給Player添加Quad,作為小地圖的mapicon,放到Player正上方並將其x軸旋轉90°
給mapicon添加一個圖標,材質設為Diffuse(透明)
給mapicon一個Minimap標簽
給每個敵人AI一個紅色的icon
添加一個Camera放到Player中,Position設置為(0,9,0),X軸旋轉90°,命名為minimap-camera
設置minimap-camera的Projection為Orthographic
minimap-camera小地圖大小由Size控制
為了將在小地圖上看不見敵人,將Culling Mask取消Human的渲染
將小地圖渲染的視覺渲染到Render Texture圖片上
創建一個新的Render Texture,命名為minimap-texture,將minimap-texture綁定到minimap-camera上的Target Texture
使用NGUI添加小地圖進場景中
添加NUI編輯包
版本遺棄問題:遇到提示RuntimePlatform.WindowsWebPlayerNGUI過時,將WindowsWebPlayerNGUI修改為WindowsPlayerNGUI
添加NGUI中第一個背景UI Root
UI Root添加一個Simple Texture
將Texture放到UI Root右上角並將minimap-texture指定到UITexture上的Texture中
將Texture下的Anchors設置為Unified,放置到UI Root右上方
將正方形地圖制作成圓形地圖
制作一個自定義材質Mask,取名minimap-mat
將minimap-mat放置到Texture下的Material中,可通過改變Size來改變小地圖的大小
切換3D視角,地圖camera渲染地面
Main Camera和minimap-camera下的Culling Mask設置渲染Ground標簽(地面)