Guillemsc / GTweensGodot

C# tweening library for Godot 4.x
MIT License
180 stars 6 forks source link

License: MIT Release NuGet Downloads Tests Twitter Follow

Contributions welcome

LogoWide

GTweens-Godot is a lightweight and versatile tweening library for Godot 4 with C#. This library simplifies the process of creating animations and transitions in your Godot projects, allowing you to bring your game elements to life with ease.

Unlike the default Godot tweening engine, which relies on nodes and their string properties to create animations, this tweening engine doesn't require the use of nodes.

An extension that builds upon the GTweens library.

🍰 Features

📦 Installation

Form Asset Library

  1. Inside Godot, open the AssetLib tab.

image

  1. Search for and select "GTweens (C#)".

image

  1. Download then install the asset.

  2. On the Godot editor, go to Project/Project Settings/Autoload, and select GTweensGodot/Godot/Source/Contexts/GodotGTweensContextNode.cs to be autoloaded.

From NuGet:

  1. Install the GTweensGodot NuGet package in your godot project.
  2. Create a new node script, anywhere in your project, that's going to update all the tweens. Copy and paste the code from here:

    public partial class GTweensGodotUpdater : GodotGTweensContextNode
    {
    
    }
  3. On the Godot editor, go to Project/Project Settings/Autoload, and select the GTweensGodotUpdater.cs we just created, to be autoloaded.

    To quickly check if everything has been setup properly, you can create a new script with this code, and assign any Node2D to the Target export, and play it.

    public partial class TweenExample : Node
    {
        [Export] public Node2D Target;
    
        public override void _Ready()
        {
            Target.TweenPosition(new Vector2(100, 0), 3)
               .SetEasing(Easing.InOutCubic)
               .Play();
        }
    }

From releases:

  1. Download the latest GTweensGodot.zip release.
  2. Unpack the GTweensGodot.zip folder into the Godot's project root folder.
  3. On the Godot editor, go to Project/Project Settings/Autoload, and select GTweensGodot/Godot/Source/Contexts/GodotGTweensContextNode.cs to be autoloaded.

[!WARNING] If the example scenes cannot be opened, it probably means that the contents of GTweensGodot.zip were not placed on the root of your project, or that the extracted folder has been renamed. Make sure you don't change the path nor rename any folder, since this will break scene references.

✔️ After installing

[!NOTE] (Except on NuGet installation) To quickly check if everything has been setup properly, you can go to GTweensGodot/Godot/Examples/Scenes/ and open any of the example scenes. When you run any of those scenes, a simple functionality example should play.

[!NOTE] If after playing an example scene, nothing moves or gets animated, this means that the GodotGTweensContextNode.cs has not been autoloaded. Make sure to properly follow the last installation step.

📚 Getting started

Nomenclature

Prefixes

Prefixes are important to use the most out of IntelliSense, so try to remember these:

Generic tweening

This is the most flexible way of tweening and allows you to tween almost any value.

// For default C# values (int, float, etc)
GTweenExtensions.Tween(getter, setter, to, duration)

// For Godot specific values (Vector2, Vector3, etc)
GTweenGodotExtensions.Tween(getter, setter, to, duration)
// For default C# values
GTween tween = GTweenExtensions.Tween(
    () => Target.SomeFloat, // Getter
    x => Target.SomeFloat = x, // Setter
    100f, // To
    1 // Duration
);

// For Godot specific values
GTween tween = GTweenGodotExtensions.Tween(
    () => Target.Position, // Getter
    x => Target.Position = x, // Setter
    new Vector2(100f, 100f), // To
    1 // Duration
);

Shortcut tweening

GTweem includes shortcuts for some known C# and Godot objects, like Node2D, Node3D, Control, etc. You can create a tween directly from a reference to these objects, like:

node2D.TweenPositionX(100f, 1f);
node3D.TweenScale(new Vector3(2f, 2f, 2f), 1f);
control.TweenSizeY(200f, 2f);

See all shortcuts you can use.

Sequences

Sequences are a combination of tweens that get animated as a group. Sequences can be contained inside other sequences without any limit to the depth of the hierarchy. To create sequences, you need to use the helper GTweenSequenceBuilder.

