Unity3D game development 18 NGUI Animation

Source: Internet
Author: User

Let's first look at the next frame Animation, as its name implies, which is an Animation composed of frames. We need to use the UISprite Animation component. Its Attributes are as follows:


Framerate: the playback rate, that is, the number of frames played per second.

Name Prefix: the Prefix of the image Name. It can be used to filter images in the image set to specify the image you need.

Loop: Loop

Follow the steps for creating the UI in the previous article to create a sprite and a button to control the playback and pause of the animation by clicking the button. OK. First, create an Atlas and put the required materials in it, for example:


Click Create to Create Atlas, set the sprite image of sprite to the first frame, and Create a new button. The project structure is as follows:



Then we create a new c # script to control the animation. The Code is as follows:

Using UnityEngine; using System. collections; public class TestRun: MonoBehaviour {private bool isAnimation = false; GameObject sprite = null; // Use this for initializationvoid Start () {GameObject play = GameObject. findGameObjectWithTag ("Player"); UIEventListener. get (play ). onClick = playClick; sprite = GameObject. find ("RunnerSprite");} // Update is called once per framevoid Update () {} void playClick (GameObject button) {if (isAnimation) {isAnimation = false; // xiaohuiDestroy (sprite. getComponent <UISpriteAnimation> (); UISprite us = sprite. getComponent <UISprite> (); // after stopping playing the video, set the sprite image to the first us image. spriteName = us. atlas. spriteList [0]. name;} else {Debug. log ("---- 11111"); isAnimation = true; UISpriteAnimation an = sprite. addComponent <UISpriteAnimation> (); // sets the playback speed. framesPerSecond = 20 ;}}}
Then bind the script to the button and run it, for example:


Next let's take a look at the Tween library. As the name suggests, it is actually some animations such as translation, rotation, and scaling. Let's take a look at the animations of Tween, such:


We add a tween component to the button. The attributes are as follows:


Right-click the widget and select the appropriate option in the Tween menu to add the tween function to it. You can also use the NGUI menu to add a new one. Alternatively, you can use the name to mount the tween script.
The newly added tween is active by default. Therefore, after you click Play, the tween will run. If you do not want to run the script, check the check box in the script name.
All tweets inherit from the same base class (UITweener) and have the same basic functions. From and To indicate the start and end values. Animation Curve is used to tune the variation process of these two values.
Duration specifies the Duration of an animation. Start Delay. In seconds.
The Tween Group can control the trigger of multiple tweets on the same object. Specifies the group ID during playback.
You can call the Play () function of tween by specifying the OnClick part of the button.

Upload a UIPlayTween Script (Attach-> Play Tween Script) to implement advanced functions. More play options are available. The attributes are as follows:


First, specify the Tween Target. This game object requires at least one tween to be triggered. Include Child to activate all Child nodes of this object.

Similar to the above tween, the Tween Group allows you to selectively activate the tween.

Trigger condition specifies the action that triggers tween. Play ction determines the play mode. If the target is disabled, You can activate it or retain it as it is (so that the tween contained in the object you intentionally set to disabled will not be played immediately ).

If tween is playing, you can select restart to enable it to play from the beginning, continue to play, or play after the previous play. You can disable target after setting tween. It is useful when switching menus.

Finally, if you want to call other function functions at the end of the tween, drag a game object to the policy attribute and select the desired function. Like all NGUI notifications, the function must be declared as "public void FuncName (void.




Related Article

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.