Unity 制作血量滑动条(Slider)

1.创建UI slider

层级面板点击右键-UI-slider

2.调整UI位置

选择2D视图,调整锚点和滑动条位置

 3.PS中制作UI

导出2个图层,PNG格式。

4.改成精灵模式(sprite2d)

把两个PNG导入Unity仓库中,选中两个图,右上角从切换为sprite

5.选中左边background,修改滑动条背景图

 选中Fill ,修改Sourceimage

6.用脚本把血量的数据赋值给滑动条的value 

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

public class ManagerUI : MonoBehaviour
{
    // 这个脚本主要管理UI
    public Slider oneSlider;
    void Start()
    {
//初始化滑动条的最大数值为10000,这个是为了与另一个脚本的变量保持一致
        oneSlider.maxValue = 10000;
        oneSlider.value = 10000;//这个是滑动条当前的数值,随着这个数值的变化,滑动条自己会滑动
    }

    // Update is called once per frame
    void Update()
    {
        do
        {
            if (oneSlider == null)
            {
                Debug.Log("滑动条为空");
                break;
            }
//从另一个类中拿到血量变量实时赋值给滑动条的当前数值
            oneSlider.value = ScoreManager.CurrentBlood;

        } while (false);
    }
}

你需要另一个类里面的血量变量!附赠另一个管理分数和血量的脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScoreManager : MonoBehaviour
{
    //分数管理 血量管理
    // 如果撞击到金币就加分,如果撞击到障碍物就掉血

    public static int CurrentScore=0;
    public static int CurrentBlood = 10000;

    private void Start()
    {
        CurrentScore = 0;//初始化
        CurrentBlood = 10000;//初始化
    }

    public  static void ScoreAdd()
    {
        Debug.Log("加分函数开始执行");
        CurrentScore += 10;
        Debug.Log("分:"+CurrentScore);
    }

    public static void SubBlood()
    {
        Debug.Log("掉血函数开始执行");
        CurrentBlood -= 100;
        Debug.Log("血:" + CurrentBlood);
    }

    private void OnGUI()
    {
        Rect oneLableRec = new Rect(100, 100, 50, 50);
        GUILayout.Box(CurrentBlood.ToString(), GUILayout.Width(200), GUILayout.Height(50), GUILayout.ExpandWidth(false)); 
        // 创建另一个矩形框,背景色为红色,边框宽度为3像素
        GUILayout.Box(CurrentScore.ToString(), GUILayout.Width(200), GUILayout.Height(70), GUILayout.ExpandWidth(false));
        GUILayout.TextField("欢迎来到跑酷小将", GUILayout.Width(200), GUILayout.Height(50), GUILayout.ExpandWidth(false));
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity3d青子

难题的解决使成本节约,求打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值