Question by
Arystide · Apr 26, 2018 at 05:36 PM ·
buildunityeditoranimationclipruntime-generationprocedural animation
How do i create a sprite sheet animation by script without the UnityEditor classes
Hello guys,
I have this function that i use to create a sprite sheet animationClip at runtime. The problem is EditorCurveBinding, ObjectReferenceKeyframe, AnimationClipSettings and AnimationUtility are UnityEditor classes and cannot be used in the built version. i have searched all arount the web for a built version alternative but to no avail.
Does anyone have any idea about how to make this work for a built version?
Here is my function:
public static AnimationClip CreateAnimationCLip(string name, int frameRate, bool isLooping, string spritesPath, bool isLegacy = false)
{
AnimationClip animationClip = new AnimationClip();
animationClip.name = name;
animationClip.frameRate = frameRate;
animationClip.wrapMode = WrapMode.Loop;
animationClip.legacy = isLegacy;
EditorCurveBinding spriteBinding = new EditorCurveBinding();
spriteBinding.type = typeof(SpriteRenderer);
spriteBinding.path = "";
spriteBinding.propertyName = "m_Sprite";
Sprite[] idleSheet = Resources.LoadAll<Sprite>(spritesPath);
ObjectReferenceKeyframe[] spriteKeyFrames = new ObjectReferenceKeyframe[idleSheet.Length];
for (int i = 0; i < idleSheet.Length; i++)
{
spriteKeyFrames[i] = new ObjectReferenceKeyframe();
spriteKeyFrames[i].time = i / animationClip.frameRate;
spriteKeyFrames[i].value = idleSheet[i];
}
AnimationClipSettings animClipSett = new AnimationClipSettings();
animClipSett.loopTime = true;
AnimationUtility.SetAnimationClipSettings(animationClip, animClipSett);
AnimationUtility.SetObjectReferenceCurve(animationClip, spriteBinding, spriteKeyFrames);
return animationClip;
}
Comment