user3467689
user3467689

Reputation:

Is it possible to create 2D animation at run time with Unity ?

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?

Upvotes: 6

Views: 6490

Answers (3)

dnkira
dnkira

Reputation: 362

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;
}

Upvotes: 5

Dasu
Dasu

Reputation: 433

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;
    }

Upvotes: 1

Esa
Esa

Reputation: 1666

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.

Upvotes: 0

Related Questions