### Advanced Examples Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Examples demonstrating advanced usage of the Primetween library. ```APIDOC ## Advanced Examples Besides the many different types of Tweens and Tween Options, Tweens also provides a wide range of features that can be used to create advanced animations. The following sections will show you how to implemented some of these features to create advanced animation logic. ### Tweening Custom Values The following example shows how to create a custom Tween that can be used to animate a value of an enemy Component. The Tween will animate the value of the Component from the current value to the new value. ```csharp var enemy = GetComponent(); var tween = new FloatTween { from = enemy.health, to = 23, duration = 1, easeType = EaseType.SineOut, onUpdate = (_, value) => enemy.health = value, }; enemy.gameObject.AddTween(tween); ``` ``` -------------------------------- ### Migrating DOTween From() to PrimeTween Constructor Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Demonstrates how to set the starting value for an animation in PrimeTween, mapping DOTween's From() method. ```csharp transform.DOMoveX(to, 1).From(from) --> Tween.PositionX(transform, from, to, 1) ``` -------------------------------- ### Install Unity Tweens via OpenUPM Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Use the OpenUPM Package manager's Command Line Tool to install the latest stable release of the Unity Tweens module. ```sh openupm add nl.jeffreylanters.tweens ``` -------------------------------- ### Install PrimeTween via Unity Package Manager Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Modify your Packages/manifest.json file to include PrimeTween from a scoped registry. ```json { "dependencies": { "com.kyrylokuzyk.primetween": "1.4.6", ... }, "scopedRegistries": [ { "name": "npm", "url": "https://registry.npmjs.org/", "scopes": [ "com.kyrylokuzyk" ] } ] } ``` -------------------------------- ### Assign OnStart Delegate Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Assigns a callback to the onStart delegate to log a message when the tween starts. This requires an instance of ExampleTween. ```csharp var tween = new ExampleTween { onStart = (instance) => { Debug.Log("Tween has started"); }, }; ``` -------------------------------- ### Define OnStart Delegate Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Defines the delegate invoked when a Tween begins execution. No specific setup is needed beyond the delegate definition. ```csharp OnStartDelegate onStart; ``` -------------------------------- ### Migrating DOTween Delay and OnStart to PrimeTween Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Shows how to chain delays and callbacks to the start of tweens and sequences in PrimeTween, comparing DOTween's SetDelay and OnStart. ```csharp tween.SetDelay(1f).OnStart(callback) --> Tween.Delay(1, callback).Chain(tween) sequence.OnStart(callback) --> sequence.ChainCallback(callback) // at the beginning of the sequence ``` -------------------------------- ### Install Unity Tweens via Unity Package Manager Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Add this line to your project's manifest.json file to install the latest stable release using the Unity Package Manager. ```json "nl.jeffreylanters.tweens": "git+https://github.com/jeffreylanters/unity-tweens" ``` -------------------------------- ### Define Tween Start Value Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Set the 'from' value to explicitly define the starting value of a tween. If not set, the tween will automatically use the current property value as its starting point. ```csharp DataType from; var tween = new ExampleTween { from = new Vector3(10, 5, 20), }; ``` -------------------------------- ### DOTween Window Animation Example Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Demonstrates DOTween's approach to managing reusable tweens with SetAutoKill(false), SetLink, and manual Kill calls. This pattern can lead to resource waste. ```csharp public class DOTweenWindow : MonoBehaviour { // Disable auto-kill and store tween reference to reuse the tween later. // Disabling auto-kill wastes resources: even when the tween is not running, it still receives an update every frame and consumes memory. Tween tween; void Awake() { tween = transform.DOLocalMove(Vector3.zero, 1) .ChangeStartValue(new Vector3(0, -500)) .SetEase(Ease.InOutSine) .SetAutoKill(false) // Option 1: link the tween to this GameObject, so the tween is killed when the GameObject is destroyed .SetLink(gameObject) // Paused tweens still receive updates every frame and consume resources .Pause(); } public void SetWindowOpened(bool isOpened) { if (isOpened) { tween.PlayForward(); } else { tween.PlayBackwards(); } } void OnDestroy() { // Option 2: kill the tween before destroying an object. tween.Kill(); // Option 3: enable 'Safe Mode' and don't use SetLink() and don't kill the tween in OnDestroy(). // BUT: // - 'Safe Mode' will silence other potential errors or exceptions // - 'Safe Mode' doesn't work on WebGL and with 'Fast and no exceptions' on iOS } } ``` -------------------------------- ### PrimeTween Window Animation Example Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Shows PrimeTween's non-reusable tween approach. Animations are created directly when needed, offering better performance and simpler lifecycle management. ```csharp public class PrimeTweenWindow : MonoBehaviour { public void SetWindowOpened(bool isOpened) { Tween.LocalPosition(transform, isOpened ? Vector3.zero : new Vector3(0, -500), 1, Ease.InOutSine); } } ``` -------------------------------- ### Play Animation Forward and Backward Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Demonstrates how to play an animation forward and backward by starting new tweens in the desired direction. New tweens seamlessly overwrite previous ones on the same target. ```csharp [SerializeField] RectTransform window; public void SetWindowOpened(bool isOpened) { Tween.UIAnchoredPositionY(window, endValue: isOpened ? 0 : -500, duration: 0.5f); } ``` -------------------------------- ### PrimeTween Alternatives for Specific DOTween Features Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Provides links to discussions or examples for specific DOTween features that have different implementations or are experimental in PrimeTween, such as DOText, DOJump, DOPath, DOLookAt, and DOBlendable. ```csharp textMeshPro.DOText(...) --> discussions.unity.com/t/926420/159 --> or see TypewriterAnimatorExample.cs in Demo text.DOCounter() --> discussions.unity.com/t/926420/80 transform.DOJump() --> discussions.unity.com/t/926420/4 transform.DOPath() --> discussions.unity.com/t/926420/158 transform.DOLookAt() --> discussions.unity.com/t/926420/189 tween.SetId() --> github.com/KyryloKuzyk/PrimeTween/discussions/26#discussioncomment-7700985 target.DOBlendable___(...) --> Tween.___Additive(target, ...) // experimental --> github.com/KyryloKuzyk/PrimeTween/discussions/55 ``` -------------------------------- ### Set Tween Offset Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Define the starting time for the tween. If not set, the tween starts from the beginning. ```csharp float offset; ``` ```csharp var tween = new ExampleTween { offset = 5, }; ``` -------------------------------- ### Create Cyclic Sequences Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Apply cycles to a Sequence using Sequence.Create() with 'cycles' and 'cycleMode' parameters. This example chains two tweens with Yoyo cycle mode. ```csharp Sequence.Create(cycles: 2, CycleMode.Yoyo) .Chain(Tween.PositionX(transform, 10, duration)) .Chain(Tween.PositionY(transform, 20, duration)); ``` -------------------------------- ### Define OnAdd Delegate Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Defines the delegate invoked when a Tween is added to a GameObject. No setup required. ```csharp OnAddDelegate onAdd; ``` -------------------------------- ### Utilize Parametric Easing Functions Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Customize standard easing types using parametric functions. Examples include BounceExact for specific bounce amplitudes or Overshoot/Bounce for adjustable strength. ```csharp // Regardless of the current position and endValue, the bounce will have the exact amplitude of 1 meter Tween.PositionY(transform, endValue, duration, Easing.BounceExact(1)); ``` -------------------------------- ### Set Fill Mode Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Define how the tween behaves before starting and after ending. Defaults to 'Backward'. Options are None, Forward, Backward, and Both. ```csharp FillMode fillMode; ``` ```csharp var tween = new ExampleTween { fillMode = FillMode.Both, }; ``` -------------------------------- ### Set Tween Delay Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Set the delay in seconds before the tween starts. The fill mode can affect behavior before the tween begins. ```csharp float delay; ``` ```csharp var tween = new ExampleTween { delay = 5, }; ``` -------------------------------- ### Define OnEnd Delegate Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Defines the delegate invoked when the Tween completes its execution normally. No special setup is required. ```csharp OnEndDelegate onEnd; ``` -------------------------------- ### Create and Add PositionTween to GameObject Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Create a new PositionTween to move a GameObject from its current position to a new target position over a specified duration. Add the configured tween to a GameObject to start the animation. ```csharp var tween = new PositionTween { to = new Vector3(10, 5, 20), duration = 5, }; gameobject.AddTween(tween); ``` -------------------------------- ### Animate Transform Euler Angles Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Animate the Euler angles of a transform from a start value to an end value over a duration. Requires the PrimeTween namespace. ```csharp // Rotate 'transform' around the y-axis by 360 degrees in 1 second Tween.EulerAngles(transform, startValue: Vector3.zero, endValue: new Vector3(0, 360), duration: 1); ``` -------------------------------- ### Migrating DOTween Ease Settings to PrimeTween Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Demonstrates how to set easing for tweens and sequences in PrimeTween, corresponding to DOTween's SetEase method. ```csharp tween.SetEase(Ease.InOutSine) --> Tween.Position(..., ease: Ease.InOutSine); sequence.SetEase(Ease.OutBounce) --> Sequence.Create(..., sequenceEase: Ease.OutBounce) ``` -------------------------------- ### Migrating DOTween Sequence Creation to PrimeTween Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Compares the creation and manipulation of sequences in DOTween and PrimeTween, including joining, appending, and inserting elements. ```csharp DOTween.Sequence() --> Sequence.Create() sequence.Join() --> sequence.Group() sequence.Append() --> sequence.Chain() sequence.AppendCallback() --> sequence.ChainCallback() seq.Insert(1.5f, trans.DOMoveX(...)) --> seq.Insert(1.5f, Tween.PositionX(trans, ...)) --> // or `seq.Group(Tween.PositionX(..., startDelay: 1.5f))` before the first Chain() operation seq.InsertCallback(1f, callback)) --> seq.InsertCallback(1f, callback)) ``` -------------------------------- ### Migrating DOTween Update Settings to PrimeTween Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Illustrates how to configure tweens and sequences to use unscaled time or fixed update in PrimeTween, corresponding to DOTween's SetUpdate. ```csharp tween.SetUpdate(true) --> Tween.Position(..., useUnscaledTime: true) sequence.SetUpdate(true) --> Sequence.Create(..., useUnscaledTime: true) tween.SetUpdate(UpdateType.Fixed) --> Tween.Position(..., new TweenSettings(1f, updateType: UpdateType.Fixed)) sequence.SetUpdate(UpdateType.Fixed) --> Sequence.Create(updateType: UpdateType.Fixed) ``` -------------------------------- ### Migrating DOTween Loop Settings to PrimeTween Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Shows the PrimeTween equivalents for setting loops and loop types, comparing DOTween's SetLoops with PrimeTween's cycles and CycleMode. ```csharp tween.SetLoops(2, LoopType.Yoyo) --> Tween.Position(..., cycles: 2, CycleMode.Rewind) // Yoyo in DOTween works like Rewind sequence.SetLoops(2, LoopType.Yoyo) --> Sequence.Create(cycles: 2, Sequence.SequenceCycleMode.Rewind) ``` -------------------------------- ### Migrating DOTween Virtual Calls to PrimeTween Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Shows how to create delayed calls and custom tweens in PrimeTween, mapping DOTween's DOVirtual and DOTween.To. ```csharp DOVirtual.DelayedCall() --> Tween.Delay() DOTween.To() --> Tween.Custom() DOVirtual.Vector3() --> Tween.Custom() ``` -------------------------------- ### Group and Chain Animations in a Sequence Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Use Sequence.Create() to group tweens that run in parallel using .Group(), and chain tweens that run sequentially using .Chain(). Insert animations at specific times with .Insert(). ```csharp Sequence.Create(cycles: 10, CycleMode.Yoyo) // PositionX and Scale tweens are 'grouped', so they will run in parallel .Group(Tween.PositionX(transform, endValue: 10f, duration: 1.5f)) .Group(Tween.Scale(transform, endValue: 2f, duration: 0.5f, startDelay: 1)) // Rotation tween is 'chained' so it will start when both previous tweens are finished (after 1.5 seconds) .Chain(Tween.Rotation(transform, endValue: new Vector3(0f, 0f, 45f), duration: 1f)) .ChainDelay(1) .ChainCallback(() => Debug.Log("Sequence cycle completed")) // Insert color animation at time of '0.5' seconds // Inserted animations overlap with other animations in the sequence .Insert(atTime: 0.5f, Tween.Color(image, Color.red, duration: 0.5f)); ``` -------------------------------- ### Migrating DOTween Yield Instructions to PrimeTween Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Compares the usage of yield instructions for waiting for tween completion in DOTween and PrimeTween, including async methods. ```csharp yield return tween.WaitForCompletion() --> yield return tween.ToYieldInstruction() yield return sequence.WaitForCompletion() --> yield return sequence.ToYieldInstruction() // PrimeTween doesn't use threads, so you can write async methods even on WebGL await tween.AsyncWaitForCompletion() --> await tween await sequence.AsyncWaitForCompletion() --> await sequence ``` -------------------------------- ### Run Primtween Animations in Coroutines Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Use `.ToYieldInstruction()` to yield control back to the coroutine until the animation completes. This is useful for creating animation sequences. ```csharp IEnumerator Coroutine() { Tween.PositionX(transform, endValue: 10f, duration: 1.5f); yield return Tween.Scale(transform, 2f, 0.5f, startDelay: 1).ToYieldInstruction(); yield return Tween.Rotation(transform, new Vector3(0f, 0f, 45f), 1f).ToYieldInstruction(); // Non-allocating alternative to 'yield return new WaitForSeconds(1f)' yield return Tween.Delay(1).ToYieldInstruction(); Debug.Log("Sequence completed"); } ``` -------------------------------- ### Migrating DOTween Animations to PrimeTween Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md This snippet shows the direct mapping of common DOTween animation calls to their PrimeTween equivalents. It covers basic movement, fading, color changes, and shaking. ```csharp DOTween API on the left --> PrimeTween API on the right // All animations are supported, here are only a few of them as an example transform.DOMove(...) --> Tween.Position(transform, ...) uiImage.DOFade(...) --> Tween.Alpha(uiImage, ...) material.DOColor(...) --> Tween.Color(material, ...) transform.DOShakePosition(...) --> Tween.ShakeLocalPosition(transform, ...) ``` -------------------------------- ### Migrating DOTween Syntax to PrimeTween Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Replace DOTween using statements and adapt tween management. PrimeTween simplifies null checking and tween lifecycle management. ```csharp // using DG.Tweening; using PrimeTween; // if (tween != null) { // tween.Kill(complete: true); // tween = null; // } tween.Complete(); // null checking and setting tween to null is not needed // tween/sequence.PlayForward/PlayBackwards/Rewind/Restart(); // In PrimeTween, tweens and sequences are non-reusable, so there is no direct equivalent. // Instead, start a new animation in the desired direction (see the example below). // Starting new animations in PrimeTween is extremely fast, so there is no need for caching. ``` -------------------------------- ### Migrating DOTween Speed-Based Animations to PrimeTween Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Illustrates how to create speed-based animations in PrimeTween, corresponding to DOTween's SetSpeedBased() modifier. ```csharp trans.DOMove(pos, speed).SetSpeedBased() --> Tween.PositionAtSpeed(trans, pos, speed) ``` -------------------------------- ### Shake Camera Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Shake the camera with specified strength, duration, and frequency. Requires the PrimeTween namespace. ```csharp // Shake the camera with medium strength (0.5f) Tween.ShakeCamera(camera, strengthFactor: 0.5f); // Shake the camera with heavy strength (1.0f) for a duration of 0.5f seconds and a frequency of 10 shakes per second Tween.ShakeCamera(camera, strengthFactor: 1.0f, duration: 0.5f, frequency: 10); ``` -------------------------------- ### Assign OnAdd Delegate Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Assigns a callback to the onAdd delegate to log a message when the tween is added. Ensure the ExampleTween class is correctly defined. ```csharp var tween = new ExampleTween { onAdd = (instance) => { Debug.Log("Tween has been added"); }, }; ``` -------------------------------- ### Window Animation with Inspector Integration Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md This C# snippet demonstrates animating a UI element's position using tween settings that can be configured in the Unity Inspector. The WithDirection helper method simplifies selecting target values. ```csharp [SerializeField] RectTransform window; [SerializeField] TweenSettings windowAnimationSettings; public void SetWindowOpened(bool isOpened) { Tween.UIAnchoredPositionY(window, windowAnimationSettings.WithDirection(toEndValue: isOpened)); } ``` -------------------------------- ### Migrating Global DOTween Kill to PrimeTween Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Shows how to stop or complete all tweens associated with a specific target object in PrimeTween, corresponding to DOTween.Kill(target). ```csharp DOTween.Kill(target, false) --> Tween.StopAll(onTarget: target) DOTween.Kill(target, true) --> Tween.CompleteAll(onTarget: target) ``` -------------------------------- ### Migrating DOTween Kill Operations to PrimeTween Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Compares DOTween's Kill method for stopping or completing tweens with PrimeTween's Stop and Complete methods. ```csharp tween.Kill(false) --> tween.Stop() tween.Kill(true) --> tween.Complete() ``` -------------------------------- ### Await Animations with Async/Await Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Use async/await to chain animations without callbacks. This method is suitable for preventing callback hell and works across platforms, including WebGL. ```csharp async void AsyncMethod() { Tween.PositionX(transform, endValue: 10f, duration: 1.5f); await Tween.Scale(transform, endValue: 2f, duration: 0.5f, startDelay: 1); await Tween.Rotation(transform, endValue: new Vector3(0f, 0f, 45f), duration: 1f); // Non-allocating alternative to 'await Task.Delay(1000)' that doesn't use 'System.Threading' // Animations can be awaited on all platforms, even on WebGL await Tween.Delay(1); Debug.Log("Sequence completed"); } ``` -------------------------------- ### Execute Code on Tween Completion Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Use .OnComplete() to run custom code when an animation finishes. Be mindful of potential memory allocations from closures. ```csharp // Call SomeMethod() when the animation completes Tween.Position(transform, endValue: new Vector3(10, 0), duration: 1) .OnComplete(() => SomeMethod()); // After the animation completes, wait for 0.5 seconds, then destroy the GameObject Tween.Scale(transform, endValue: 0, duration: 1, endDelay: 0.5f) .OnComplete(() => Destroy(gameObject)); ``` -------------------------------- ### Tween Instances Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Methods and properties for controlling active tween instances. ```APIDOC ## Tween Instances When a Tween is added to a GameObject, an Instance will be returned. This is where the Tween will be running. The Instance can be used to control the Tween, for example to pause, resume or cancel the Tween. ### Cancel The `Cancel` method will cancel the Tween. When the Tween is cancelled, the [On Cancel](#on-cancel) and [On Finally](#on-finally) delegates will be invoked. ```csharp void Cancel(); ``` **Example Usage:** ```csharp var tween = new ExampleTween { }; var instance = gameObject.AddTween(tween); instance.Cancel(); ``` ### Is Paused The `isPaused` property will return whether the Tween is paused while also allowing you to pause the Tween. ```csharp bool isPaused; ``` **Example Usage:** ```csharp var tween = new ExampleTween { }; var instance = gameObject.AddTween(tween); instance.isPaused = true; ``` ### Target The `target` property defines the target GameObject on which the Tween is running. ```csharp readonly GameObject target; ``` **Example Usage:** ```csharp var tween = new ExampleTween { }; var instance = gameObject.AddTween(tween); Debug.Log(instance.target); ``` ``` -------------------------------- ### Repeat Animations with Cycles Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Apply cycles to animations using the 'cycles' and 'cycleMode' parameters. Setting cycles to -1 repeats indefinitely. CycleMode.Yoyo animates forth and back. ```csharp Tween.PositionY(transform, endValue: 10, duration: 0.5f, cycles: 2, cycleMode: CycleMode.Yoyo); ``` -------------------------------- ### Enable Ping Pong Animation Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Set to true to make the tween play backwards after completing. This affects how loops are counted. ```csharp bool usePingPong; ``` ```csharp var tween = new ExampleTween { usePingPong = true, }; ``` -------------------------------- ### Use Serialized Tween Settings Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Configure animation properties like startValue, endValue, duration, and ease directly in the Inspector using serialized TweenSettings. This allows for runtime tweaking without code changes. ```csharp // Tweak all animation properties from the Inspector: // startValue, endValue, duration, ease (or custom ease curve), etc. [SerializeField] TweenSettings yPositionTweenSettings; // Then pass tween settings to the animation method Tween.PositionY(transform, yPositionTweenSettings); [SerializeField] TweenSettings rotationTweenSettings; Tween.Rotation(transform, rotationTweenSettings); [SerializeField] TweenSettings eulerAnglesTweenSettings; Tween.EulerAngles(transform, eulerAnglesTweenSettings); [SerializeField] ShakeSettings cameraShakeSettings; Tween.ShakeLocalPosition(Camera.main.transform, cameraShakeSettings); ``` -------------------------------- ### Create Speed-Based Animations Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Animate based on speed rather than duration using AtSpeed methods. PrimeTween calculates duration using distance/speed. To chain these tweens, explicitly set the startValue for subsequent tweens. ```csharp Tween.LocalPositionAtSpeed(transform, endValue: midPos, speed) // Set 'startValue: midPos' to continue the movement from the 'midPos' instead of the initial 'transform.position' .Chain(Tween.LocalPositionAtSpeed(transform, startValue: midPos, endValue: endPos, speed)); ``` -------------------------------- ### Control Running Tweens Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Manage active tweens using properties like isAlive, Stop(), Complete(), isPaused, elapsedTime, progress, and timeScale. This allows for fine-grained control over animation playback. ```csharp Tween tween = Tween.LocalPositionX(transform, endValue: 1.5f, duration: 1f); // ... if (tween.isAlive) { // '.isAlive' means the tween was created and not completed (or manually stopped) yet. // While the tween '.isAlive' you can access its properties such as duration, // elapsedTime, progress, interpolationFactor, etc. Debug.Log($"Animation is still running, elapsed time: {tween.elapsedTime}."); } tween.Stop(); // Interrupt the tween, leaving the animated value at the current value Tween.StopAll(onTarget: transform); // Alternative way to stop the tween by its target tween.Complete(); // Instantly complete the running tween and set the animated value to the endValue Tween.CompleteAll(onTarget: transform); // Alternative way to complete the tween by its target tween.isPaused = true; // Pause the tween Tween.PausedAll(true, onTarget: transform); // Alternative way to pause the tween by its target tween.elapsedTime = 0.5f; // Manually set the elapsed time of the tween. Use 'elapsedTimeTotal' to set the elapsed time of all cycles. tween.progress = 0.5f; // Manually set the normalized progress of the current cycle. This property is similar to the 'normalizedTime' property of the Animation component. tween.timeScale = 2f; // Apply a custom timeScale to the tween ``` -------------------------------- ### Control Tween Instance Lifecycle Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md When a tween is added to a GameObject, an instance is returned that allows for control over the tween's lifecycle. Use the instance to pause, resume, or cancel the tween's execution. ```csharp var tween = new PositionTween { }; var instance = gameObject.AddTween(tween); instance.Cancel(); ``` -------------------------------- ### Custom Tween with Inspector Settings Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md This C# snippet demonstrates animating a float value using Tween.Custom() with serialized TweenSettings. This allows animation parameters to be adjusted directly within the Unity Inspector. ```csharp [SerializeField] TweenSettings tweenSettings; float floatField; Tween.Custom(tweenSettings, onValueChange: newVal => floatField = newVal); ``` -------------------------------- ### Assign OnFinally Delegate Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Assigns a callback to the onFinally delegate to log a message when the tween concludes, regardless of whether it completed or was cancelled. This guarantees execution. ```csharp var tween = new ExampleTween { onFinally = (instance) => { Debug.Log("Tween has ended or has been cancelled"); }, }; ``` -------------------------------- ### Non-allocating Delay and Custom Tweens Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Use the target parameter for non-allocating callbacks with Delay and Custom tweens. ```csharp Tween.Delay(this, duration: 1f, target => target.SomeMethod()); ``` ```csharp Tween.Custom(this, 0, 10, duration: 1, (target, newVal) => target.floatField = newVal); ``` ```csharp var shakeSettings = new ShakeSettings(frequency: 10, strength: Vector3.one, duration: 1); Tween.ShakeCustom(this, startValue: vector3Field, shakeSettings, (target, val) => target.vector3Field = val); ``` -------------------------------- ### Assign OnUpdate Delegate Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Assigns a callback to the onUpdate delegate to log a message and potentially update a value during the tween's progress. The callback receives the instance and the current value. ```csharp var tween = new ExampleTween { onUpdate = (instance, value) => { Debug.Log("Tween has updated"); }, }; ``` -------------------------------- ### Enable Infinite Looping Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Set to true to make the tween loop forever. This overrides the 'Loops' setting. ```csharp bool isInfinite; ``` ```csharp var tween = new ExampleTween { isInfinite = true, }; ``` -------------------------------- ### Define OnUpdate Delegate Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Defines the delegate invoked during each update cycle of the Tween. It receives the tween instance and the current value. ```csharp OnUpdateDelegate onUpdate; ``` -------------------------------- ### Set Tween Loops Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Define the number of times a tween will repeat. For ping pong loops, forward and backward play counts as one loop. Infinite loops ignore this setting. ```csharp int loops; ``` ```csharp var tween = new ExampleTween { loops = 5, }; ``` -------------------------------- ### Set Ping Pong Interval Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Define the delay in seconds before playing the animation backwards after it has finished. If not set, it plays instantly. ```csharp float pingPongInterval; ``` ```csharp var tween = new ExampleTween { pingPongInterval = 5, }; ``` -------------------------------- ### Tween Extensions Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Extension methods for managing tweens on GameObjects. ```APIDOC ## Extensions Tweens also provides extension methods that can be used to control the Tween module. ### Add Tween The `AddTween` method will add a new Tween to the target GameObject. When the Tween is added, an Instance will be returned. This is where the Tween will be running. The Instance can be used to control the Tween, for example to pause, resume or cancel the Tween. ```csharp TweenInstance AddTween(this GameObject target, Tween tween) where ComponentType : Component; ``` **Example Usage:** ```csharp var tween = new ExampleTween { }; var instance = gameObject.AddTween(tween); ``` ### Cancel Tweens The `CancelTweens` method will cancel all Tweens on the target GameObject. When a Tween is cancelled, the [On Cancel](#on-cancel) and [On Finally](#on-finally) delegates will be invoked. When the `includeChildren` option is set, all Tweens on the children of the target GameObject will also be cancelled, otherwise only the Tweens on the target GameObject will be cancelled. ```csharp void CancelTweens(this GameObject target, bool includeChildren = false); ``` **Example Usage:** ```csharp gameObject.CancelTweens(); ``` ``` -------------------------------- ### Create Allocation-Free Delays Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Use Tween.Delay() to create delays that behave like tweens and can be used with sequences, coroutines, and async/await methods without memory allocation. ```csharp Tween.Delay(duration: 1f, () => Debug.Log("Delay completed")); ``` -------------------------------- ### Define OnFinally Delegate Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Defines the delegate invoked when a Tween finishes, either by completion or cancellation. This is ideal for final cleanup or logging. ```csharp OnFinallyDelegate onFinally; ``` -------------------------------- ### Shake Local Rotation Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Shake the local rotation of a transform with a specified amplitude (Euler angles), duration, and frequency. Requires the PrimeTween namespace. ```csharp // Shake the z-axis rotation with an amplitude of 15 degrees Tween.ShakeLocalRotation(transform, strength: new Vector3(0, 0, 15), duration: 1, frequency: 10); ``` -------------------------------- ### Configure Tween Update Types (LateUpdate/FixedUpdate) Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Control which Unity event function updates the animation using the 'updateType' parameter. Options include Update, LateUpdate, and FixedUpdate. This can be set via TweenSettings or directly in Sequence.Create. ```csharp // Use TweenSettings or TweenSettings struct to pass the 'updateType' parameter to static 'Tween.' methods Tween.PositionX(transform, endValue: 10f, new TweenSettings(duration: 1f, updateType: UpdateType.LateUpdate)); var tweenSettingsFloat = new TweenSettings(endValue: 10f, duration: 1f, updateType: UpdateType.FixedUpdate); Tween.PositionX(transform, tweenSettingsFloat); // To update the Sequence in FixedUpdate(), pass the 'updateType' parameter to Sequence.Create() Sequence.Create(updateType: UpdateType.FixedUpdate); ``` -------------------------------- ### Assign OnCancel Delegate Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Assigns a callback to the onCancel delegate to log a message when the tween is cancelled. This ensures specific actions are taken upon cancellation. ```csharp var tween = new ExampleTween { onCancel = (instance) => { Debug.Log("Tween has been cancelled"); }, }; ``` -------------------------------- ### Tween Delegates Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Callbacks invoked at different stages of a tween's lifecycle. ```APIDOC ## Tween Delegates Callbacks invoked at different stages of a tween's lifecycle. ### On Add The `onAdd` delegate will be invoked when the Tween has been added to a GameObject. ```csharp OnAddDelegate onAdd; ``` **Example Usage:** ```csharp var tween = new ExampleTween { onAdd = (instance) => { Debug.Log("Tween has been added"); }, }; ``` ### On Start The `onStart` delegate will be invoked when the Tween has started. ```csharp OnStartDelegate onStart; ``` **Example Usage:** ```csharp var tween = new ExampleTween { onStart = (instance) => { Debug.Log("Tween has started"); }, }; ``` ### On Update The `onUpdate` delegate will be invoked when the Tween has updated. ```csharp OnUpdateDelegate onUpdate; ``` **Example Usage:** ```csharp var tween = new ExampleTween { onUpdate = (instance, value) => { Debug.Log("Tween has updated"); }, }; ``` ### On End The `onEnd` delegate will be invoked when the Tween has ended. ```csharp OnEndDelegate onEnd; ``` **Example Usage:** ```csharp var tween = new ExampleTween { onEnd = (instance) => { Debug.Log("Tween has ended"); }, }; ``` ### On Cancel The `onCancel` delegate will be invoked when the Tween has been cancelled. ```csharp OnCancelDelegate onCancel; ``` **Example Usage:** ```csharp var tween = new ExampleTween { onCancel = (instance) => { Debug.Log("Tween has been cancelled"); }, }; ``` ### On Finally The `onFinally` delegate will be invoked when the Tween has ended or has been cancelled. ```csharp OnFinallyDelegate onFinally; ``` **Example Usage:** ```csharp var tween = new ExampleTween { onFinally = (instance) => { Debug.Log("Tween has ended or has been cancelled"); }, }; ``` ``` -------------------------------- ### Animate Transform Rotation Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Animate the rotation of a transform to a specified Quaternion end value over a duration. Requires the PrimeTween namespace. ```csharp // Rotate 'transform' from the current rotation to (0, 90, 0) in 1 second Tween.Rotation(transform, endValue: Quaternion.Euler(0, 90, 0), duration: 1); ``` -------------------------------- ### Non-allocating OnComplete delegate Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Avoid delegate allocations by passing the target object and using it within the callback. ```csharp Tween.Position(transform, new Vector3(10, 0), duration: 1) .OnComplete(() => SomeMethod()); // delegate allocation! ``` ```csharp Tween.Position(transform, new Vector3(10, 0), duration: 1) .OnComplete(target: this, target => target.SomeMethod()); // no allocation ``` -------------------------------- ### Execute Callback on Value Update Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Use the OnUpdate callback to execute custom logic when an animated value changes. The callback receives the target object and the tween instance, allowing access to interpolationFactor or progress. ```csharp // Rotate the transform around the y-axis as the animation progresses Tween.PositionY(transform, endValue, duration) .OnUpdate(target: transform, (target, tween) => target.rotation = Quaternion.Euler(0, tween.interpolationFactor * 90f, 0)); // Call the OnPositionUpdated() method on every position change Tween.PositionY(transform, endValue, duration) .OnUpdate(target: this, (target, tween) => target.OnPositionUpdated(tween.progress)); ``` -------------------------------- ### Reuse and Modify a PositionTween Instance Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md A tween configuration can be reused across multiple GameObjects and modified during its use. The same tween instance can be added to different GameObjects, and its properties can be changed before or during its execution. ```csharp var tween = new PositionTween { to = new Vector3(10, 5, 20), duration = 5, }; gameobject.AddTween(tween); differentGameObject.AddTween(tween); tween.to.x = 20; otherGameObject.AddTween(tween); ``` -------------------------------- ### Assign OnEnd Delegate Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Assigns a callback to the onEnd delegate to log a message when the tween finishes. This is typically used for post-completion actions. ```csharp var tween = new ExampleTween { onEnd = (instance) => { Debug.Log("Tween has ended"); }, }; ``` -------------------------------- ### Set Repeat Interval Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Define the delay in seconds before repeating the tween after it has finished. If not set, it repeats instantly. ```csharp float repeatInterval; ``` ```csharp var tween = new ExampleTween { repeatInterval = 5, }; ``` -------------------------------- ### Use Unscaled Time Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Set to true to use unscaled time for the tween. Otherwise, scaled time is used. ```csharp bool useUnscaledTime; ``` ```csharp var tween = new ExampleTween { useUnscaledTime = true, }; ``` -------------------------------- ### Custom Float and Color Tween Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md This C# snippet shows how to use the generic Tween.Custom() method to animate float and Color values. The onValueChange callback is used to update fields with the new tweened values. ```csharp float floatField; Color colorField; // Animate 'floatField' from 0 to 10 in 1 second Tween.Custom(0, 10, duration: 1, onValueChange: newVal => floatField = newVal); // Animate 'colorField' from white to black in 1 second Tween.Custom(Color.white, Color.black, duration: 1, onValueChange: newVal => colorField = newVal); ``` -------------------------------- ### Set Tween Duration Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Set the duration of the tween in seconds. If not set, the tween completes instantly. ```csharp float duration; ``` ```csharp var tween = new ExampleTween { duration = 5, }; ``` -------------------------------- ### Set Ease Type Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Define the animation curve for the tween. If an AnimationCurve is set, this will be ignored. Defaults to Linear if not set. ```csharp EaseType easeType; ``` ```csharp var tween = new ExampleTween { easeType = EaseType.QuadInOut, }; ``` -------------------------------- ### Punch Local Position Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Apply a 'punch' effect to the local position of a transform, simulating a quick outward movement and return. Requires the PrimeTween namespace. ```csharp // Punch localPosition in the direction of 'punchDir' var punchDir = transform.up; Tween.PunchLocalPosition(transform, strength: punchDir, duration: 0.5f, frequency: 10); ``` -------------------------------- ### Shake Local Position Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Shake the local position of a transform with a specified amplitude, duration, and frequency. Requires the PrimeTween namespace. ```csharp // Shake the y-axis position with an amplitude of 1 unit Tween.ShakeLocalPosition(transform, strength: new Vector3(0, 1), duration: 1, frequency: 10); ``` -------------------------------- ### Pause and Check Tween Instance Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Pauses a Tween instance by setting its isPaused property to true. Reading this property also returns the current paused state. ```csharp bool isPaused; ``` ```csharp var tween = new ExampleTween { }; var instance = gameObject.AddTween(tween); instance.isPaused = true; ``` -------------------------------- ### Define OnCancel Delegate Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Defines the delegate invoked when a Tween is explicitly cancelled. This is useful for cleanup or alternative logic paths. ```csharp OnCancelDelegate onCancel; ``` -------------------------------- ### Apply Custom Animation Curves Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Use AnimationCurve objects instead of standard Ease enums for custom animation curves. This allows for fine-grained control over the animation's acceleration and deceleration. ```csharp AnimationCurve animationCurve = AnimationCurve.EaseInOut(0, 0, 1, 1); Tween.PositionY(transform, endValue, duration, animationCurve); ``` -------------------------------- ### Tween Custom Float Value Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Animates a custom float value, such as an enemy's health, from its current value to a target value over a specified duration with easing. Requires an Enemy component and a FloatTween. ```csharp var enemy = GetComponent(); var tween = new FloatTween { from = enemy.health, to = 23, duration = 1, easeType = EaseType.SineOut, onUpdate = (_, value) => enemy.health = value, }; enemy.gameObject.AddTween(tween); ``` -------------------------------- ### Set Animation Curve Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Define a custom animation curve for the tween. If not set, the tween uses the Ease Type. ```csharp AnimationCurve animationCurve; ``` ```csharp var tween = new ExampleTween { animationCurve = AnimationCurve.EaseInOut(0, 0, 1, 1), }; ``` -------------------------------- ### Add TweenAnimation to a script Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Add TweenAnimation as a serialized field to your script to configure animations in the Inspector. ```csharp // Add TweenAnimation to your script, then set the animation up in the Inspector. [SerializeField] TweenAnimation doorAnimation = new(); void Update() { if (Input.GetKeyDown(KeyCode.Space)) { // Play the animation in response to gameplay events. doorAnimation.Trigger(); // Or set the state directly. doorAnimation.state = true; } } ``` -------------------------------- ### Add Tween to GameObject Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Adds a new Tween to a target GameObject, returning a TweenInstance to control it. Requires a generic ComponentType and DataType. ```csharp TweenInstance AddTween(this GameObject target, Tween tween) where ComponentType : Component; ``` ```csharp var tween = new ExampleTween { }; var instance = gameObject.AddTween(tween); ``` -------------------------------- ### Access Tween Target Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Retrieves the target GameObject on which the Tween instance is running. This property is read-only. ```csharp readonly GameObject target; ``` ```csharp var tween = new ExampleTween { }; var instance = gameObject.AddTween(tween); Debug.Log(instance.target); ``` -------------------------------- ### Animate Transform Position Source: https://github.com/kyrylokuzyk/primetween/blob/main/README.md Animate the Y position of a transform to a specified end value over a duration with an easing function. Requires the PrimeTween namespace. ```csharp using PrimeTween; // Animate 'transform.position.y' from the current value to 10 in 1 second using the Ease.InOutSine Tween.PositionY(transform, endValue: 10, duration: 1, ease: Ease.InOutSine); ``` -------------------------------- ### Define Tween End Value Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Set the 'to' value to explicitly define the end value of a tween. If not set, the tween will automatically use the current property value as its end point. ```csharp DataType to; var tween = new ExampleTween { to = new Vector3(10, 5, 20), }; ``` -------------------------------- ### Cancel All Tweens on GameObject Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Cancels all Tweens associated with a GameObject. Optionally includes children by setting includeChildren to true. Triggers OnCancel and OnFinally delegates. ```csharp void CancelTweens(this GameObject target, bool includeChildren = false); ``` ```csharp gameObject.CancelTweens(); ``` -------------------------------- ### Cancel Tween Instance Source: https://github.com/kyrylokuzyk/primetween/blob/main/Benchmarks/Packages/nl.jeffreylanters.tweens@c4f2322175/README.md Cancels a specific Tween instance. This will trigger the OnCancel and OnFinally delegates. Ensure the tween has been added to a GameObject first. ```csharp void Cancel(); ``` ```csharp var tween = new ExampleTween { }; var instance = gameObject.AddTween(tween); instance.Cancel(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.