56 lines
1.8 KiB
C#
56 lines
1.8 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using ZXKFramework;
|
||
namespace YiLiao.JingMaiLiuZhiZhen
|
||
{
|
||
public class SpriteToUIImage : MonoBehaviour
|
||
{
|
||
public SpriteRenderer spriteRenderer;
|
||
public Image uiImage;
|
||
public Animator spriteAnimator; // 用于控制SpriteRenderer动画的Animator组件
|
||
Coroutine coroutine;
|
||
private bool isAnimationFinished = false;
|
||
private void OnEnable()
|
||
{
|
||
// 当GameObject被激活时,重置动画状态并开始播放
|
||
isAnimationFinished = false;
|
||
if (spriteAnimator != null)
|
||
{
|
||
spriteAnimator.Play(spriteAnimator.GetCurrentAnimatorClipInfo(0)[0].clip.name);
|
||
coroutine = Game.Instance?.IEnumeratorManager.Run(4f, () => {
|
||
isAnimationFinished = true;
|
||
gameObject.SetActive(false);
|
||
});
|
||
}
|
||
}
|
||
|
||
private void OnDisable()
|
||
{
|
||
// 当GameObject被禁用时,停止动画相关操作
|
||
isAnimationFinished = true;
|
||
Game.Instance?.IEnumeratorManager.Stop(coroutine);
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
if (spriteRenderer != null && uiImage != null && !isAnimationFinished)
|
||
{
|
||
uiImage.sprite = spriteRenderer.sprite;
|
||
//CheckAnimationState();
|
||
}
|
||
}
|
||
|
||
//void CheckAnimationState()
|
||
//{
|
||
// if (spriteAnimator != null)
|
||
// {
|
||
// AnimatorStateInfo currentState = spriteAnimator.GetCurrentAnimatorStateInfo(0);
|
||
// if (currentState.normalizedTime >= 1f && !currentState.loop)
|
||
// {
|
||
// isAnimationFinished = true;
|
||
// gameObject.SetActive(false);
|
||
// }
|
||
// }
|
||
//}
|
||
}
|
||
} |