- Düzenlendi
How to access the events of a spine animation
I started working on the spine a few days ago and was looking for ways to control the duration of animation in addition to using time scaling.
After researching, I saw that I could follow the example of the SpineGauce scene and manually update the playback timepoint.
public void SetGaugePercent (float percent)
{
if (skeletonRenderer == null) return;
var skeleton = skeletonRenderer.skeleton; if (skeleton == null) return;
fillAnimation.Animation.Apply(skeleton, 0, percent, false, null, 1f, MixBlend.Setup, MixDirection.In);
skeleton.UpdateWorldTransform();
}
With that, I could make the animation run at a different time than the original, without using the timescale.
The example code below shows my attempt
attackAnimation = skeletonRenderer.skeleton.Data.FindAnimation("attack");
//The original animation have a duration of 1s so lets say the new duration have 10s
attackAnimation.Duration = 10f;
public void Update()
{
if (m_renderer != null )
{
elapsedTime += Time.deltaTime;
float percent = elapsedTime / attackAnimation.Duration;
attackAnimation.Apply(m_renderer.skeleton, 0, percent, false, null, 1f, MixBlend.Setup, MixDirection.In);
m_renderer.skeleton.UpdateWorldTransform();
}
}
However, the animation events are not triggered and in the SpineGauce example, the list of events is passed as null.
So I would like to know if there is a way to access the events to pass them in the Apply method because I couldn't find any way to access this list.
If you want to play back an animation with different speed to result in a different duration, it's still the best way to adjust the TimeScale of the TrackEntry returned by SetAnimation
or AddAnimation
.
edubrand yazdı
attackAnimation = skeletonRenderer.skeleton.Data.FindAnimation("attack"); //The original animation have a duration of 1s so lets say the new duration have 10s attackAnimation.Duration = 10f;
In the code above you are modifying the shared animation data, which is shared across all skeleton instances in your scene. You normally don't want that. Instead you should be modifying TrackEntry properties of a single enqueued animation.
See the documentation here to better understand the distance between data vs. instance objects:
Runtime Architecture - Spine Runtimes Guide: Data objects
In general we highly recommend to take a look at the spine-unity documentation page, and the example scenes that come with the spine-unity package.
Thank you for the answer
I read the links to the documentation you provided and just to be clear, generally, increasing or decreasing the duration of an animation by the property
fooAnimation.Duration = value;
Does not readjust the animation as in the timescale, so is there any application to set its value that differs from the original or is this property more often used as reading?
And the other question is about the Animation.Apply method only worth using as in SpineGauge example scene, if there are no events linked to that animation since it is not possible to access ExposedList<Event> to pass to the method?
fillAnimation.Animation.Apply(skeleton, 0, percent, false, null, 1f, MixBlend.Setup, MixDirection.In);
In general the API Reference page provides the most precise definition of properties. In your case animation.Duration
is documented here:
Animation duration
It is only used for when an animation shall end, and when it shall start over if looping.
edubrand yazdıso is there any application to set its value that differs from the original or is this property more often used as reading?
You generally only adjust shared animation or skeleton data if you are performing some post-processing on the imported data. Don't modify shared data if you want to adjust a single animation instance for one skeleton only.
edubrand yazdıAnd the other question is about the Animation.Apply method only worth using as in SpineGauge example scene, if there are no events linked to that animation since it is not possible to access ExposedList<Event> to pass to the method?
See the documentation here. If you pass an ExposedList<Event> events
to the method, it will be filled with fired events accordingly and you could then iterate over these events and process them. In your use case we don't recommend to apply animations manually however. You normally want to use the SkeletonAnimation
component to control playback and blending of all your animations.