• RuntimesUnity
  • Control blinking animation

Hi, I’ve been using this script to make my character blink, but I was wondering if there’s a way to change the minimum and maximum delay values during runtime? I’m fairly new to coding, so any guidance or references would be really helpful. Thank you so much!

using Spine.Unity;
using System.Collections;
using UnityEngine;
namespace Spine.Unity.Examples
{
    public class BlinkPlayerSpine2D : MonoBehaviour
    {
        const int BlinkTrack = 3;
        public AnimationReferenceAsset blinkAnimation;
        public float minimumDelay = 3f;
        public float maximumDelay = 5f;
        IEnumerator Start()
        {
            SkeletonAnimation skeletonAnimation = GetComponent<SkeletonAnimation>();
            if (skeletonAnimation == null) yield break;
            while (true)
            {
                skeletonAnimation.AnimationState.SetAnimation(BlinkPlayerSpine2D.BlinkTrack, blinkAnimation, false);
                yield return new WaitForSeconds(Random.Range(minimumDelay, maximumDelay));
            }
        }
Related Discussions
...

To change the minimum and maximum delay values during runtime, you can add public methods to your BlinkPlayerSpine2D script that allow you to set these values. Here is an updated version of your script with the added methods:

using Spine.Unity;
using System.Collections;
using UnityEngine;

namespace Spine.Unity.Examples
{
    public class BlinkPlayerSpine2D : MonoBehaviour
    {
        const int BlinkTrack = 3;
        public AnimationReferenceAsset blinkAnimation;
        public float minimumDelay = 3f;
        public float maximumDelay = 5f;

        IEnumerator Start()
        {
            SkeletonAnimation skeletonAnimation = GetComponent<SkeletonAnimation>();
            if (skeletonAnimation == null) yield break;
            while (true)
            {
                skeletonAnimation.AnimationState.SetAnimation(BlinkPlayerSpine2D.BlinkTrack, blinkAnimation, false);
                yield return new WaitForSeconds(Random.Range(minimumDelay, maximumDelay));
            }
        }

        public void SetMinimumDelay(float newMinimumDelay)
        {
            minimumDelay = newMinimumDelay;
        }

        public void SetMaximumDelay(float newMaximumDelay)
        {
            maximumDelay = newMaximumDelay;
        }
    }
}

With these methods, you can now change the minimumDelay and maximumDelay values at runtime from other scripts or through the Unity Inspector. For example, you can call these methods from another script like this:

public class BlinkController : MonoBehaviour
{
    public BlinkPlayerSpine2D blinkPlayer;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            blinkPlayer.SetMinimumDelay(1f);
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            blinkPlayer.SetMaximumDelay(10f);
        }
    }
}

In this example, pressing the 1 key will set the minimum delay to 1 second, and pressing the 2 key will set the maximum delay to 10 seconds.

Thank you, Spinebot!