I have New Game text/button in my Canvas/Scene and I want when I click New Game to fade out! READ MORE!
So I want when I click on New Game to slowly fade out. Now also I have a background music playing. I'd like to fade out the music with the screen. And then when the screen faded out I need to switch to my scene that only has black plane and that plane is there to create different images appear. For example my "studio" name BlackPanther. So I want BlackPanther to appear and then disappear slowly and then word "Presents" to appear slowly and disappear slowly and some other texts/logos/images. And then my "firstscene" starts. I made animation, so That will work. But in general I need fade out; and maybe if someone is kind to help - I'd give you credits in my game.
Answer by PPenar · Mar 22, 2017 at 02:30 PM
Attach this script to any GameObject, drag the button or any other canvas element to the CanvasRenderer field. Call StartCanvasFadeOut() on a button click to fade out only 1 element and StartCanvasFamilyFadeOut() to fade out whole family of Canvas Renderers using System.Collections; using UnityEngine;
public class FadeOutEffect : MonoBehaviour
{
[SerializeField]
private CanvasRenderer canvasRenderer;
[SerializeField]
float fadeTime = 1;
[SerializeField]
bool fadeOnStart;
void Start()
{
if (fadeOnStart)
{
StartCanvasFamilyFadeOut();
}
}
public void StartCanvasFadeOut()
{
if (canvasRenderer != null)
{
StartCoroutine(CanvasFadeOut(canvasRenderer));
}
}
public void StartCanvasFamilyFadeOut()
{
if (canvasRenderer != null)
{
StartCoroutine(CanvasFamilyFadeOut(canvasRenderer));
}
}
IEnumerator CanvasFadeOut(CanvasRenderer targetCanvas)
{
float currentTime = fadeTime;
while (currentTime > 0)
{
targetCanvas.SetAlpha(currentTime / fadeTime);
currentTime -= Time.deltaTime;
yield return null;
}
}
IEnumerator CanvasFamilyFadeOut(CanvasRenderer parentCanvas)
{
float currentTime = fadeTime;
CanvasRenderer[] canvasRenderers = parentCanvas.GetComponentsInChildren<CanvasRenderer>();
while (currentTime > 0)
{
foreach (CanvasRenderer canvasRenderer in canvasRenderers)
{
canvasRenderer.SetAlpha(currentTime / fadeTime);
}
currentTime -= Time.deltaTime;
yield return null;
}
}
}
Okay thanks. So what happens to me now, only New Game Text/button disappears, not the screen. But what I don't understand is this
"and StartCanvasFamilyFadeOut() to fade out whole family of Canvas Renderers using System.Collections; using UnityEngine;"
Where do I put this... In the same button? You can answer me, but in the mean time I will try to fix this.
@$$anonymous$$arloBRI$$anonymous$$ You can launch either function - depending if you want to fade out multiple UI elements in the same time or only one. If you want to fade out the whole scene to - let's say - black colour, you can put black panel on screen overlay canvas, and reverse the process - ins$$anonymous$$d of decreasing alpha, increase it
Well to be honest, I only want screen to go black slowly and the sound with it. And then Load another scene that has the black plane and then do the splash things. But I'll try to do this now and see if it works.