Is it possible to create a 2D animation while working with Unity?

I am trying to create an animation at runtime. However, I did not find a way to do this.

Can Unity create it at runtime?

What I want to do in the web player:

  • mouse click detection and getting click position. (flower appears)

  • choose flower color randomly

  • flower blooming, using animation, which is configured using 3 sprites. (sprites just change sequentially)

As I understand it, there is no way to change the color of the animation (sprites), so I'm looking for a way to change the color of the three sprites and combine them into an animation and run it.

Although I could create an instance and change the color, I could not find a join method.

Is it possible that I'm trying to design in the first place?

+6
source share
3 answers

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; } 
+4
source

I usually use this method to animate a game object from one point 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; } 
+1
source

You can create this animation with the new Unity tool for Unity . Since you know that there are three sprites, you can make an assembly that has this animation. Then just load the correct sprites into the empty sprite objects.

0
source

All Articles