Table of Contents

Sequencing

HyperTween allows combining tweens into sequences with arbitrarily complex structures. They are primarily composed of Serial and Parallel tweens which are created by appending tweens to SequenceFactories and finally calling Build() to create a tween which is interchangeable with any other tween.

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 UnityEngine;

namespace HyperTween.Examples
{
    public class SequenceTweenExample : MonoBehaviour
    {
        void Start()
        {
            var factory = HyperTweenFactory.Get();

            var serialTweenFactory = factory.Serial("Serial");
            
            serialTweenFactory
                .Append(factory.CreateTween("SequenceTweenExampleA")
                    .WithDuration(5f)
                    .WithTransform(transform)
                    .WithLocalPositionOutput(new Vector3(5f, 0f, 0f)))
                .Append(factory.CreateTween("SequenceTweenExampleB")
                    .WithDuration(0.001f)
                    .WithTransform(transform)
                    .WithLocalPositionOutput(new Vector3(-5f, 0f, 0f)));

            // Build creates a tween like any other, that could be added to another sequence
            var serialTween = serialTweenFactory
                .Build()
                .InvokeActionOnStop(_ => Debug.Log("Serial tween stopped..."));
            
            serialTween.Play();
        }
    }
    
}

Functional Tween Composition

Because the tweens created by SequenceFactories are just regular tweens, with different components, they can be added to other sequences. Like this it is possible to build up composite tweens with arbitrarily complex behaviour.

A very useful pattern to employ when working with such structures is to create methods that return TweenHandle, then combine them into sequences. This can help to simplify your code and allow you to reuse methods for the creation of tweens:

using System.Linq;
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 CompositionTweenExample : MonoBehaviour
    {
        private void Start()
        {
            var factory = HyperTweenFactory.Get();
            
            var transforms = Enumerable.Range(1, 3)
                .Select(_ => GameObject.CreatePrimitive(PrimitiveType.Cube).transform)
                .ToArray();

            // Create a serial sequence factory to add child tweens to
            var serial = factory.Serial("Serial");
            for (var i = 0; i < 3; i++)
            {
                var randomPositionsTween = CreateRandomPositionsTween(factory, transforms);
                
                serial.Append(randomPositionsTween);
            }

            // Builds the serial sequence factory so that it becomes a regular tween
            serial.Build()
                // Destroys all the transforms when the serial sequence stops
                .InvokeActionOnStop(_ =>
                {
                    foreach (var t in transforms)
                    {
                        Destroy(t.gameObject);
                    }
                })
                .Play();
        }

        private static TweenHandle CreateRandomPositionsTween<T>(TweenFactory<T> factory, Transform[] transforms) 
            where T : unmanaged, ITweenBuilder
        {
            // Creates a tween for each transform that moves it to a random position
            var tweens = transforms
                .Select(t => CreateRandomPositionTween(factory, t));

            // Creates a parallel sequence so that all the transforms move at the same time
            return factory.Parallel("Parallel")
                .Append(tweens)
                .Build();
        }
        
        private static TweenHandle CreateRandomPositionTween<T>(TweenFactory<T> factory, Transform t) 
            where T : unmanaged, ITweenBuilder
        {
            return factory.CreateTween("MoveTransform")
                .WithDuration(1f)
                .WithTransform(t)
                .WithLocalPositionOutput(Random.onUnitSphere);
        }
    }
}