Hi RainyRizzle, I need to get animation clip duration in seconds. Can you help me in this? My recommendation:
private float GetAnimationDuration(int portraitIndex, string animClipName)
{
var portrait = portraits[portraitIndex];
if (portrait == null) return 0f;
// Find the animation clip by name and get its duration
var animClip = portrait._animClips.Find(clip => clip._name == animClipName);
if (animClip != null)
{
return animClip._duration; // Assuming _duration gives the length of the animation
}
return 0f;
}
Can you add _duration to animClip please? Or is there any workaround currently I can use it? Thanks in advance!
Hi.
We are sorry for the delay in responding to your inquiry as we have been reviewing all the posts you have left one by one.
In the meantime, you have already found the answer!
Yes, using AnimClip's TimeLength is a proper solution.
Although it's late, we will also leave the answer we prepared.
If you are interested, please refer to it.
Here is the script code in the answer we prepared:
bool isLoop = false; float timeLength = 0.0f; float duration = 0.0f; apAnimPlayData animPlayData = portrait.GetAnimationPlayData(animName); if (animPlayData != null) { if (animPlayData._linkedAnimClip != null) { isLoop = animPlayData._linkedAnimClip.IsLoop; timeLength = animPlayData._linkedAnimClip.TimeLength; float speedRatio = Mathf.Abs(animPlayData._linkedAnimClip._speedRatio); duration = (speedRatio > 0.0f) ? (timeLength / speedRatio) : -1.0f; } }
The above code checks the animation status by inputting portrait, a member of apPortrait type, and animName, a string type.
You can check its Loop, Time Length, and Duration.
In this example, Time Length and Duration are distinguished, and if you set a value other than 1 for the Animation Speed, Duration may increase or decrease accordingly.
You can use it according to the situation.
However, since referencing members of apAnimClip, which is a kind of internal data, makes the code not clean.
So, we want to provide wrapped properties as much as possible.
We modified the AnyPortrait script a little bit so that you can write your script a little more cleanly.
Please install the attached package file.
This package file was written based on AnyPortrait v1.5.1, so please check the version in advance.
After updating, you can write the above code more simply as follows.
apAnimPlayData animPlayData = portrait.GetAnimationPlayData(animName); if (animPlayData != null) { isLoop = animPlayData.IsLoop; timeLength = animPlayData.TimeLength; duration = animPlayData.Duration; }
The above method is a convenient way to save apAnimPlayData as a variable, and if you don't need to do so, you can write it shorter as follows.
isLoop = portrait.IsAnimationLoop(animName); timeLength = portrait.GetAnimationTimeLength(animName); duration = portrait.GetAnimationDuration(animName);
The scripts we provided will be included in the next update version.
Thank you for your great suggestion!
P.S. 1
We've also incorporated replies from the other post related to Loop into this comment.
P.S. 2
The issues regarding Docking and Maximize in the 3 posts are currently under review.
This issue is expected to take quite some time to be answered and as we have answered in other posts, it may be difficult to resolve.
Hi! I I think I found the solution.
Method 1:
Using the TimeLength parameter.
Method 2:
Dividing End Frame / FPS.
End Frame: 60
FPS: 30 60 / 30 = 2 seconds animation.
Code example:
private float GetAnimationDuration(int portraitIndex, string animClipName) { var portrait = portraits[portraitIndex]; if (portrait == null) return 0f; // Find the animation clip by name var animClip = portrait._animClips.Find(clip => clip._name == animClipName); if (animClip != null) { // Calculate the duration by dividing the end frame by the FPS float duration = animClip.TimeLength; // First Method //float duration = animClip.EndFrame / animClip.FPS; // Second Method return duration; } // If animation clip is not found, return 0 Debug.LogWarning($"Animation clip '{animClipName}' not found in portrait {portrait.name}."); return 0f; }