tween.SetEasing(Easing.InOutCubic); tween.Play();


As it can be seen on the example, you can Append/Join different things with the builder:
- Append/Join Callback: adds a callback that will be called when this part of the sequence is executed.
- Append/Join Time: adds a time delay (in seconds) to the sequence.
- Append/Join Sequence: creates a new `GTweenSequenceBuilder` and provides it through the action. Then it automatically builds it and adds the resulting tween to the sequence.

[Example of a complex sequence](https://github.com/Guillemsc/GTweensGodot/blob/main/Godot/Examples/Scripts/Cube3DExample.cs):

![ezgif com-gif-maker](https://github.com/Guillemsc/GTweensGodot/assets/17142208/92e01c51-a9e8-43c4-a5d8-280ea03d4ae9)

### Tween controls
- **PlayConfigured**: plays the tween. 
    - Allows you to configure the process mode.
    - Allows you to configure if it will be paused if `GetTree().Paused` is set to true.
    - If the parameter `instantly` is set to true, the tween will be instantly completed after starting (will reach the end of the sequence).
- **Play**: plays the tween. Runs on the `_Process` method. Will be paused if `GetTree().Paused` is set to true.
- **Kill**: kills the tween. This means that the tween will instantly stop playing, leaving it at its current state.
- **Complete**: instantly reaches the final state of the tween, and stops playing.
- **Reset**: sets the tween to its initial state, and stops playing.
- **SetLoops**: sets the amount of times the tween should loop.
- **SetEasing**: sets the easing used by the tween. If the tween is a sequence, the easing will be applied to all child tweens. Set to linear by default.
- **SetTimeScale**: sets the time scale that will be used to tick the tween. Set to 1 by default.

### Tasks
- **PlayConfiguredAsync**: plays the tween and returns a Task that waits until the tween is killed or completed.
    - Allows you to configure the process mode.
    - Allows you to configure if it will be paused if `GetTree().Paused` is set to true.
    - If the parameter `instantly` is set to true, the tween will be instantly completed after starting (will reach the end of the sequence).
    - If the parameter CancellationToken is cancelled, the tween will be killed.
- **PlayAsync**: plays the tween and returns a Task that waits until the tween is killed or completed. If the parameter CancellationToken is cancelled, the tween will be killed. Runs on the `_Process` method. Will be paused if `GetTree().Paused` is set to true.
- **AwaitCompleteOrKill**: returns a Task that waits until the tween is killed or completed.

## 📖 Shortcuts
These are the currently avaliable shortcuts for Godot nodes (the list grows with time).
- **Node2D**: GlobalPosition, Position, GlobalRotation, Rotation, GlobalRotationDegrees, RotationDegrees, GlobalScale, Scale, GlobalSkew, Skew.  
- **Node3D**: GlobalPosition, Position, GlobalRotation, Rotation, GlobalRotationDegrees, RotationDegrees, GlobalScale, Scale.
- **Control**: GlobalPosition, Position, Rotation, RotationDegrees, Scale, Size, PivotOffset.
- **Sprite2D**: Offset.
- **SpriteBase3D**: Modulate, Offset.
- **Light2D**: Color, Energy, ShadowColor.
- **Light3D**: LightColor, LightEnergy, LightIndirectEnergy, LightVolumetricFogEnergy, LightAngularDistance, ShadowBias, ShadowNormalBias, ShadowTransmittanceBias, ShadowOpacity, ShadowBlur.
- **Camera2D**: Offset, Zoom.
- **Camera3D**: HOffset, VOffset, Fov.
- **AudioStreamPlayer2D**: VolumeDb, PitchScale.
- **AudioStreamPlayer3D**: VolumeDb, PitchScale, AttenuationFilterDb, AttenuationFilterCutoffHz.
- **CanvasModulate**: Color.
- **CanvasItem**: Modulate, SelfModulate.
- **ShaderMaterial**: PropertyInt, PropertyFloat, PropertyVector2, PropertyVector2I, PropertyColor.
- **BaseMaterial3D**: AlbedoColor, Metallic, MetallicSpecular, Roughness.

  ![Reddit](https://github.com/Guillemsc/GTweensGodot/assets/17142208/f03cca23-94b5-484b-8202-0d1961b14f98)