unity 不切换场景黑屏淡入淡出过渡

using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class BlackViewTransition : MonoBehaviour
{

    public Transform canvasOld;
    public Transform canvasNew;
    public MyMessage beforeOpenMsg;//在幕布拉开前执行的方法
    public MyMessage afterOpenMsg;//在幕布拉开前执行的方法

    [ContextMenu("Play")]
    public void Play()
    {

        GameObject blackGO = null;
        if (canvasOld) {
            blackGO = CreateBlack(canvasOld.transform);
        }
        else {
            blackGO = CreateBlack(this.transform);
        }

        blackGO.AddComponent().time = 1;
        blackGO.GetOrAddComponent().alpha = 0;
        blackGO.GetOrAddComponent().DOFade(1, 0.5f).OnComplete(() => {
            blackGO.GetOrAddComponent().DOFade(0, 0.5f);
            //Destroy(blackGO, 0.5f);
            Step2();
        });

        Invoke("AfterMsg",1);
    }


    public void Play(UnityAction centerAction,UnityAction finAction) {
        Play();
        MySuperInvoke.Run(0.5f,()=> {
            centerAction.Invoke();
        });
        MySuperInvoke.Run(1f, () => {
            finAction.Invoke();
        });
    }

    void Step2()
    {
        beforeOpenMsg.SendMessage();

        //GameObject blackGO = CreateBlack(canvasNew.transform);
        if (canvasOld) {
            canvasOld.gameObject.SetActive(false);
            canvasOld.GetOrAddComponent().alpha = 0;
            canvasOld.GetOrAddComponent().blocksRaycasts = false;

            ParticleSystem[] particleSystems = canvasOld.GetComponentsInChildren();
            foreach (ParticleSystem item in particleSystems)
            {
                item.Stop();
            }
        }
        if (canvasNew) {
            canvasNew.gameObject.SetActive(true);
            canvasNew.GetOrAddComponent().alpha = 1;
            canvasNew.GetOrAddComponent().blocksRaycasts = true;

        }
    }
    void AfterMsg() {
        if (afterOpenMsg != null) {
            afterOpenMsg.SendMessage();
        }
    }

    GameObject CreateBlack(Transform parent)
    {
        GameObject blackGO = new GameObject("blackGO");
        blackGO.transform.parent = parent.GetComponentInParent().transform;
        blackGO.transform.localScale = Vector3.one;
        blackGO.transform.localPosition = Vector3.zero;
        MyUGUI.ToFullScreen(blackGO);
        blackGO.AddComponent().color = new Color(0, 0, 0, 1);

        return blackGO;
    }

    public static BlackViewTransition GetActiveBlackView() {
        BlackViewTransition go = GameObject.FindObjectOfType();
        if (go != null) {
            return go;
        }

        GameObject singleton = new GameObject("BlackView");
        return singleton.AddComponent();
    }

    public void Clear() {
        canvasOld = null;
        canvasNew = null;
        beforeOpenMsg = new MyMessage();
        afterOpenMsg = new MyMessage();

    }

}

你可能感兴趣的:(unity 不切换场景黑屏淡入淡出过渡)