【Unity ShaderGraph使用雪碧图制作导航光效效果】

效果:
请添加图片描述
ShaderGraph的制作:
请添加图片描述
shader中uv数和雪碧图的uv数对上就能出效果。

直接上代码:

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

public class TurnDirEffectController : MonoBehaviour
{
    
    
    public RectTransform effecRrectTf;
    public Material mat;
    public int currentLevel = -1;
    public float totalTime;
    public float currentSpeed;

    public Texture2D[] textures;
    public float[] mainColorAlpha;

    public bool isEffectOver;
    public int trunValue = -2;

    public Button[] testBtn;

    public Sprite dayimg;
    public Sprite nightimg;
    void Start()
    {
    
    
        Application.targetFrameRate = 24;

        StopTween();

        testBtn[0].onClick.AddListener(() =>  Btn1());
        testBtn[1].onClick.AddListener(() => {
    
     Btn2(); });
        testBtn[2].onClick.AddListener(() => {
    
     Btn3(); });
        testBtn[3].onClick.AddListener(() => {
    
     Btn4(); });
        testBtn[4].onClick.AddListener(() => {
    
     Btn5(); });
    }
    public void StopTween()
    {
    
    
        trunValue = -2;
        mat.SetFloat("_alpha", 0);
        totalTime = 0;
    }

    float time;
    float time1;
    float time2;
    float time3;

    private DayOrNightMode mo;
    void Update()
    {
    
    
#if UNITY_EDITOR
        if (Input.GetKeyDown(KeyCode.Z))
        {
    
    
            mo = DayOrNightMode.Day;
            Turn_new2(0, 0, mo);//左1
        }
        if (Input.GetKeyDown(KeyCode.X))
        {
    
    
            mo = DayOrNightMode.Day;
            Turn_new2(1, 0, mo);//右1
        }
        if (Input.GetKeyDown(KeyCode.C))
        {
    
    
            mo = DayOrNightMode.Day;
            Turn_new2(0, 1, mo);//左2
        }
        if (Input.GetKeyDown(KeyCode.V))
        {
    
    
            mo = DayOrNightMode.Day;
            Turn_new2(1, 1, mo);//右2
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
    
    
            trunValue = -2;
        }
#endif

        //time += Time.deltaTime;
        //time1 += Time.deltaTime;
        //time2 += Time.deltaTime;
        //time3 += Time.deltaTime;
        //if (time > 5 && time < 11)
        //{
    
    
        //    Turn_new2(-1, 1, DayOrNightMode.Day);
        //    time = 11;
        //}
        //if (time1 > 11 && time1 < 17)
        //{
    
    
        //    Turn_new2(1, 1, DayOrNightMode.Day);//右1
        //    time1 = 17;
        //}
        //if (time2 > 17 && time2 < 25)
        //{
    
    
        //    Turn_new2(-1, 2, DayOrNightMode.Day);//左2
        //    time2 = 25;
        //}
        //if (time3 > 25 && time3 < 33)
        //{
    
    
        //    Turn_new2(1, 2, DayOrNightMode.Day);//右2
        //    //time3 = 33;
        //}
        //if (time3 > 33)
        //{
    
    
        //    Btn5();
        //}

        if (isEffectOver)
        {
    
    
            return;
        }

        totalTime += Time.deltaTime * currentSpeed;

        //每次周期结束时判断一下
        if (totalTime >= 48f)
        {
    
    
            if (trunValue == -2)
            {
    
    
                totalTime = 0;
                effecRrectTf.gameObject.SetActive(false);
                isEffectOver = true;
            }
            else
            {
    
    
                totalTime = 0;
                if(trunValue == 0)
                {
    
    
                    effecRrectTf.localScale = new Vector3(-1, 1, 1);
                }
                if (trunValue == 1)
                {
    
    
                    effecRrectTf.localScale = new Vector3(1, 1, 1);
                }
            }
            //1秒30帧,每帧时间100/3=33;
            float picturePreSecont = 33;
            if (currentLevel == -1)
            {
    
    
                speedTye = SpeedType.normal;
                currentSpeed = picturePreSecont / 1;
            }
            else if (currentLevel == 0)
            {
    
    
                speedTye = SpeedType.high;
                currentSpeed = picturePreSecont / 1.5f;
            }
            else if (currentLevel == 1)
            {
    
    
                speedTye = SpeedType.low;
                currentSpeed = picturePreSecont / 2.5f;
            }
            OnDayOrNight(mo, currentLevel);
        }

        totalTime %= 48;

        mat.SetFloat("_timer", totalTime);
    }

