Unity3D use scripts to create dynamic, animated call (rpm)

Original link: https://blog.csdn.net/pigautumn/article/details/81781403

Demand scenario: aligned top to bottom by a plurality of unspecified number of objects, moving from left to right in turn requires 0.1s intervals to a location, and then returns a short distance.

Analysis: The animation system using Unity3D is very easy to implement this feature, but uncertain number of objects, consider bind it into the same object, the object is dynamically created Animation, AnimationClip (Animation may contain a number of actions that AnimationClip, but we need only one action), and then create different AnimationCurve for each sub-objects, and finally call the animation, you can achieve each object has moved according to the motion animation set.

Code:

testAnim class
{
Private Animation _animation = null;

public the GameObject the Target = null; // audiences
public GameObject [] Items = null; // audiences array of child objects
public float [] PosY = null; Y // child objects of the target object coordinate

public float AnimDuration = 0.3f; // animation used time
public float AnimBackTime = 0.1f; // the return of the animation time
public float AnimInterval = 0.1f; // object animation interval between adjacent
public float InitXPos = -25; // X coordinate initialization position
public float MaxXPos = 90; // X coordinate is the maximum position of the
public float DestXPos = 60; // X coordinate of the target position

// initialization for the Use the this
void the Start ()
{
IF (the target == == null || the Items || items.length null <= 0 || items.length = PosY.Length)!
{
return;
}

= Target.AddComponent _animation <Animation> ();

var = new new AnimationClip Clip ()
{
name = "Test",
Legacy = to true,
WrapMode = WrapMode.Once
};

for (var I = 0; I <items.length; I ++ )
{
var = the Items Item [I];
IF (Item == null)
{
Continue;
}

var = relativePath The item.name, for;

// Create animation X-axis
var = new new AnimationCurve Curve ();
curve.AddKey (new new Keyframe (0 , InitXPos)); // initial state
curve.AddKey (new keyframe (0 + AnimInterval * i, InitXPos)); // pause key frame
curve.AddKey (new keyframe (0 + AnimInterval * i + AnimDuration, MaxXPos)); // motion keyframe
curve.AddKey (new Keyframe (AnimInterval * i + AnimDuration + AnimBackTime, DestXPos)); // Returns the key frame
clip.SetCurve (relativePath The, typeof (RectTransform), "m_AnchoredPosition.x", Curve);

// Create Animation Y-axis
= new new AnimationCurve Curve ();
curve.AddKey (new new Keyframe (0, PosY [I]));
curve.AddKey (new new Keyframe (AnimDuration AnimInterval * + I, PosY [I]));
clip.SetCurve (relativePath The, typeof (RectTransform), "m_AnchoredPosition.y", Curve);

// create the animation Scale
Curve = new new AnimationCurve ();
curve.AddKey (new Keyframe (0, 0)); // initial state
curve.AddKey (new Keyframe (0 + AnimInterval * i, 0)) ; // pause state
curve.AddKey (new Keyframe (AnimInterval * i + AnimDuration, 1)); // motion
clip.SetCurve(relativePath, typeof(RectTransform), "m_Scale.x", curve);
clip.SetCurve(relativePath, typeof(RectTransform), "m_Scale.y", curve);
}

_animation.AddClip(clip, clip.name);
_animation.Play(clip.name);
}
}
Points:

(1) each animation (Animation) may comprise a plurality of segments (AnimationClip), where only one;

(2) each segment comprising a plurality of curves (AnimationCurve), each of which describes a curve object (relativePath The) which attributes (e.g. RectTransform of m_AnchoredPosition.x, indicates a change of the anchor position of the X-axis) how motion;

(3) each action curve (AnimationCurve) comprising a plurality of key frames (Keyframe), shows changes of the movement, for example from X-axis coordinate (0 second position 0) to (0.3s, position 90), then move to (0.4s, position 60);

(4) The important point is that in describing the type of property RectTransform example, requires the use of the form "m_AnchoredPosition.x", refer to "solve AnimationClip.SetCurve RectTransform Color parameters appear Missing! Situation."
---------------------
Author: xhubobo
Source: CSDN
Original: https: //blog.csdn.net/pigautumn/article/details/81781403
Disclaimer: This article as a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/fujianxiaopihai/p/10966235.html