制作小地图: 使用 Camera 渲染出来 Render Texture
游戏项目已托管到 GitHub 上 传送门 https://github.com/luoweiwu/Unity_Minimap_plug-in
小地图效果:
(不足: 当玩家旋转方向的时候, 并未对玩家 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);
- }
- }
- PlayerMove.cs
添加几个 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);
- }
- }
- EnemyMove.cs
实现过程
给 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 标签 (地面)
来源: https://www.cnblogs.com/1138720556Gary/p/9932608.html