unity 快速创建小地图

发表于2017-04-21
评论0 1.6k浏览
  1. 先写一个纹理遮罩shader

  1. Shader "Unlit/TexMask"  
  2. {  
  3.     Properties  
  4.     {  
  5.         _MainTex ("Texture", 2D) = "white" {}  
  6.         _MaskTex("Mask Texture",2D) = "white"{}  
  7.     }  
  8.     SubShader  
  9.     {  
  10.         Tags {"Queue"="Transparent" "RenderType"="Transparent" }  
  11.         LOD 100  
  12.   
  13.         Pass  
  14.         {  
  15.             Blend SrcAlpha OneMinusSrcAlpha  
  16.             CGPROGRAM  
  17.             #pragma vertex vert  
  18.             #pragma fragment frag  
  19.             // make fog work  
  20.             #pragma multi_compile_fog  
  21.              
  22.             #include "UnityCG.cginc"  
  23.   
  24.             struct appdata  
  25.             {  
  26.                 float4 vertex : POSITION;  
  27.                 float2 uv : TEXCOORD0;  
  28.             };  
  29.   
  30.             struct v2f  
  31.             {  
  32.                 float2 uv : TEXCOORD0;  
  33.                 UNITY_FOG_COORDS(1)  
  34.                 float4 vertex : SV_POSITION;  
  35.             };  
  36.   
  37.             sampler2D _MainTex;  
  38.             float4 _MainTex_ST;  
  39.             sampler2D _MaskTex;  
  40.   
  41.             v2f vert (appdata v)  
  42.             {  
  43.                 v2f o;  
  44.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);  
  45.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);  
  46.                 UNITY_TRANSFER_FOG(o,o.vertex);  
  47.                 return o;  
  48.             }  
  49.               
  50.             fixed4 frag (v2f i) : SV_Target  
  51.             {  
  52.                 // sample the texture  
  53.                 fixed4 col = tex2D(_MainTex, i.uv)*tex2D(_MaskTex,i.uv);  
  54.                 // apply fog  
  55.                 UNITY_APPLY_FOG(i.fogCoord, col);  
  56.                 return col;  
  57.             }  
  58.             ENDCG  
  59.         }  
  60.     }  
  61. }  

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


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

  1. using UnityEngine;  
  2. using UnityEngine.UI;  
  3. using System.Collections;  
  4.   
  5. public class MiniMap : MonoBehaviour {  
  6.     public Transform target;  
  7.     private Camera miniMapCamera;  
  8.     private RenderTexture renderTex;  
  9.     public Image miniMapTex;  
  10.     // Use this for initialization  
  11.     void Start () {  
  12.         miniMapCamera = new GameObject("MiniMapCamera"typeof(Camera)).GetComponent();  
  13.         renderTex = new RenderTexture((int)miniMapCamera.pixelWidth, (int)miniMapCamera.pixelHeight, 1);  
  14.         miniMapCamera.targetTexture = renderTex;  
  15.         miniMapTex.material.SetTexture("_MainTex", renderTex);  
  16.     }  
  17.       
  18.     // Update is called once per frame  
  19.     void Update () {  
  20.         miniMapCamera.transform.position = target.position + new Vector3(0, 30, 0);  
  21.         miniMapCamera.transform.LookAt(target.position);  
  22.   
  23.     }  
  24. }  

3.显示小地图


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

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引