Files
JJBB/Assets/Project/Script/RawImageAlpha.cs

63 lines
1.4 KiB
C#
Raw Normal View History

2024-08-23 15:49:34 +08:00
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class RawImageAlpha : MonoBehaviour {
public bool PlayOnAwake = false;
public RawImage m_RawImage;
public float delayTime = 1;
public float durTime = 1;
private float Startime = 0;
public float StartAlpha;
public float EndAlpha;
void Awake()
{
if (m_RawImage == null || m_RawImage.texture == null)
{
gameObject.SetActive(false);
return;
}
}
private void Start()
{
if(PlayOnAwake)
{
Startime = Time.time;
Update();
}
}
public void SetStart(float startAlpha,float endAlpha,float durtion = 1,float delay = 0)
{
delayTime = Time.time + delay;
durTime = durtion;
StartAlpha = startAlpha;
EndAlpha = endAlpha;
Startime = Time.time + delay;
Update();
}
void Update()
{
if (Startime <= 0 || m_RawImage==null)
return;
if(Time.time - delayTime < 0)
{
return;
}
float rate = (Time.time - Startime) / durTime;
if(rate > 1 || rate < 0)
{
Startime = 0;
return;
}
Color newColor = m_RawImage.color;
newColor.a = Mathf.Lerp(StartAlpha, EndAlpha, rate);
m_RawImage.color = newColor;
}
}