SHADE_Y3/Assets/Scripts/UI/SC_SceneFadeInOut.cs

133 lines
3.1 KiB
C#
Raw Normal View History

2023-02-27 15:28:05 +08:00
using SHADE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SHADE_Scripting.UI
{
public class SceneFadeInOut : Script
{
2023-03-01 17:36:02 +08:00
//[Tooltip("If true, the panel fadeUI fades in (increasing alpha). If false, no fading. Mutually exclusive with fadeIn")]
private bool fadeIn;
2023-02-27 15:28:05 +08:00
2023-03-01 17:36:02 +08:00
//[Tooltip("If true, the panel fadeUI fades out (decreasing alpha). If false, no fading. Mutually exclusive with fadeIn")]
private bool fadeOut;
2023-02-27 15:28:05 +08:00
2023-03-01 17:36:02 +08:00
[Tooltip("The initial alpha value of the UI that is faded. Between 0 and 1 inclusive.")]
2023-02-27 15:28:05 +08:00
public float alphaValue;
[SerializeField]
2023-03-01 17:36:02 +08:00
private float fadeInTime;
2023-02-27 15:28:05 +08:00
[SerializeField]
2023-03-01 17:36:02 +08:00
private float fadeOutTime;
2023-02-27 15:28:05 +08:00
private Renderable fadeR;
2023-02-28 06:49:40 +08:00
public static SceneFadeInOut Instance { get; private set; }
2023-02-27 15:28:05 +08:00
protected override void awake()
{
2023-02-28 06:49:40 +08:00
if (Instance != null && Instance != this)
RemoveScript<SceneFadeInOut>();
else
Instance = this;
fadeR = GameObject.GetComponent<Renderable>();
fadeR.Material.SetProperty<float>("data.alpha", alphaValue);
2023-03-02 11:33:21 +08:00
GameObject.Parent.SetActive(false);
2023-02-28 06:49:40 +08:00
//alphaValue = fadeR.Material.GetProperty<float>("data.alpha");
2023-02-27 15:28:05 +08:00
}
protected override void start()
{
}
protected override void update()
{
if (fadeR != null)
{
if (fadeIn) //fading in
{
2023-02-28 06:49:40 +08:00
fadeOut = false;
2023-03-01 17:36:02 +08:00
if (fadeInTime == 0.0f)
{
alphaValue = 1.0f;
}
else
{
alphaValue += (1.0f / fadeInTime) * Time.DeltaTimeF;
}
2023-03-02 11:33:21 +08:00
if (alphaValue >= 1.0f)
{
2023-03-02 15:47:00 +08:00
alphaValue = 1.0f;
//GameObject.Parent.SetActive(false);
2023-03-02 11:33:21 +08:00
}
2023-02-27 15:28:05 +08:00
}
if (fadeOut) //fading out
{
2023-02-28 06:49:40 +08:00
fadeIn = false;
2023-03-01 17:36:02 +08:00
if (fadeOutTime == 0.0f)
{
alphaValue = 0.0f;
2023-03-02 11:33:21 +08:00
2023-03-01 17:36:02 +08:00
}
else
{
alphaValue -= (1.0f / fadeOutTime) * Time.DeltaTimeF;
}
2023-03-02 11:33:21 +08:00
if (alphaValue <= 0.0f)
{
2023-03-02 15:47:00 +08:00
alphaValue = 0.0f;
//This line is to be added so that (because the fade in and out item is usually the front-most UI element)
//the fade in and out UI element does not block functionality of other UI elements
GameObject.Parent.SetActive(false);
2023-03-02 11:33:21 +08:00
}
2023-02-27 15:28:05 +08:00
}
fadeR.Material.SetProperty<float>("data.alpha", alphaValue);
}
}
2023-02-28 06:49:40 +08:00
protected override void onDestroy()
{
if (Instance == this)
Instance = null;
}
2023-03-01 17:36:02 +08:00
public void CallFadeIn()
{
fadeIn = true;
fadeOut = false;
2023-03-02 11:33:21 +08:00
GameObject.Parent.SetActive(true);
2023-03-01 17:36:02 +08:00
}
public void CallFadeOut()
{
fadeOut = true;
fadeIn = false;
2023-03-02 11:33:21 +08:00
GameObject.Parent.SetActive(true);
2023-03-01 17:36:02 +08:00
}
public void CallFadeStop()
{
fadeOut = false;
fadeIn = false;
2023-03-02 11:33:21 +08:00
GameObject.Parent.SetActive(false);
2023-03-01 17:36:02 +08:00
}
public bool FadeOutFinished()
{
return (alphaValue <= 0.0f);
}
public bool FadeInFinished()
{
return (alphaValue >= 1.0f);
}
2023-02-27 15:28:05 +08:00
}
}