前言
每次有相关功能,都要找半天,怪麻烦的。
正文
这里实例Image,有时候是RawImage。
这样写 可以使触发和功能分开,低耦合。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SpritesChange : MonoBehaviour
{
[Header("每帧间隔时间(秒)")]
[Range(0.01f, 1)]
public float offset = 0.5f;
public AudioClip aClip;
//默认不循环,调试模式是循环状态
public bool isDebug;
//播放完成后,需要控制隐藏的物体,大多数带有特效和模型。
public GameObject[] gos_Content;
public Sprite[] sprites;
private Image image;
private AudioSource aSource;
private int currentId;
private int maxNum;
private Coroutine coroutine;
private void Awake()
{
image = GetComponent<Image>();
aSource = GetComponent<AudioSource>();
isDebug = false;
currentId = 0;
maxNum = sprites.Length;
}
private void OnEnable()
{
coroutine = StartCoroutine(PlaySprite());
}
private void OnDisable()
{
if (coroutine != null)
{
StopCoroutine(coroutine);
coroutine = null;
}
currentId = 0;
}
IEnumerator PlaySprite()
{
if (maxNum > 0)
{
if (aSource != null)
{
aSource.clip = aClip;
aSource.Play();
}
while (true)
{
image.sprite = sprites[currentId];
currentId++;
yield return new WaitForSeconds(offset);
if (currentId >= maxNum)
{
if (isDebug)
{
currentId = 0;
}
else
{
break;
}
}
}
}
if (gos_Content.Length > 0)
{
TransformHelper.SetActive(gos_Content, false);
}
coroutine = null;
}
}
Comments | NOTHING