Unity Plugin Ngui Learning (9)--tween and world coordinate system dimensions to Ngui dimensions

Source: Internet
Author: User

In the game, there is a more common animation effect, that is, after the score is generated, scores will be displayed in the game, and quickly moved to the position of the total points and add it. Today I'm going to use Ngui's tween to make this kind of fractional animation effect.


Learn from the Unity Plugin's Ngui (2), create a UI Root, and then use Ngui to create a label and a button.


In the Project window, create a prefab in the Resources/prefabs folder, which is a Ngui label, and then select Ngui->tween->rotation and ngui-in the menu. >tween->position



The play style indicates how the animation is played.

Animation Curve animation speed curve, can be customized 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 angle of rotation 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, which is the coordinate system under Ngui, where the coordinate values of the to are not set, which are set in the code later.


Create a Tweentest script file in the Project window with the following code:

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 common 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> ();

Sets 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 window tween Position hook select Remove The script name there check box, so Tween position will not execute, manual play required
Tweenp.play ();
}


void scoremovefinished () {
score + = add;
Scorelabel.text = "" + score;
Destroy (Addscore);
}
}

The key code is tweenp.to = transform. Inversetransformpoint (scoreLabel.transform.position), because the To,from value of tween position is based on the Ngui coordinate system, and what I can now get 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.


Then add the script to the UI root, after running, click on the button will randomly generate a score in the middle, and then will quickly rotate and move to the position of the total, and the total score accumulated.




Unity Plugin Ngui Learning (9)--tween and world coordinate system dimensions converted to Ngui dimensions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.