Moving a Transform
- Create a
struct
which implements IAnimation:
public struct PositionAnimation : IAnimation
{
public Vector3 Start;
public Vector3 End;
public Transform TransformToMove;
public void Update(float percentDone)
{
TransformToMove.localPosition = Vector3.Lerp(Start, End, percentDone);
}
}
- To trigger the animation, call Run with the desired duration:
PositionAnimation animation = new PositionAnimation()
{
Start = transform.localPosition,
End = Vector3.zero,
TransformToMove = transform,
};
// Run the animation for 1 second
animation.Run(durationInSeconds: 1f);
Animations
Overview
Adding animations and visual effects driven by user interactions or underlying application states (e.g. a loading indicator) can really enhance the quality and responsiveness of your Nova-built user experience.
Depending on your workflow, you may have a preference for scripting vs. non-scripting animation tools. Nova supports both, so you and your team are free to choose any or all options which best suits you and your project.
Code Amount | Summary |
---|---|
No Code | UIBlocks are fully compatible with Unity's key-frame-based Animation System, a flexible, code-free option for creating complex animations and visual states for your UI content. |
A Bit Of Code | Nova's runtime Animation API provides a simple, extensible, and garbage-free animation system, making it a unique alternative to existing script-based animation solutions, commonly referred to as tweening systems. |
A Bit More Code | Custom scripting implementation. |
Nova Animation API
IAnimation and IAnimationWithEvents
Let's look at an example of an animation in Nova:
public struct PositionAnimation : IAnimation
{
public Vector3 Start;
public Vector3 End;
public Transform TransformToMove;
public void Update(float percentDone)
{
TransformToMove.localPosition = Vector3.Lerp(Start, End, percentDone);
}
}
More concretely, an animation in Nova is a struct
which implements either the IAnimation or IAnimationWithEvents interface. For most common, simple, stateless transitions, IAnimation is everything you need. For some more complex, event-driven animations, IAnimationWithEvents provides additional hooks to handle changes to the animation's running state. Outside the differences in state updates, however, working with the two interfaces (and the structs
which implement them) looks identical.
// Create an instance of the animation
PositionAnimation positionAnimation = new PositionAnimation()
{
TransformToMove = transform,
Start = transform.localPosition;
End = Vector3.zero,
};
// Queue the animation to run
AnimationHandle positionHandle = positionAnimation.Run(1f);
AnimationHandle
An AnimationHandle is a unique identifier for either an individual animation, group animation, sequenced animation, or any combination of the three. An AnimationHandle is returned whenever an animation is queued with the animation system via:
// pretending these are configured...
PositionAnimation positionAnimation, otherPositionAnimation;
// An individual animation
AnimationHandle singleHandle = positionAnimation.Run(1f);
// An individual animation which repeats
AnimationHandle loopHandle = positionAnimation.Loop(1f, iterations: 2);
// A group animation
AnimationHandle groupHandle = singleHandle.Include(otherPositionAnimation);
// A sequenced animation
AnimationHandle sequenceHandle = loopHandle.Chain(otherPositionAnimation, 1f);
In addition to supporting creation of compound animations, like above, AnimationHandles are also used to control the state of the individual animation, group animation, or sequenced animation which produced them:
// Queue the animation to run, returns an AnimationHandle
AnimationHandle positionHandle = positionAnimation.Run(1f);
// Pause the animation
positionHandle.Pause();
// Resume the animation
positionHandle.Resume();
// Stop the animation in its current state
positionHandle.Cancel();
// Alternatively, stop the animation and move it to
// what would be the state on its last frame
positionHandle.Complete();
Animation API in Action
Run
Run an animation a single time, over the specified duration.
// Runs the animation for 1 second
animation.Run(1f);
Loop
Loop an animation for a specific number of iterations, or until cancelled.
// Loops the animation 3 times,
// 1 second per loop iteration.
animation.Loop(1f, 3);
Include
Run an animation simultaneously with another animation.
// Run
AnimationHandle handle = animation1.Run(1f);
// Run animation2 with animation1
handle.Include(animation2);
Chain
Run an animation after another animation.
// Run
AnimationHandle handle = animation1.Run(1f);
// Queue animation2 to run after animation1
handle.Chain(animation2, 1f);
Pause & Resume
Pause an animation and resume it at a later point in time.
// Run
AnimationHandle handle = animation.Run(1f);
// Pause animation
handle.Pause();
// Resume animation
handle.Resume();
Cancel
Cancel an animation.
// Run
AnimationHandle handle = animation.Run(1f);
// Stop animation in its current state
handle.Cancel();
Complete
Jump an animation to its final update.
// Run
AnimationHandle handle = animation.Run(1f);
// Move to last frame of animation
handle.Complete();
Combinations
Any number of animations can be combined with Chain and Include to create more complex, compound animations from multiple simple transitions.
// Queue animation1 to run
AnimationHandle combo = animation1.Run(1f);
// Run animation2 with animation1
combo = combo.Include(animation2);
// Queue animation3 to run after animation1 and animation2
combo = combo.Chain(animation3, 1f);
// Run animation4 with animation3
combo = combo.Include(animation4);