In the game, there is also a more common animation, which is generated after the goal, scores will be displayed in the game, and quickly moved to make it lose position to join. I'm going to use Ngui's tween to make such a fractional animation effect.
Create a UI Root based on the unity plugin's Ngui Learning (2). Then use Ngui to create a label and a button.
In the project form, create a prefab in the Resources/prefabs directory. The prefab is a Ngui label, then select Ngui->tween->rotation and Ngui->tween->position in the menu
The play style indicates how the animation is played.
Animation Curve animation speed curve. You can define your own after clicking.
Duration Specifies the length of time an animation is animated. Start delay is delayed play. seconds in the unit.
The Tween Group represents the gradient animation group ID.
Ignore Timescale whether to ignore timescale.
Tween in Rotation
from and to, set the rotation angle of the gameobject at the beginning and end of the x, Y, Z, and now I set the-720 on the to axis, indicating that the object rotates 2 times clockwise on the z axis.
Tween in Position
from and the to , set the coordinates of the gameobject at the beginning and end of the x, Y, z, respectively, the coordinate system under Ngui, which is not set to the coordinate value of to, which is set in the code later.
Create a Tweentest script file in the project form. The code is as follows:
using UnityEngine;
Using System.Collections;
public class Tweentest:monobehaviour {
private Gameobject Prefab;
private UIButton btn;
private UILabel Scorelabel;
private int score;
private int add;
private Gameobject Addscore;
void Awake () {
< /span>//pre-created a frequently used score prefab
prefab = (gameobject) resources.load ("prefabs/ Addscore ");
}
//Use the FOR Initialization
void Start () {
score = 1000;
btn = gameobject.find ("button"). Getcomponent<uibutton> ();
Scorelabel = Gameobject.find ("Score"). Getcomponent<uilabel> ();
Scorelabel.text = "" + Score;
< /span>//Set the button response function
Eventdelegate.add (Btn.onclick, addscore);
}
/ /Update is called once per frame
void Update () {
}
void Addscore () {
Clone score Gameobject
Addscore = (gameobject) Instantiate (prefab, new Vector3 (0, 0, 0), transform.rotation);
UILabel AddLabel = Addscore. Getcomponent<uilabel> ();
System.Random Random = new System.Random ();
Random score.
add = random. Next (50, 100);
Addlabel.text = "" + add;
Get Tweenposition Object
Tweenposition TWEENP = Addscore. Getcomponent<tweenposition> ();
Set the coordinate value of to, which is the value of the Ngui coordinate system, so you need to reverse the world coordinate value transform. Inversetransformpoint
Tweenp.to = transform. Inversetransformpoint (scoreLabel.transform.position);
Debug.Log (TweenP.to.ToString ());
< /span>//set the callback function after the end of the animation,
Eventdelegate.add (tweenp.onfinished, scoremovefinished);
< /span>//in inspector form tween Position hook select Remove The script name there check box, so Tween position does not run, requires manual play
Tweenp.play ();
}
void scoremovefinished () {
score + = add;
Scorelabel.text = "" + score;
Destroy (Addscore);
}
}
The key code is tweenp.to = transform. Inversetransformpoint (scoreLabel.transform.position); Since the To,from value of tween position is based on the Ngui coordinate system, what I can get right now is ScoreLabel.transform.position, which is the value of the world coordinate system, so it needs to be converted to the value of the Ngui coordinate system.
This is exactly the reversal of the (8) coordinate system.
The script is then added to the UI root, after execution. Clicking the button will randomly generate a fraction in the middle, then rotate it at high speed and move it to the total score and accumulate the total.
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
Unity The plugin Ngui learned (9)--tween and converted to the world coordinate system size Ngui size