The fucking 3rd time i had to upload this project to git
This commit is contained in:
135
Assets/Plugins/Simple Scene Fade Load System/Scripts/Fader.cs
Normal file
135
Assets/Plugins/Simple Scene Fade Load System/Scripts/Fader.cs
Normal file
@ -0,0 +1,135 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class Fader : MonoBehaviour
|
||||
{
|
||||
[HideInInspector] public bool start;
|
||||
|
||||
[HideInInspector] public float fadeDamp;
|
||||
|
||||
[HideInInspector] public string fadeScene;
|
||||
|
||||
[HideInInspector] public float alpha;
|
||||
|
||||
[HideInInspector] public Color fadeColor;
|
||||
|
||||
[HideInInspector] public bool isFadeIn;
|
||||
|
||||
private Image bg;
|
||||
private float lastTime;
|
||||
private CanvasGroup myCanvas;
|
||||
|
||||
private bool startedLoading;
|
||||
|
||||
//Set callback
|
||||
private void OnEnable()
|
||||
{
|
||||
SceneManager.sceneLoaded += OnLevelFinishedLoading;
|
||||
}
|
||||
|
||||
//Remove callback
|
||||
private void OnDisable()
|
||||
{
|
||||
SceneManager.sceneLoaded -= OnLevelFinishedLoading;
|
||||
}
|
||||
|
||||
public void InitiateFader()
|
||||
{
|
||||
DontDestroyOnLoad(gameObject);
|
||||
|
||||
//Getting the visual elements
|
||||
if (transform.GetComponent<CanvasGroup>())
|
||||
myCanvas = transform.GetComponent<CanvasGroup>();
|
||||
|
||||
if (transform.GetComponentInChildren<Image>())
|
||||
{
|
||||
bg = transform.GetComponent<Image>();
|
||||
bg.color = fadeColor;
|
||||
}
|
||||
|
||||
//Checking and starting the coroutine
|
||||
if (myCanvas && bg)
|
||||
{
|
||||
myCanvas.alpha = 0.0f;
|
||||
StartCoroutine(FadeIt());
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("Something is missing please reimport the package.");
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator FadeIt()
|
||||
{
|
||||
while (!start)
|
||||
//waiting to start
|
||||
yield return null;
|
||||
lastTime = Time.time;
|
||||
var coDelta = lastTime;
|
||||
var hasFadedIn = false;
|
||||
|
||||
while (!hasFadedIn)
|
||||
{
|
||||
coDelta = Time.time - lastTime;
|
||||
if (!isFadeIn)
|
||||
{
|
||||
//Fade in
|
||||
alpha = newAlpha(coDelta, 1, alpha);
|
||||
if (alpha == 1 && !startedLoading)
|
||||
{
|
||||
startedLoading = true;
|
||||
SceneManager.LoadScene(fadeScene);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//Fade out
|
||||
alpha = newAlpha(coDelta, 0, alpha);
|
||||
if (alpha == 0) hasFadedIn = true;
|
||||
}
|
||||
|
||||
lastTime = Time.time;
|
||||
myCanvas.alpha = alpha;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
Initiate.DoneFading();
|
||||
|
||||
Debug.Log("Your scene has been loaded , and fading in has just ended");
|
||||
|
||||
Destroy(gameObject);
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
|
||||
private float newAlpha(float delta, int to, float currAlpha)
|
||||
{
|
||||
switch (to)
|
||||
{
|
||||
case 0:
|
||||
currAlpha -= fadeDamp * delta;
|
||||
if (currAlpha <= 0)
|
||||
currAlpha = 0;
|
||||
|
||||
break;
|
||||
case 1:
|
||||
currAlpha += fadeDamp * delta;
|
||||
if (currAlpha >= 1)
|
||||
currAlpha = 1;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return currAlpha;
|
||||
}
|
||||
|
||||
private void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
StartCoroutine(FadeIt());
|
||||
//We can now fade in
|
||||
isFadeIn = true;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fc74b55a9de3e0e4b8490869beb9118e
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public static class Initiate
|
||||
{
|
||||
private static bool areWeFading;
|
||||
|
||||
//Create Fader object and assing the fade scripts and assign all the variables
|
||||
public static void Fade(string scene, Color col, float multiplier)
|
||||
{
|
||||
if (areWeFading)
|
||||
{
|
||||
Debug.Log("Already Fading");
|
||||
return;
|
||||
}
|
||||
|
||||
var init = new GameObject();
|
||||
init.name = "Fader";
|
||||
var myCanvas = init.AddComponent<Canvas>();
|
||||
myCanvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
||||
init.AddComponent<Fader>();
|
||||
init.AddComponent<CanvasGroup>();
|
||||
init.AddComponent<Image>();
|
||||
|
||||
var scr = init.GetComponent<Fader>();
|
||||
scr.fadeDamp = multiplier;
|
||||
scr.fadeScene = scene;
|
||||
scr.fadeColor = col;
|
||||
scr.start = true;
|
||||
areWeFading = true;
|
||||
scr.InitiateFader();
|
||||
}
|
||||
|
||||
public static void DoneFading()
|
||||
{
|
||||
areWeFading = false;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9d8ace4dfb9f90c4e8213ec4bdc8a745
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user