unity 场景背景替换2D图片方法

unity 场景背景替换2D图片方法

方法一 二维图片天空球Shader

特点:

直接替换天空球,操作方便

天空球环境光受背景贴图影响

Shader

Shader "Skybox/Background 2D" 
{
	Properties 
	{		  
		_Color("  Color", color) = (1, 1, 1, 1)
		[NoScaleOffset] _MainTex ("  Map", 2D) = "white" {}
	}

	SubShader 
	{
		Tags { "QUEUE"="Background" 
			   "RenderType"="Background" 
			 } 
		LOD 200
		Cull Off ZWrite Off

		Pass 
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			fixed4 _Color;
			sampler2D _MainTex;

			struct vertOut 
			{
                float4 pos : SV_POSITION;
                float4 scrPos : TEXCOORD0;
            };

            vertOut vert(appdata_base v) 
			{
                vertOut o;
                o.pos = UnityObjectToClipPos (v.vertex);
                o.scrPos = ComputeScreenPos(o.pos);
                
				return o;
            }

            fixed4 frag(vertOut i) : SV_Target 
			{	
                float2 uv = (i.scrPos.xy / i.scrPos.w);

				return tex2D(_MainTex, uv) * _Color;
            }

			ENDCG
		}
	} 
} 

方法二 辅助背景Camera

特点:

设置复杂,控制简单

需要两个Camera

背景Camera设置:

Depth 值要比Main Camera低

Main Camera设置:

ClearFlags设置成Don‘tClear

Camera.main.clearFlags = CameraClearFlags.Nothing;

新建二维背景Canvas:

BackgroundCanvas设置

RenderMode 设置成ScreenSpace-Camera

RenderCamera设置成背景Camera

BackGroundImage背景图片设置:

猜你喜欢

转载自blog.csdn.net/dxs1990/article/details/124116651