Unity UGUI用Slider制作血条

Unity UGUI用Slider制作血条

众所周知:unity的Slider组件(滑动条)是制作的;
但是有经验的就会发现,Slider组件做血条的话,虽然也可以实现,但是效果总是差强人意。所以我们开发的时候,一般都是让美术小姐姐,做精美得图片然后用Image来实现血条的效果。

具体操作如下:
首先新建一个Image,并把名字改为HP;
设置好的宽高,并添加Slider组件;如图
Unity UGUI用Slider制作血条_第1张图片
在HP下再新建两个子物体Image组件;
分别命名为bg(背景)和Fill(血条);
把颜色改为黑色和红色;
在Slider组件中把Maxvalue的值设置为血量值;
可以拉动Value来调节进度条测试效果;
效果如图:
Unity UGUI用Slider制作血条_第2张图片
Unity UGUI用Slider制作血条_第3张图片

然后就是写代码了:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

/// 
/// 进度条缓动效果
/// 
public class HPSlider : MonoBehaviour
{
    //private static HPSlider instance;


    //public float maxVlaue;
    // public int v;
    // public int initVlue;
    public int targetValue; //
    //public float smooting = 2;

    public static Slider slider;

    public bool start;

    //public static HPSlider Instance
    //{
    //    get
    //    {
    //        if (instance == null)
    //            instance = new HPSlider();

    //        return instance;
    //    }
    //}

    //private HPSlider() { }


    public delegate void CallBack();
    public CallBack back;
    public float timer = 1;

    Image fill;


    //停止
    public float stop;


    //执行次数
    public int index = 1;

    void Awake()
    {
        slider = this.GetComponent<Slider>();
        slider.wholeNumbers = false; //float过渡
        fill = this.transform.Find("Fill").GetComponent<Image>();
        //maxVlaue = slider.maxValue;
        //initVlue = 0; //初始化值
        index = 1;
        timer = 8; //最大值
        fill.fillAmount = 0;
        //stop = 1f / slider.maxValue;


    }
    void Start()
    { }
    public float curr = 1;
    void Update()
    {
        //Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, Time.deltaTime * smooting)&& targetValue != slider.value;

        if (start)
        {
            timer -= Time.deltaTime;

            //slider.value = Math.Abs((targetValue - timer) / targetValue);

            //if (targetValue <= slider.value)
            //{
            //    //timer = 1;
            //    start = false;
            //    curr = targetValue;
            //    //slider.value = targetValue;
            //    if (back != null)
            //        back();
            //}
            stop = ((float)Math.Round(1f / slider.maxValue, 2));
            fill.fillAmount = (8 - timer) / 8;

            float ab = fill.fillAmount;
            if (ab > stop * index)
            {
                index++;
                start = false;
                if (back != null)
                    back();
            }

            if (fill.fillAmount == 1)
            {
                index = 1;
                start = false;
                timer = 8;
            }
            //时间到
            //if (timer <= 0)
            //{
            //    timer = 1;
            //    start = false;
            //    curr = targetValue;
            //    //slider.value = targetValue;
            //    if (back != null)
            //        back();
            //}
        }
    }

    public int TargetValue
    {
        set
        {
            targetValue = value;
        }
    }

    public bool Starts
    {
        set
        {
            start = value;
        }
    }

    public CallBack Callback
    {
        set
        {
            back = value;
        }
    }
}

你可能感兴趣的:(开发工具的整理学习,unity,c#)