100 lines
2.1 KiB
C#
100 lines
2.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
public class ChangeScene : MonoBehaviour
|
|
{
|
|
public static ChangeScene instance;
|
|
|
|
public float fadeSpeed = 1.5f;
|
|
|
|
private bool startFadeToClear = false;
|
|
|
|
private bool startFadeToBlack = false;
|
|
|
|
private RawImage rawImage;
|
|
|
|
private Action tempAction;
|
|
void Awake()
|
|
{
|
|
instance = this;
|
|
}
|
|
void Start()
|
|
{
|
|
|
|
rawImage = GetComponent<RawImage>();
|
|
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (startFadeToClear)
|
|
FadeToClear();
|
|
|
|
if (startFadeToBlack)
|
|
FadeToBlack();
|
|
}
|
|
|
|
//显示
|
|
private void FadeToClear()
|
|
{
|
|
rawImage.color = Color.Lerp(rawImage.color, Color.clear, fadeSpeed * Time.deltaTime);
|
|
if (rawImage.color.a < 0.1f)
|
|
{
|
|
rawImage.color = Color.clear;
|
|
rawImage.enabled = false;
|
|
startFadeToClear = false;
|
|
Debug.Log(tempAction);
|
|
if (tempAction != null)
|
|
{
|
|
tempAction.Invoke();
|
|
}
|
|
}
|
|
}
|
|
|
|
//渐黑
|
|
private void FadeToBlack()
|
|
{
|
|
rawImage.color = Color.Lerp(rawImage.color, Color.black, fadeSpeed * 2 *Time.deltaTime);
|
|
if (rawImage.color.a > 0.95f)
|
|
{
|
|
rawImage.color = Color.black;
|
|
rawImage.enabled = true;
|
|
startFadeToBlack = false;
|
|
Debug.Log(tempAction);
|
|
if (tempAction != null)
|
|
{
|
|
tempAction.Invoke();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void StartFadeBlack()
|
|
{
|
|
tempAction = null;
|
|
rawImage.enabled = true;
|
|
startFadeToBlack = true;
|
|
tempAction = FadeToClear;
|
|
}
|
|
|
|
public void StartFadeBlack(Action a)
|
|
{
|
|
tempAction = null;
|
|
rawImage.enabled = true;
|
|
startFadeToBlack = true;
|
|
tempAction = a;
|
|
}
|
|
|
|
public void StartFadeClear(Action a)
|
|
{
|
|
tempAction = null;
|
|
rawImage.enabled = true;
|
|
startFadeToClear = true;
|
|
tempAction = a;
|
|
}
|
|
|
|
}
|