    public void naviLightMessage(string data)
    {
    
    
        //data=0-1-1   第一个开关导航0=关闭、1=开启    第二个表示左右 0=左  1=右    第三个表示快慢  0=快  1=慢
        Debug.Log("receiveNaviMessage params=" + data);

        string[] value = data.Split('-');
        if (int.Parse(value[0])==0)
        {
    
    
            trunValue = -2;
        }
        else if(int.Parse(value[0]) == 1)
        {
    
    
            Turn_new2(int.Parse(value[1]), int.Parse(value[2]), DayOrNightMode.Day);
        }
    }

    public void Turn_new2(int value, int speedValue, DayOrNightMode mode)
    {
    
    
        //Application.targetFrameRate = 24;

        currentLevel = speedValue;
        if (trunValue == value)
        {
    
    
            //MR_Debug.LogI(null, "TrunValue==value and return" + value);
            return;
        }

        trunValue = value;
        if (trunValue == -2)
        {
    
    
            return;
        }

        //DayNight(mode);

        if (trunValue == 0)
        {
    
    
            effecRrectTf.localScale = new Vector3(-1, 1, 1);
        }
        if (trunValue == 1)
        {
    
    
            effecRrectTf.localScale = new Vector3(1, 1, 1);
        }
        //effecRrectTf.localScale = new Vector3(trunValue, 1, 1);

        //currentLevel = speedValue;
        totalTime = 0;
        //0 normal 1 high 2 low
        //1秒30帧,每帧时间100/3=33;
        float picturePreSecont = 33;
        if (currentLevel == -1)
        {
    
    
            speedTye = SpeedType.normal;
            currentSpeed = picturePreSecont / 1;
        }
        else if (currentLevel == 0)
        {
    
    
            speedTye = SpeedType.high;
            currentSpeed = picturePreSecont / 1.5f;
        }
        else if (currentLevel == 1)
        {
    
    
            speedTye = SpeedType.low;
            currentSpeed = picturePreSecont / 2.5f;
        }
        //重新播放
        OnDayOrNight(mode, speedValue);


        isEffectOver = false;
        effecRrectTf.gameObject.SetActive(true);

    }
    private void OnDayOrNight(DayOrNightMode mode, int speedValue)
    {
    
    
        //重头播
        // totalTime = 0;
        mat.SetFloat("_alpha", 1);
        //更换贴图和透明度
        if (mode == DayOrNightMode.Day)
        {
    
    
            //if (speedValue == 0)
            //{
    
    
            //    mat.SetTexture("_MainTex", textures[0]);
            //}
            //else if(speedValue == 1)
            //{
    
    
            //    mat.SetTexture("_MainTex", textures[1]);
            //}

            mat.SetTexture("_MainTex", textures[0]);
            if (speedTye == SpeedType.low)
            {
    
    
                mat.SetFloat("_alpha", mainColorAlpha[1]);
            }
            else if(speedTye == SpeedType.high)
            {
    
    
                mat.SetFloat("_alpha", mainColorAlpha[0]);
            }

        }
        else if (mode == DayOrNightMode.Night)
        {
    
    
            //if (speedValue == 0)
            //{
    
    
            //    mat.SetTexture("_MainTex", textures[2]);
            //}
            //else if (speedValue == 1)
            //{
    
    
            //    mat.SetTexture("_MainTex", textures[3]);
            //}

            mat.SetTexture("_MainTex", textures[1]);
            if (speedTye == SpeedType.low)
            {
    
    
                mat.SetFloat("_alpha", mainColorAlpha[1]);
            }
            else if (speedTye == SpeedType.high)
            {
    
    
                mat.SetFloat("_alpha", mainColorAlpha[0]);
            }
        }
    }
    private void Btn1()
    {
    
    
        Turn_new2(0, 0, DayOrNightMode.Day);
    }
    private void Btn2()
    {
    
    
        Turn_new2(1, 0, DayOrNightMode.Day);
    }
    private void Btn3()
    {
    
    
        Turn_new2(0, 1, DayOrNightMode.Day);//左2
    }
    private void Btn4()
    {
    
    
        Turn_new2(1, 1, DayOrNightMode.Day);//右2
    }
    private void Btn5()
    {
    
    
        trunValue = -2;
    }

    private void DayNight(DayOrNightMode mode)
    {
    
    
        if(mode== DayOrNightMode.Day)
        {
    
    
            transform.GetComponent<Image>().sprite = dayimg;
        }
        else if(mode == DayOrNightMode.Night)
        {
    
    
            transform.GetComponent<Image>().sprite = nightimg;
        }
    }
    public enum SpeedType
    {
    
    
        normal,
        high,
        low
    }
    public SpeedType speedTye;

    public enum DayOrNightMode
    {
    
    
        Day,
        Night,
    }
}

代码绑定:
请添加图片描述

下面是雪碧图:
请添加图片描述

猜你喜欢

转载自blog.csdn.net/CPCM_VN/article/details/130926812