首页 > 解决方案 > Unity - Button OnClick 仅在我单击另一个按钮后执行动画

问题描述

我有这 3 个按钮

在此处输入图像描述

当我按下点击按钮时,它会播放一个动画,并且可交互按钮设置为关闭一段时间,并显示“bt.interactable = false;”。在那个时间段之后,它被设置为 true。

在此处输入图像描述

在那之后它让我再次玩。

但是,动画的第二次播放仅在我再次播放“点击按钮”之前播放其他按钮之一时才有效。这是带有按钮计时器的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ldbutton : MonoBehaviour
{

    public Image loading;
    public Button bt;
    int sec;
    string pl1_name = "Unicorn";
    public int counterclick;
    bool buttonLocked;
    Firebase_Bridge FB = new Firebase_Bridge();

    int tmp_sec;
    int totalSeconds = 0;
    int TOTAL_SECONDS = 0;

    async void Start()
    {

        if (bt.name.Equals("SpecialHitButton")){
            string time = await FB.LoadData_player_timeplsuperhit(pl1_name);
            sec = int.Parse(time);
        }
        if (bt.name.Equals("HitButton"))
        {
            string time = await FB.LoadData_player_timeplhit(pl1_name);
            sec = int.Parse(time);
        }
        if (bt.name.Equals("DefendButton"))
        {
            string time = await FB.LoadData_player_timepldefend(pl1_name);
            sec = int.Parse(time);
        }
        Debug.Log("star");
        tmp_sec = sec;

        bt.onClick.AddListener(() =>
        {
            loading.fillAmount = 1.0f;

            bt.interactable = false;

            if (counterclick < 2)
            {
                bt.interactable = false;

                sec = tmp_sec;
                if (sec > 0)
                {
                    bt.interactable = false;

                    totalSeconds += sec;
                }
                TOTAL_SECONDS = totalSeconds;
                StartCoroutine(second());
            }
        }
        );
} 
    IEnumerator second()
    {
        bt.interactable = false;

        Debug.Log("second");

        yield return new WaitForSeconds(1f);
        if (sec > 0)
        {
            sec--;

            fillLoading();
            StartCoroutine(second());

        }
        if (sec == 0)
        {
            counterclick = 0;
            totalSeconds = 0;
            TOTAL_SECONDS = 0;
            //sec = 0;
            StopCoroutine(second());

            Debug.Log("termina");

            yield return new WaitForSeconds(1f);
            StartCoroutine(wait());


        }
    }
    IEnumerator wait ()
    {
        bt.interactable = true;
        bt.enabled=true;
        return null;

    }

    void fillLoading()
    {
        Debug.Log("fillLoading");

        totalSeconds--;
        float fill = (float)totalSeconds / tmp_sec;
        loading.fillAmount = fill;
        Debug.Log("update");


    }
}

这是我的动画师: 在此处输入图像描述

有谁知道发生了什么?

标签: unity3dbuttonvuforia

解决方案


推荐阅读