150 lines
4.6 KiB
C#
Raw Normal View History

2026-06-08 08:55:10 +08:00
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System;
namespace Kill.UI.Components
{
public class PlayFlash : MonoBehaviour
{
public bool testPlay;
public bool isplayFlash = false;
public float fps = 25.0f;
public Sprite[] flashTextures;
public bool loop;
public bool playonce;
public bool useDelay;
public bool useTimeControl = false;
public float PlayTime;
public bool hideWhenOver = false;
public GameObject[] overHideObjs;
private float hasplaytime = 0;
private float time = 0;
private int currentIndex = 0;
public bool isplaying = false;
public Sprite defaultSprite = null;
Action callback = null;
private void Start()
{
if (testPlay)
Play();
}
void FixedUpdate()
{
if (isplaying)
{
time += Time.deltaTime;
hasplaytime += Time.deltaTime;
if (isplayFlash)
{
gameObject.GetComponent<Image>().sprite = flashTextures[currentIndex];
if (time >= 1.0f / fps)
{
time = 0;
currentIndex++;
if (currentIndex >= flashTextures.Length)
{
if (!loop)
{
isplaying = false;
currentIndex = 0;
if (playonce)
{
isplayFlash = false;
}
if (hideWhenOver)
{
if (overHideObjs != null && overHideObjs.Length > 0)
{
foreach (GameObject g in overHideObjs)
g.SetActive(false);
}
else
{
gameObject.GetComponent<Image>().enabled = false;
}
}
if (defaultSprite != null)
{
gameObject.GetComponent<Image>().sprite = defaultSprite;
}
callback?.Invoke();
}
else
currentIndex = 0;
}
}
}
if (useTimeControl && hasplaytime >= PlayTime)
{
isplaying = false;
hasplaytime = 0;
gameObject.GetComponent<Image>().enabled = false;
}
}
}
bool hasdelay = false;
public void Play(Action cb = null)
{
gameObject.SetActive(true);
gameObject.GetComponent<Image>().enabled = true;
callback = cb;
if (useDelay && !hasdelay)
{
StartCoroutine(PlayDelay());
return;
}
hasdelay = false;
isplaying = true;
//Debug.Log("Play");
}
public void Stop()
{
// Debug.Log("stopPlay");
if (isplayFlash)
{
//gameObject.GetComponent<Image>().enabled=false;
isplaying = false;
//currentIndex = 0;
//gameObject.GetComponent<Image>().sprite=flashTextures[0];
}
if (hideWhenOver)
{
if (overHideObjs != null && overHideObjs.Length > 0)
{
foreach (GameObject g in overHideObjs)
g.SetActive(false);
}
else
{
gameObject.SetActive(false);
}
}
if (defaultSprite != null)
{
gameObject.GetComponent<Image>().sprite = defaultSprite;
}
//gameObject.SetActive = false;
}
public IEnumerator PlayDelay()
{
yield return new WaitForSeconds(PlayTime);
hasdelay = true;
Play();
}
}
}