Table of Contents

Invocation

Invocation in HyperTween allows you to execute specific code at key points during a tween's lifecycle, such as when it starts or stops. This feature is crucial for creating complex, reactive animations and game logic.

Action Invocation

The simplest method to run code when a tween plays or stops is to use InvokeActionOnPlay or InvokeActionOnStop. Here's an example:

using BreezeBlockGames.HyperTween.UnityShared.API;
using BreezeBlockGames.HyperTween.UnityShared.Modules.InvokeAction.API;
using BreezeBlockGames.HyperTween.UnityShared.Modules.LocalTransforms.API;
using BreezeBlockGames.HyperTween.UnityShared.Modules.Transforms.API;
using BreezeBlockGames.HyperTween.UnityShared.TweenBuilders;
using UnityEngine;

namespace HyperTween.Examples
{
    public class InvocationTweenExample : MonoBehaviour
    {
        void Start()
        {
            CreateTween(HyperTweenFactory.Get());
        }

        void CreateTween<T>(TweenFactory<T> tweenFactory) where T : unmanaged, ITweenBuilder
        {
            tweenFactory.CreateTween("InvocationTweenExample")
                .WithDuration(1f)
                .WithTransform(transform)
                .WithLocalPositionOutput(5f * Random.onUnitSphere)
                .WithEaseInOut()
                .InvokeActionOnPlay(_ =>
                {
                    Debug.Log("Tween played!");
                })
                .InvokeActionOnStop(context =>
                {
                    Debug.Log("Tween stopped!");
                    CreateTween(context.TweenFactory);
                })
                .Play();
        }
    }
}

Note that to create new tweens from the invoked action, you need to use a TweenFactory created from the EntityCommandBuffer passed to the action. This requires use of a different type of TweenFactory. This example shows how it's possible to write a single method that can work with any type of TweenFactory.

Caution

Because InvokeActionOnPlay and InvokeActionOnStop use managed delegates, code using it cannot be Burst compiled, limiting efficiency. See Extending Invocation for ways to get around this limitation.