"Unity Ngui Game Development ii" tweenposition Displacement Animation (i): displacement animation not relative to anchor

Source: Internet
Author: User
<span id="Label3"></p><p><p>Below to learn some of the next Ngui tweenposition displacement animation, The general displacement animation requirements are divided into the anchor anchor point displacement animation and relative to the anchor displacement animation (mainly to consider whether the screen resolution), Here are some of the most common tweenposition usages of the two games that are not relative to Anchor:</p></p><p><p>Usage 1. Ngui controls move from the Posa position to the POSB position to play the animation</p></p><p><p>Usage 2. In the game you need to dynamically create an object with Tweenposition component animation, object creation, movement, arrival at the specified location, destruction process. Eg. in the game, players eat gold coins, after late gold converted to fractions, scores play a tweenposition component animation, from the player position to the score label position, after the score is Destroyed.</p></p><p><p><br></p></p><p><p><strong>Usage 1. Ngui controls move from the Posa position to the POSB position to play the animation</strong></p></p><p><p><br></p></p><p><p>This is easiest to use, set up a location, play the animation<br></p></p><p><p><br></p></p><p><p><br></p></p><p><p>As follows:</p></p><p><p><br></p></p><p><p>You can hide the control and start the animation in the Program:</p></p><p><p></p></p><pre name="code" class="csharp"><pre name="code" class="csharp">tweenposition TP = tweenobject.getcomponent<tweenposition> (); TP. Playforward ();</pre></pre><p><p></p></p><p><p><br></p></p><strong><strong>Usage 2. In the game you need to dynamically create an object with Tweenposition component animation, object creation, movement, arrival at the specified location, destruction process. Eg. in the game, players eat gold coins, after late gold converted to fractions, scores play a tweenposition component animation, from the player position to the score label position, after the score is Destroyed. </strong></strong><p><p><strong><br></strong></p></p><p><p><strong>As follows:</strong></p></p><p><p><strong><br></strong></p></p><p><p><strong><br></strong></p></p><p><p><strong>Several points to note:</strong></p></p><p><p><strong>1. To dynamically generate the fractional object, it needs to be made into a prefab, placed in the Resources/prefabs directory, scene scenes without backup</strong></p></p><p><p><strong>2.TweenPositon do not open, in the program to control the time to play the animation</strong></p></p><p><p><br></p></p><p><p><strong><br></strong></p></p><p><p><strong>Create a PlayerEatScore.cs, either on the Bind button or on the uiroot, and the code is as Follows:</strong></p></p><p><p><strong></strong></p></p><pre name="code" class="csharp">Using unityengine;using system;using system.collections;public class Playereatcoin:monobehaviour {private GameObject addscoreprefab; ADD score Prefabprivate UILabel totalscorelabel; Total score labelprivate UIButton btn;private int totalscore; Total number of private int score; Added score void Awake () {//pre-created good common score Prefabaddscoreprefab = Resources.load ("prefabs/addscore") as gameobject;} void Start () {totalscore = 10000;totalscorelabel = Gameobject.find ("totalscore"). getcomponent<uilabel> (); totalscorelabel.text = totalscore.tostring (); btn = Gameobject.find (" Btnplayereatscore "). getcomponent<uibutton> ();//set button response function Eventdelegate.add (btn.onclick, addscore);} void Addscore () {//clone score Gameobjectgameobject addscoreclone = (gameobject) Instantiate (addscoreprefab, new Vector3 (0, 0, 0 ), transform.rotation); UILabel label = addscoreclone.getcomponent<uilabel> ();//random score Number score = UnityEngine.Random.Range (100, 300); Label.text = Score. ToString ();//get tweenposition Object tweenposition TP = Addscoreclone. getcomponent<tweenposition> ();//sets The coordinate value of the to, which is the value of the Ngui coordinate system, so you need to reverse the world coordinate value transform. Inversetransformpointtp.to = Transform. Inversetransformpoint (totalScoreLabel.transform.position);D Ebug. Log (tp.to.ToString ());//set The callback function after the end of the animation eventdelegate.add (tp.onfinished, () = {scoremovefinished (addscoreclone, score);}); /in the inspector window tween position check box removed the script name, so tween position will not execute, need to manually playtp.playforward ();} void scoremovefinished (gameobject label, int Sc) {Debug.Log ("-----callback score:" + sc); Totalscore + = Sc;totalscorelabe L.text = totalscore.tostring ();//use The function of the pass and tweenposition.current as if (label! = TweenPosition.current.gameObject) { Debug.Log ("Param Object Not equal tweenposition.current!");} Destroys the object that plays the animation Destroy (TweenPosition.current.gameObject);//destroy (label.gameobject);}}</pre><p><p></p></p><p><p><strong><br></strong></p></p>The above code has several points to note:<p><p><strong>1. The precast body is dynamically generated, and the catalogue location of the Preform</strong></p></p><p><p><strong>The callback arguments for 2.TweenPositon animations can be resolved with a lambda function</strong></p></p><p><p><strong></strong></p></p><pre name="code" class="csharp"><pre name="code" class="csharp"><span style= "white-space:pre" ></span>eventdelegate.add (tp.onfinished, () = {ScoreMoveFinished ( addscoreclone, score);});</pre></pre><p><p>3. The Tweenposition.current object in the callback is the Tweenposition object of the current callback, which is not passed by argument, which is the usual use of the new Ngui.</p></p><p><p></p></p><pre name="code" class="csharp"><pre name="code" class="csharp"><span style= "white-space:pre" ></span>void scoremovefinished (gameobject label, int Sc) {//using the pass parameter Same as tweenposition.current function if (label! = TweenPosition.current.gameObject) {Debug.Log ("Param Object Not Equal tweenposition.current! ");} Destroys the object that plays the animation Destroy (TweenPosition.current.gameObject);//destroy (label.gameobject);}</pre></pre><br><br><p><p></p></p><br><br><p><p></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>"Unity Ngui Game Development ii" tweenposition Displacement Animation (i): displacement animation not relative to anchor</p></p></span>

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.