Is it possible to create 2D animation at run time with Unity ?
unity-game-engine
Solution
a part of my importer
private AnimationClip CreateSpriteAnimationClip(string name, List<Sprite> sprites, int fps, bool raiseEvent = false)
{
int framecount = sprites.Count;
float frameLength = 1f / 30f;
AnimationClip clip = new AnimationClip();
clip.frameRate = fps;
AnimationUtility.GetAnimationClipSettings(clip).loopTime = true;
EditorCurveBinding curveBinding = new EditorCurveBinding();
curveBinding.type = typeof(SpriteRenderer);
curveBinding.propertyName = "m_Sprite";
ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[framecount];
for (int i = 0; i < framecount; i++)
{
ObjectReferenceKeyframe kf = new ObjectReferenceKeyframe();
kf.time = i * frameLength;
kf.value = sprites[i];
keyFrames[i] = kf;
}
clip.name = name;
AnimationUtility.SetAnimationType(clip, ModelImporterAnimationType.Generic);
//if (name != "Fall")
Debug.Log(clip.wrapMode);
clip.wrapMode = WrapMode.Once;
//setAnimationLoop(clip);
AnimationUtility.SetObjectReferenceCurve(clip, curveBinding, keyFrames);
clip.ValidateIfRetargetable(true);
if (raiseEvent)
{
//AnimationUtility.SetAnimationEvents(clip, new[] { new AnimationEvent() { time = clip.length, functionName = "on" + name } });
}
//clip.AddEvent(e);
return clip;
}
Problem
I am trying to create an animation at runtime. However, I didn't find the method to do this. Can Unity create it at runtime? What I want to do in web player are following: detect mouse click and get the click position. (flower appear there) decide flower colour randomly bloom the flower by using and animation which is configured using 3 sprites. (sprites simply change sequentially) As far as I confirmed, there is no method to change colour of the animation (sprites), so, I'm searching method to change the colour of the 3 sprites and combine these into an animation and run it. Although I could create an instance and change the colour, I couldn't find method of combining. Is it possible what I am trying to design in the first place?