[ad_1]
In future, please ask one query per submit. As you’ll be able to see within the hyperlinks beneath, many of those questions are answered by the documentation already,
Is “float time” in seconds?
The documentation says it’s. Seconds as unit of time is the norm in Unity.
When a time is represented relative to the size of a clip, they’re going to name it normalizedTime
as a substitute, to differentiate this.
Where is Animation Event’s receiver object saved?
According to the documentation, animation occasions name capabilities on “any script connected to the GameObject” that hosts the Animator
or Animation
part enjoying the animation. “Similar to SendMessage” as the scripting API docs say.
During runtime, I might need to know which animation fires the occasion, and the time level of the occasion within the animation.
If you might have a perform that is being referred to as by an animation occasion, you may get a reference to the Animation
or Animator
part on that very same object, inquire about its currently-played clip(s), and discover the matching occasion in them.
That might look one thing like this – I have not examined this code extensively, but it surely ought to give the final thought of the technique:
(AnimationClip, float) DiscoverAnimationEventSource(string functionName)
{
// If fired by an Animation part,
// we simply must verify all cross-fading animation states.
if (TryGetComponent(out Animation animation))
{
foreach (AnimationState state in animation)
{
foreach (AnimationOccasion e in state.clip.occasions)
{
if (e.functionName != functionName) proceed;
// A clip may name the identical occasion perform greater than as soon as,
// so let's make certain this occasion matches the playback time.
if (e.time <= state.time
&& e.time >= state.time - Time.deltaTime * state.velocity)
{
return (state.clip, e.time);
}
}
}
}
// For an Animator, issues get extra sophisticated, since we
// must verify the present and subsequent transition states in every layer.
else if (TryGetComponent(out Animator animator))
{
for (int i = 0; i < animator.layerCount; i++) {
var state = animator.GetCurrentAnimatorStateInfo(i);
foreach (var clipInfo in animator.GetCurrentAnimatorClipInfo(i)) {
foreach (var e in clipInfo.clip.occasions) {
if (e.functionName != functionName) proceed;
float time = state.normalizedTime * clipInfo.clip.size;
if (e.time <= time
&& e.time >= time - Time.deltaTime * state.velocity)
{
return (clipInfo.clip, e.time);
}
}
}
state = animator.GetNextAnimatorStateInfo(i);
foreach (var clipInfo in animator.GetNextAnimatorClipInfo(i))
{
foreach (var e in clipInfo.clip.occasions)
{
if (e.functionName != functionName) proceed;
float time = state.normalizedTime * clipInfo.clip.size;
if (e.time <= time
&& e.time >= time - Time.deltaTime * state.velocity)
{
return (clipInfo.clip, e.time);
}
}
}
}
}
return (null, float.NaN); // No match discovered.
}
How to find SpriteRenderer, and so forth. from a AnimationClip? (I imply, for SpriteRenderer, Animator and so forth. do they at all times sit between 0-1 depth away, or they search one another deeper? Unity part dependencies are extraordinarily puzzling and undocumented)
It’s not that the relationships of the parts are undocumented, it is that these relationships are as much as the developer. I can have an Animation
on one object modify properties on one other object arbitrarily distant in its baby hierarchy, if that is what I select to do because the creator of the sport. Paths to those parts are specified explicitly when authoring the clip, they don’t seem to be discovered by looking out.
Here’s an excerpt of the YAML of a pattern AnimationClip
I simply made, altering the sprite of a SpriteRenderer
part situated on an object named “Grandchild” which is nested underneath an object referred to as “FirstChild” which is in flip nested underneath the foundation object with the Animation
part.
m_PPtrCurves:
- curve:
- time: 0
worth: {fileID: 21300000, guid: 7bc5fd6fd95fa87488b4b5405bfccdb7, sort: 3}
- time: 0.21666667
worth: {fileID: 21300000, guid: e2b76bfe1164e774291c9a49d5cea5f0, sort: 3}
- time: 0.38333333
worth: {fileID: 21300000, guid: 7bc5fd6fd95fa87488b4b5405bfccdb7, sort: 3}
attribute: m_Sprite
path: FirstChild/Grandchild
classID: 212
-
The
attribute
line signifies the identify of the sector being modified. -
The
path
line provides the trail to the article with this part. -
The
classID
line identifies theSpriteRenderer
part.
[ad_2]