6

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:

  1. detect mouse click and get the click position. (flower appear there)

  2. decide flower colour randomly

  3. 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?

lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46

3 Answers3

5

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;
}
dnkira
  • 362
  • 3
  • 8
  • +1... ObjectReferenceKeyframe was my missing piece. Surprised information on this is not more readily available. Took a ton of searching to find this post. – D_C Nov 01 '14 at 06:24
1

i usually use this method to animate gameobject from one pos to another, may be useful to you.

        public void MoveGO (GameObject TempGO, Vector3 StartPos, Vector3 EndPos)
    {
            float clipLength = 1f;
            AnimationCurve curve1 = null, curve2 = null, curve3 = null;
            AnimationClip clip = null;
            curve1 = AnimationCurve.Linear (0, StartPos.x, clipLength, EndPos.x);
            curve2 = AnimationCurve.Linear (0, StartPos.y, clipLength, EndPos.y);
            curve3 = AnimationCurve.Linear (0, StartPos.z, clipLength, EndPos.z);

            clip = new AnimationClip ();
            clip.SetCurve ("", typeof(Transform), "localPosition.x", curve1);
            clip.SetCurve ("", typeof(Transform), "localPosition.y", curve2);
            clip.SetCurve ("", typeof(Transform), "localPosition.z", curve3);

            if (TempGO.GetComponent ("Animation") == null) {
                    TempGO.AddComponent ("Animation");
            }
            if (TempGO.animation.IsPlaying ("AnimationDemo")) {
                    //TempGO.animation["AnimationDemo"].time = 0.5f ;
                    TempGO.animation.Sample ();
                    TempGO.animation.RemoveClip ("AnimationDemo");
            }

            TempGO.animation.AddClip (clip, "AnimationDemo");
            TempGO.animation ["AnimationDemo"].speed = 1f;
            TempGO.animation.Play ("AnimationDemo");
            //TempGO.animation.wrapMode=WrapMode.PingPong;
    }
Dasu
  • 433
  • 6
  • 16
  • just pass the param i.e pass the gameobject, start and end positions – Dasu Mar 27 '14 at 10:34
  • Thank you Dasu! It's really good code for me!(Actually,I didn't understand relation between AnimationCurve,AnimationClip, and animation)I'm going to try to design to complete what I want to do with reference your code.Thank you! –  Mar 28 '14 at 01:10
  • hey did it helped you? then please tick right so that other can use it. – Dasu Apr 01 '14 at 06:04
  • sorry for my late reply, Dasu, however, it's not correct answer what I asked, so I'm waiting for other answer. –  Apr 14 '14 at 07:04
  • Have you used HotTween unity package? its free on asset store. Using this you can create animations at run time.Its very simple to use. – Dasu Apr 14 '14 at 19:57
0

You could create that animation using the Unity's new animation tools. Since you know that there are three sprites you could make a prefab which has that animation. Then just load the correct sprites to the empty sprite objects.

Esa
  • 1,636
  • 3
  • 19
  • 23
  • Thank you for your answer Esa. I could create animation by using Animation, and play it. In this case, I want to create animation in script(C#), because number of color variation pattern is so large, then, I'm trying to create animation at runtime.Could you give me the way or better way? –  Mar 27 '14 at 09:42