Unity Button delay function

Sometimes Button down point is not called for an immediate response, but first there is a special short animation, and then react.

 

achieve:

Inheritance Button, and then rewrite OnPointerClick, to delay the use of coroutines.

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

public class MyButton : Button
{
    [Header("执行onClick的延迟时间")]
    public float delayTime = 1f;

    public override void OnPointerClick(PointerEventData eventData)
    {
        StartCoroutine(Click());
    }

    IEnumerator Click()
    {
        Debug.Log("动画...");
        yield return new WaitForSecondsRealtime(delayTime);
        onClick.Invoke();
    }
}

Guess you like

Origin www.cnblogs.com/Peng18233754457/p/11444954.html