unity 快速创建小地图

来源:互联网 发布:好用的隔离霜 知乎 编辑:程序博客网 时间:2024/05/17 05:50
  1. 先写一个纹理遮罩shader

Shader "Unlit/TexMask"{Properties{_MainTex ("Texture", 2D) = "white" {}_MaskTex("Mask Texture",2D) = "white"{}}SubShader{Tags {"Queue"="Transparent" "RenderType"="Transparent" }LOD 100Pass{Blend SrcAlpha OneMinusSrcAlphaCGPROGRAM#pragma vertex vert#pragma fragment frag// make fog work#pragma multi_compile_fog#include "UnityCG.cginc"struct appdata{float4 vertex : POSITION;float2 uv : TEXCOORD0;};struct v2f{float2 uv : TEXCOORD0;UNITY_FOG_COORDS(1)float4 vertex : SV_POSITION;};sampler2D _MainTex;float4 _MainTex_ST;sampler2D _MaskTex;v2f vert (appdata v){v2f o;o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);o.uv = TRANSFORM_TEX(v.uv, _MainTex);UNITY_TRANSFER_FOG(o,o.vertex);return o;}fixed4 frag (v2f i) : SV_Target{// sample the texturefixed4 col = tex2D(_MainTex, i.uv)*tex2D(_MaskTex,i.uv);// apply fogUNITY_APPLY_FOG(i.fogCoord, col);return col;}ENDCG}}}

原理就是:初始图片*遮罩图片,需要隐藏的地方,遮罩图片的alpha值为0,需要显示的地方为白色,这里我设置的是一张圆形遮罩,后面就是一张圆形的小地图


2.写一个小地图脚本,用来显示小地图,原理就是在目标头上生成一个摄像机,让摄像机跟随目标,并将看到的俯视图转为小地图的纹理

using UnityEngine;using UnityEngine.UI;using System.Collections;public class MiniMap : MonoBehaviour {    public Transform target;    private Camera miniMapCamera;    private RenderTexture renderTex;    public Image miniMapTex;// Use this for initializationvoid Start () {        miniMapCamera = new GameObject("MiniMapCamera", typeof(Camera)).GetComponent<Camera>();        renderTex = new RenderTexture((int)miniMapCamera.pixelWidth, (int)miniMapCamera.pixelHeight, 1);        miniMapCamera.targetTexture = renderTex;        miniMapTex.material.SetTexture("_MainTex", renderTex);    }// Update is called once per framevoid Update () {        miniMapCamera.transform.position = target.position + new Vector3(0, 30, 0);        miniMapCamera.transform.LookAt(target.position);    }}

3.显示小地图


4.这里功能都写得特别简单粗糙,适当在上面添加一些功能和美术,应该还是蛮好看的


0 0
原创粉丝点击