赞
踩
层级面板点击右键-UI-slider
选择2D视图,调整锚点和滑动条位置
导出2个图层,PNG格式。
把两个PNG导入Unity仓库中,选中两个图,右上角从切换为sprite
选中Fill ,修改Sourceimage
- 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);
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
你需要另一个类里面的血量变量!附赠另一个管理分数和血量的脚本
- 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));
- }
-
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。