unity 快速创建小地图

  1. 先写一个纹理遮罩shader

Shader "Unlit/TexMask"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_MaskTex("Mask Texture",2D) = "white"{}
	}
	SubShader
	{
		Tags {"Queue"="Transparent" "RenderType"="Transparent" }
		LOD 100

		Pass
		{
			Blend SrcAlpha OneMinusSrcAlpha
			CGPROGRAM
			#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 texture
				fixed4 col = tex2D(_MainTex, i.uv)*tex2D(_MaskTex,i.uv);
				// apply fog
				UNITY_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 initialization
	void 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 frame
	void Update () {
        miniMapCamera.transform.position = target.position + new Vector3(0, 30, 0);
        miniMapCamera.transform.LookAt(target.position);

    }
}

3.显示小地图


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


原文地址:https://www.cnblogs.com/liang123/p/6325892.html