In future, please ask one query per submit. As you’ll be able to see within the hyperlinks under, 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 will name it normalizedTime
as an alternative, to tell apart this.
The place is Animation Occasion’s receiver object saved?
In accordance with the documentation, animation occasions name capabilities on “any script hooked up to the GameObject” that hosts the Animator
or Animation
part taking part in the animation. “Much like SendMessage” as the scripting API docs say.
Throughout runtime, I might need to know which animation fires the occasion, and the time level of the occasion within the animation.
In case you have a operate that is being referred to as by an animation occasion, you will 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, however it ought to give the final thought of the technique:
(AnimationClip, float) FindAnimationEventSource(string functionName)
{
// If fired by an Animation part,
// we simply should examine all cross-fading animation states.
if (TryGetComponent(out Animation animation))
{
foreach (AnimationState state in animation)
{
foreach (AnimationEvent e in state.clip.occasions)
{
if (e.functionName != functionName) proceed;
// A clip may name the identical occasion operate greater than as soon as,
// so let's be sure 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
// should examine 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.
}
Tips on how to find SpriteRenderer, and so on. from a AnimationClip? (I imply, for SpriteRenderer, Animator and so on. do they all the time sit between 0-1 depth away, or they search one another deeper? Unity part dependencies are extraordinarily puzzling and undocumented)
It is not that the relationships of the elements 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 youngster hierarchy, if that is what I select to do because the creator of the sport. Paths to those elements are specified explicitly when authoring the clip, they don’t seem to be discovered by looking out.
Here is 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 title of the sphere being modified. -
The
path
line offers the trail to the article with this part. -
The
classID
line identifies theSpriteRenderer
part.