UGUI之Slider使用,制作血条
2015-10-01 19:35 糯米粥 阅读(4174) 评论(0) 编辑 收藏 举报用Slider来控制Cube旋转
Slider是滑动条。
效果:
就是就是获取Slider的value值。但这里我们用动态改变
先看看Slider的事件:
当改变value的时候触发。
现在写代码
using UnityEngine; using System.Collections; public class cube : MonoBehaviour { public float speed; // Use this for initialization void Start() { } // Update is called once per frame void Update() { transform.Rotate(Vector3.up * Time.deltaTime * speed); } public void c(float i) { speed = i; } }
public void c(float i)就的动态改变值。这里参数类型必须是float,
你会发现 “Dynamic float”下面显示了方法名
仔细观察。你会发现。Button对象会有Button组件。同样Slider也会有Slider组件
所以。slider组件可以添加到任何对象上。当对象添加了slider组件。那么该对象就有滑动功能了。如果用UGUI自带的slider对象来做血条是可以实现。但效果并不优美。所以可以用美工处理好的图片来做血条
比如:
可以看出来。这里有3张图片。
1:底图(Image1)
2:背景图片(background)
3:前景图片(Fill)
给Image1对象添加Slider组件,把Fill拖拽到Fill Rect
把Fill中的Image组件修改
这样就能从右像左移动
还需要取消鼠标跟Image1的交互。既不能用鼠标修改。只能通过代码修改Value的值
这样就完成了血条的UI。然后就是代码修改血条的血量了
游戏中血条都会慢慢减少。有一个缓动效果。慢慢过渡

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using UnityEngine; 6 using UnityEngine.UI; 7 /// <summary> 8 /// 进度条缓动效果 9 /// </summary> 10 public class progressbarSlow : MonoBehaviour 11 { 12 //private static progressbarSlow instance; 13 14 15 //public float maxVlaue; 16 // public int v; 17 // public int initVlue; 18 public int targetValue; // 19 //public float smooting = 2; 20 21 public static Slider slider; 22 23 public bool start; 24 25 //public static progressbarSlow Instance 26 //{ 27 // get 28 // { 29 // if (instance == null) 30 // instance = new progressbarSlow(); 31 32 // return instance; 33 // } 34 //} 35 36 //private progressbarSlow() { } 37 38 39 public delegate void CallBack(); 40 public CallBack back; 41 public float timer = 1; 42 43 Image fill; 44 45 46 //停止 47 public float stop; 48 49 50 //执行次数 51 public int index = 1; 52 53 void Awake() 54 { 55 slider = this.GetComponent<Slider>(); 56 slider.wholeNumbers = false; //float过渡 57 fill = this.transform.Find("Fill").GetComponent<Image>(); 58 //maxVlaue = slider.maxValue; 59 //initVlue = 0; //初始化值 60 index = 1; 61 timer = 8; //最大值 62 fill.fillAmount = 0; 63 //stop = 1f / slider.maxValue; 64 65 66 } 67 void Start() 68 { } 69 public float curr = 1; 70 void Update() 71 { 72 //Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, Time.deltaTime * smooting)&& targetValue != slider.value; 73 74 if (start) 75 { 76 timer -= Time.deltaTime; 77 78 //slider.value = Math.Abs((targetValue - timer) / targetValue); 79 80 //if (targetValue <= slider.value) 81 //{ 82 // //timer = 1; 83 // start = false; 84 // curr = targetValue; 85 // //slider.value = targetValue; 86 // if (back != null) 87 // back(); 88 //} 89 stop = ((float)Math.Round(1f / slider.maxValue, 2)); 90 fill.fillAmount = (8 - timer) / 8; 91 92 float ab = fill.fillAmount; 93 if (ab > stop * index) 94 { 95 index++; 96 start = false; 97 if (back != null) 98 back(); 99 } 100 101 if (fill.fillAmount == 1) 102 { 103 index = 1; 104 start = false; 105 timer = 8; 106 } 107 //时间到 108 //if (timer <= 0) 109 //{ 110 // timer = 1; 111 // start = false; 112 // curr = targetValue; 113 // //slider.value = targetValue; 114 // if (back != null) 115 // back(); 116 //} 117 } 118 } 119 120 public int TargetValue 121 { 122 set 123 { 124 targetValue = value; 125 } 126 } 127 128 public bool Starts 129 { 130 set 131 { 132 start = value; 133 } 134 } 135 136 public CallBack Callback 137 { 138 set 139 { 140 back = value; 141 } 142 } 143 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?