[Unity][UGUI]滑动条来回滑动在触发范围响应事件,武器命中率滑动条触发判定


想要实现的功能

[Unity][UGUI]滑动条来回滑动在触发范围响应事件,武器命中率滑动条触发判定_第1张图片

实际效果:

[Unity][UGUI]滑动条来回滑动在触发范围响应事件,武器命中率滑动条触发判定_第2张图片


[Unity][UGUI]滑动条来回滑动在触发范围响应事件,武器命中率滑动条触发判定_第3张图片


using UnityEngine;
using UnityEngine.UI;

public class Test_Slider : MonoBehaviour {

    public Text Slider_Text;//滑块的 值的UI显示        //using UnityEngine.UI;
    public Slider Slider;//滑块 物体        //using UnityEngine.UI;
    public int SliderValue;//滑块的值
    public float SliderSpeed;//滑块的 滑块速度
    public Image Slider_Handle;//滑块的 滑块        //using UnityEngine.UI;


    public Color Slider_Normal;//滑块普通的颜色
    public Color Slider_On;//滑块在Rang范围内的颜色

    public Color color1;
    public Color color2;

    public Image Image_Red1;        //using UnityEngine.UI;
    public Image Image_Red2;        //using UnityEngine.UI;
    public Image Image_Blue;        //using UnityEngine.UI;

    private int max;
    private int RangMax;//
    private int RangMin;//
    private int min;

    public int Style;//变换的类型


    // Use this for initialization
    void Start () {
        Style = 1;
        SliderSpeed = 0.1f;
        Slider.value = 1;


        #region 判定条_Steart

        max = 100;
        min = 0;
        RangMax = 80;
        RangMin = 70;



        //颜色初始化
        Image_Red1.color = color1;
        Image_Red2.color = color1;

        Image_Blue.color = color2;

        Slider_Handle.color = Slider_Normal;//


        //
        Image_Red1.fillAmount = (float)max / max;
        Image_Blue.fillAmount = (float)RangMax / max;
        Image_Red2.fillAmount = (float)RangMin / max;

        
        #endregion


    }

    // Update is called once per frame
    void Update ()
    {
        #region 滑块

        //更新 滑块UI 的值
        Slider_Text.text = Slider.value.ToString();


        if (Style == 1)
        {
            Slider.value = (Slider.value -= SliderSpeed * Time.deltaTime) * Slider.value / (Slider.value);
        }
        else if (Style == 0)
        {
            Slider.value = (Slider.value += SliderSpeed * Time.deltaTime) * Slider.value / (Slider.value);
        }


        if (Slider.value <= SliderSpeed/10 && Style == 1)
        {
            Style = 0;
            Slider.value = 0;
        }
        else if (Slider.value >= (1- SliderSpeed / 10) && Style == 0)
        {
            Style = 1;
            Slider.value = 1;
        }


        //判定 滑块 在 判定范围内 就变色Slider_On,不在就 恢复正常的颜色Slider_Normal
        if (Slider.value > Image_Red2.fillAmount 
            && Slider.value < Image_Blue.fillAmount)
        {
            Slider_Handle.color = Slider_On;
            if (Input.GetKeyDown(KeyCode.Space))//按下空格键,就可以在 判定范围内响应事件
            {
                DoSomething();

            }
        }
        else
        {
            Slider_Handle.color = Slider_Normal;
        }
        
        #endregion

    }

    private void DoSomething()
    {
        Debug.Log("   DoSomething    ");
    }


}





[Unity][UGUI]滑动条来回滑动在触发范围响应事件,武器命中率滑动条触发判定_第4张图片




相关文章:

1.[Unity][特效][UGUI]HUD围绕角色头顶的血条特效

2.

3.

4.

5.



你可能感兴趣的:(Unity,UGUI,Unity特效)