在Unity中,制作滚动背景

2022-04-28  本文已影响0人  全新的饭

效果图

滚动背景.gif

配置

image.png

具体实现

MoveBg.shader

Shader "My/MoveBg" 
{
    Properties 
    {
        _MainTex ("Main Tex", 2D) = "white" {}
         _Color("Tint", Color) = (1,1,1,1)
        _Width ("Width", float) = 1
        _Height("Height",float) = 1
        _XDistance ("XDistance", float) = 0
        _YDistance ("YDistance", float) = 0
    }
    
    SubShader 
    {
        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        
        Pass 
        {
            Tags { "LightMode"="ForwardBase" }
            // 显示在最前 
            // ZTest off
            // 显示在最后
            ZTest[unity_GUIZTestMode]
            ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha
            
            CGPROGRAM
            #pragma vertex vert  
            #pragma fragment frag
            #include "UnityCG.cginc"
        
            sampler2D _MainTex;
            float _Width;
            float _Height;
            float _XDistance;
            float _YDistance;
            fixed4 _Color;
            float4 _MainTex_ST;
            fixed4 _TextureSampleAdd;
              
            struct a2v 
            {  
                float4 vertex : POSITION; 
                float2 texcoord : TEXCOORD0;
                float4 color : COLOR;
            };  
            
            struct v2f 
            {  
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float4 color : COLOR;
            };  
            
            v2f vert (a2v v) 
            {  
                v2f o;  
                o.pos = UnityObjectToClipPos(v.vertex);  
                o.uv = v.texcoord.xy;
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                o.color = v.color * _Color;
                return o;
            }  
            
            fixed4 frag (v2f i) : SV_Target 
            {
                i.uv.x = frac(i.uv.x*_Width + _XDistance);
                i.uv.y = frac(i.uv.y*_Height + _YDistance);
                fixed4 c = i.color * (tex2D(_MainTex, i.uv) + _TextureSampleAdd);
                return c;
            }
            ENDCG
        }  
    }
    FallBack "Transparent/VertexLit"
}

MoveBg.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MoveBg : MonoBehaviour
{
    [SerializeField]
    private Image _img;
    private Material _mat;
    [SerializeField]
    private float _xSpeed = 1f;
    [SerializeField]
    private float _ySpeed = 1f;

    private void Start()
    {
        Init();
    }

    private void Update() 
    {
        _mat.SetFloat("_XDistance", Time.realtimeSinceStartup * _xSpeed);
        _mat.SetFloat("_YDistance", Time.realtimeSinceStartup * _ySpeed);
    }

    private void OnDestroy()
    {
        Destroy();
    }
    
    public void Init()
    {
        _mat = _img.material;
    }
    
    public void Destroy()
    {
         _mat.SetFloat("_XDistance", 0);
         _mat.SetFloat("_YDistance", 0);
         _mat = null;
    }
}
上一篇下一篇

猜你喜欢

热点阅读