public GameObject canvas;
private Transform logoParent;
private Transform Logo_logo; //logo一
private Transform Logo_qmtgTrans; //logo二
private Transform Logo_brand; //logo三
private float Timer; //计时器
private float time = 5f; //延迟的总时长
private float Alpha = 1f;
private float AlphaSpeed = 3f; //Alpha值渐变的时间,1就是对应渐变需要1秒,2就是2秒
DisPlayState disPlayState = DisPlayState.start;
public void Start(GameObject cav)
{
canvas = cav;
disPlayState = DisPlayState.start;
var logoParentPrafab = Resources.Load<Transform>("UIPrefabs/LogoParent");
logoParent = GameObject.Instantiate(logoParentPrafab);
logoParent.SetParent(canvas.transform, false);
Logo_logo = logoParent.transform.Find("Logo_logo");
Logo_qmtgTrans = logoParent.transform.Find("Logo_qmtg");
Logo_brand = logoParent.transform.Find("Logo_brand");
}
public void Update()
{
LogoDisPlay();
}
//Logo显示状态切换
private void LogoDisPlay()
{
if (disPlayState == DisPlayState.start)
{
Timer = time;
disPlayState = DisPlayState.delay;
}
if (disPlayState == DisPlayState.delay)
{
DelayTime(DisPlayState.minusAlpha);
}
if (disPlayState == DisPlayState.minusAlpha)
{
if (Logo_logo != null)
AlphaControl(true, DisPlayState.addAlpha, Logo_logo.gameObject);
}
if (disPlayState == DisPlayState.addAlpha)
{
if (Logo_brand != null)
AlphaControl(false, DisPlayState.delay2, Logo_brand.gameObject);
}
if (disPlayState == DisPlayState.delay2)
{
DelayTime(DisPlayState.minusAlpha2);
}
if (disPlayState == DisPlayState.minusAlpha2)
{
if (Logo_brand != null)
AlphaControl(true, DisPlayState.addAlpha2, Logo_brand.gameObject);
}
if (disPlayState == DisPlayState.addAlpha2)
{
if (Logo_qmtgTrans != null)
AlphaControl(false, DisPlayState.delay3, Logo_qmtgTrans.gameObject);
}
if (disPlayState == DisPlayState.delay3)
{
DelayTime(DisPlayState.minusAlpha3);
}
if (disPlayState == DisPlayState.minusAlpha3)
{
if (Logo_qmtgTrans != null)
AlphaControl(true, DisPlayState.addAlpha3, Logo_qmtgTrans.gameObject);
}
if (disPlayState == DisPlayState.addAlpha3)
{
if (Logo_logo != null)
AlphaControl(false, DisPlayState.start, Logo_logo.gameObject);
}
}
//控制Alpha值减小和增加
private void AlphaControl(bool minus, DisPlayState nextState, GameObject _logo)
{
if (minus)
{
Alpha -= Time.deltaTime * 1 / AlphaSpeed;
if (Alpha <= 0)
{
disPlayState = nextState;
//if (_logo.name == "Logo_logo")
//{
// Logo_logo.gameObject.SetActive(true);
// Logo_qmtgTrans.gameObject.SetActive(false);
// Logo_brand.gameObject.SetActive(false);
//}
//else if (_logo.name == "Logo_qmtg")
//{
// Logo_logo.gameObject.SetActive(false);
// Logo_qmtgTrans.gameObject.SetActive(true);
// Logo_brand.gameObject.SetActive(false);
//}
//else if (_logo.name == "Logo_brand")
//{
// Logo_logo.gameObject.SetActive(false);
// Logo_qmtgTrans.gameObject.SetActive(false);
// Logo_brand.gameObject.SetActive(true);
//}
//else
//{
// Debug.LogError("Logo消失了");
//}
}
_logo.GetComponent<RawImage>().color = new Color(1, 1, 1, Alpha);
}
else
{
Alpha += Time.deltaTime * 1 / AlphaSpeed;
if (Alpha >= 1)
{
disPlayState = nextState;
}
_logo.GetComponent<RawImage>().color = new Color(1, 1, 1, Alpha);
}
}
//延时并切换状态
private void DelayTime(DisPlayState _state)
{
Timer -= Time.deltaTime;
if (Timer <= 0)
{
disPlayState = _state;
Timer = time;
}
}
//显示Logo的各种状态枚举
private enum DisPlayState
{
start,
delay,
minusAlpha,
addAlpha,
delay2,
minusAlpha2,
addAlpha2,
delay3,
minusAlpha3,
addAlpha3,
end
}