One of Ngui learning Sprite animation playback and slider use __u3d

Source: Internet
Author: User
Tags try catch

First import the Ngui package and restart the project to make sure that the Ngui menu option appears in the menu bar.


I. Production of Atlas (map).

Atlas is a big picture, a collection of many small graphs called Sprite (sprites) by setting the size of the small graph to intercept.

Set up the images folder under the Assets folder and drag the pictures you want to use in the animation as shown here:


Select all pictures, menu bar Ngui-> Atlas Maker-> give your own Atlas name runner-> Create.

A mat file, a prefab file, and a PNG picture file are automatically generated after Atlas is established.


Two. Create a Ngui camera

First remove the original main Camera, menu Ngui-> Create a New UI-> create your UI.

Ngui will automatically create a UI Root that contains a camera.

Three. Establishment of Sprite

Menu Ngui-> Create a Widget.

If you choose a runner in Atlas that is not the default, drag the Runner.prefab file we just made here.

-> Template dropdown Box select Sprite-> sprete inside the "image49" is the start wizard picture-> Add to Panel.



After adding, a sprite (image 49) appears on the panel, changing the name to Runnersprite.

Some of the specific properties of Atlas can be obtained by capturing the Uisprite component on the runnersprite, such as the number of wizards in Atlas, and the name of each wizard. The code is as follows:

	Uisprite UI = Sprite. Getcomponent<uisprite> ();
	Ui.spritename = Ui.atlas.spritelist[0].name;     Sets the starting wizard name
	int spritecount = Ui.atlas.spriteList.Count for Atlas     ; Get the number of wizards in Atlas   

The sprite is the gameobject of the "Runnersprite" obtained in the Start ().

OK, the wizard added to complete, but still dead on the screen, no animation, how to let the Elves move up.

Add a uispriteanimation component to the sprite to make it move.

Component-> NGUI-> UI-> Sprite. Add complete Discovery Sprite a component with the following figure:

The Framerate property is the playback rate, which is the number of frames per second, by adjusting this property can adjust the speed of the characters running.


Four. Create Ngui button and dynamic add Uispriteanimation component

According to the above steps have been able to run the wizard, the following to complete is through a button to control the wizard running or stop.

Create a Ngui button.

NGUI-> creat a Widget. Here's the button style we choose Ngui built-in fantasy style, open folder in Project view

Assets/ngui/examples/atlases/fantasy/As shown in the picture:


Drag fantasy Atlas onto the Widget tool window on Atlas, fantasy font onto font,

Select the template for the button in the template Drop-down box, background is the background style of the buttons, select window.

Add to Penal, Button added successfully, adjust position. The button is renamed "RUN" under the Uilabel component of the button's Label,inspector view in the hierarchy view, as shown below:


The following button is added for listening.

Set up C # script runtest, the Listener is onclick (), we drag the script onto the button and call this function if the Click event occurs. In this function we decide whether to add uispriteanimation to Runnersprite by judging the state of Isplayanimation, as follows:

Using Unityengine;
Using System.Collections;

public class Runtest:monobehaviour {
	
	private bool isplayanimation = false;      Do not play animation
	Gameobject sprite = null initially;
	Use the for initialization
	void Start () {
		sprite = gameobject.find ("Runnersprite");
	}
	
	void OnClick ()
	{if
		(isplayanimation)      //If True to run, destroy Uispriteanimation component
		{
			Debug.Log ("Stop");
			Isplayanimation = false;
			Destroy (Sprite. Getcomponent<uispriteanimation> ());
			Uisprite UI = Sprite. Getcomponent<uisprite> ();
			Ui.spritename = Ui.atlas.spritelist[0].name;      After stopping playback, place the sprite picture as the first
		}
		else      //If False is the stop state, add uispriteanimation to make it move
		{
			Debug.Log ("Run ");
			Isplayanimation = true;
			Uispriteanimation Uianim = Sprite. Addcomponent<uispriteanimation> ();
			Uianim.framespersecond =;      Set the speed of Playback}}}

Done, now click Run to control the Runnersprite running and stop.


Five. Add slider control Elf's running speed

As with adding a button, add a widget, the template selection slider,background and foreground refers to the background of the slider drag bar, select Window,thumb is the drag pointer selection dark, About style can try to like it yourself.

Adjust the slider position:

Next, add a listener script for slider. Establish C # script Speedbar. Slider's listener function is Onsliderchange (float value), which is invoked after the slider thumb change (note: It is called once at the start of the program, which brings a null reference exception, which requires try...catch processing). Drag the Speedbar onto the slider object.

The problem to be noted is that the event recv must be set to receive the object as slider itself, or the event will not be invoked.


The script code is as follows:

Using System;      The namespace using Unityengine needed to be introduced in exception handling
;
Using System.Collections;


public class Speedbar:monobehaviour {
	
	gameobject sprite = null;
	Uisprite uisprite = null;
	Uispriteanimation Spriteanim = null;
	
	
	Use the for initialization
	void Start () {
		sprite = gameobject.find ("Runnersprite");
	}
	
	void Onsliderchange (float value)
	{
		try{
			Spriteanim = Sprite. Getcomponent<uispriteanimation> ();      It is possible to be empty and requires exception handling
		}
		catch (NullReferenceException e) {
			Debug.Log (e.tostring ());
		}
	
		Debug.Log (value);
		if (spriteanim!= null)      //determine if NULL
		{
			if (int) (value *) = 0)      ///Process boundary bug problem
			{
				Spriteanim.framespersecond = 1;
			}
			else
			{
				spriteanim.framespersecond = (int) (value *);
			}
			
			Debug.Log ("Framespersecond =" + Spriteanim.framespersecond);}}


The idea is to trigger the Onsliderchange function when the slider is sliding, and the value is passed in, and the value range is 0.0f~1.0f, and we already have the Uispriteanimation component on the runnersprite in start. Multiply the value by 50, get the refresh rate per second, and assign a value to the Framepersecond of the uispriteanimation component.

The problem to be noted is that when Uispriteanimation is acquired, a null reference appears because it is possible that the runnersprite is in a stop state and the animation object has been destroyed. If a null reference results in a program pause, the error pause can be turned off on the panel to keep the program running.

But here we need to deal with the exceptions that might occur. Use try catch for processing, error:

Assets/scripts/speedbar.cs (23,23): Error cs0246:the Type or namespace name ' NullReferenceException ' could ' not being found. Are you missing a using directive or a assembly reference?

This is because when the C # script is generated, the system's namespace reference is not automatically included, plus the using system;

The Spriteanim is then judged to be empty and not NULL to assign the above Framespersecond. If you go directly to the int assignment with VALUE*50, you will get a bug that the sprite's animation will become very fast after the int value is 0 assigned to Framespersecond. Then you need to set a boundary, if the int value ==1, so that the Framespersecond is 1, thus resolving the bug.


Well, the first technical article on this, there is a shortage of hope that we can put forward, mutual exchange of learning.


SOURCE Engineering and resources download click Open link


Executable file Download Click Open link



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.