Use of Unity3d NGUI (5) (UISprite & amp; UISlider)

Source: Internet
Author: User

You can use NGUI to create a colored role blood bar and load the progress bar.

When creating a blood strip, you can display different colors based on the amount of blood, and you can simply encapsulate UISider and UILabel.



UISprite: NGUI genie image component


Atlas: image sets

Sprite: Image in the selected image set

Sprite Type: Simple (scale the image to the specified size without processing it), Sliced (small-sized image to fit the size)

Tiled (fill the area in the form of bricks without scaling the image), Filled (fill area), Advacced (advanced, customizable edge pixels)

If it is a small image, you need to modify the sprite type to achieve the effect.


Make color slide:

A. Add a Progress Bar in the Widget Tool. a UISider (NGUI compent) is added by default)


Value: Percentage

Alpha: Transparency

Steps: Step threshold

Appearance: Features

A. Foreground (Foreground image)

B. Background (Background image)

C. Thumb (mobile indicator)

D. Direction (slide Direction)

On Value Change: Event distribution when sliding

B. Add a Script on the Progress Bar to change the color of the Progress.

public class UISliderColors : MonoBehaviour{public UISprite sprite;public Color[] colors = new Color[] { Color.red, Color.yellow, Color.green };UIProgressBar mBar;void Start () { mBar = GetComponent<UIProgressBar>(); Update(); }void Update (){if (sprite == null || colors.Length == 0) return;float val = mBar.value;val *= (colors.Length - 1);int startIndex = Mathf.FloorToInt(val);Color c = colors[0];if (startIndex >= 0){if (startIndex + 1 < colors.Length){float factor = (val - startIndex);c = Color.Lerp(colors[startIndex], colors[startIndex + 1], factor);}else if (startIndex < colors.Length){c = colors[startIndex];}else c = colors[colors.Length - 1];}c.a = sprite.color.a;sprite.color = c;}}
When Sprite is specified as the foreground image of the current Progress Bar

E. Create a UILabel in the current Prgoress Bar to distribute the event to the UILabel

Test code:

public class Tt : MonoBehaviour {private UISlider slider;// Use this for initializationvoid Start () {slider = GetComponent<UISlider>();slider.value = 0;}// Update is called once per framevoid Update () {if(slider!=null) {Debug.Log(slider.value);slider.value += 0.0005f;}}}



Use of Unity3d NGUI (5) (UISprite & UISinder for colored blood strips)

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.