How to fade in and out two panels in a scene? (c#)
I searched far and wide about how to make a panel fade in and out, with no success. I have a scene, and two logos, one for my school, and one for my group. I have two panels, one for each logo, and I wanted to make them go:
Black screen > Fade In Panel#1 > Stay (3 sec) > Fade Out Panel#1 > Black Screen (0.5 sec) > Fade In Panel#2 > Stay (3 sec) > Fade Out Panel#3 > Black Screen (0.5 sec) > Fade In Menu. (Do not worry about the menu, it's not important right now).
This is what I have done until now:
public class DelayLogo : MonoBehaviour
{
public float marcador;
public float intervalo;
public GameObject PanelPUCLogo;
public GameObject PanelLuftLogo;
// Use this for initialization
void Start()
{
marcador = Time.time;
intervalo = 2.0f;
}
// Update is called once per frame
void Update()
{
if (Time.time - marcador >= intervalo || Input.anyKey)
{
PanelPUCLogo.SetActive(true);
}
if (Time.time - marcador >= intervalo + 3 || Input.anyKey)
{
PanelLuftLogo.SetActive(true);
PanelPUCLogo.SetActive(false);
}
}
}
This is what this script does:
Black Screen > Panel#1 (3 sec) > Panel#2 (3sec). It doesn't fade in/out anything.
Btw, PanelPUCLogo = Panel#1 In my example; PanelLuftLogo = Panel#2 in my example
Answer by UnityCoach · Apr 14, 2017 at 09:35 AM
If you're using a UI Image component (which I would recommend), you can simply dial into its color alpha channel.
using UnityEngine.UI; // required to access UI components
public Image logo; // assign your logo here
Color logoColor;
float logoOpacity;
float time;
void Awake ()
{
logoColor = logo.color;
logoOpacity = logoColor.a;
}
void Update ()
{
time += Time.deltaTime / 10f; // time will reach 1 after ten seconds
logoOpacity = Mathf.Lerp (1f, 0f, time); interpolating from 1 to 0 with time
logoColor.a = logoOpacity;
logo.color = logoColor;
}