[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
说起游戏开发,大家一般会觉得控制角色移动和制作血条哪个难呢?
或许都会觉得血条比较难吧。
是的,正是如此。
那么我们让我们来看看血条该怎么做吧
这是效果图
受伤后是这样的
首先是创建一张Canvas画布
这个画布会很大
相比之下我们的小地图就显得微不足道了
随后我们创建两个image元素
将图片放入source image中就行了
我这里图方便就直接把头像作为子元素挂载在这个状态栏上了
注意先后排布好就行
随后
我们要做的是
创建一个image对象 ,然后搜索Mask组件
变为Mask,然后我们创建子对象image这个是血条我们就叫它Health Bar
然后会是这个状态
注意 蓝色血条里image中的image Type改为Simple,并且我们的锚点要设置在左上角,在Rect Transform中设置
这样血条才不会随着父对象变换位置,也不会随着大小的变化而改变形状
操作如下
选中最左上角
注意我们的Mask只起遮罩作用,是不显示的
所以我们应该将一个设置关掉,也就是这里
随后就是代码环节了,前面乱七八糟的很杂,但是代码倒不复杂
using System.Collections; using System.Collections.Generic; using UnityEngine;
using UnityEngine.UI; public class MyUIHealthBar : MonoBehaviour { // Start is
called before the first frame update public static MyUIHealthBar instance {
get; private set; }//static将该变量设为全局变量,所有类都可调用 public Image mask;// float
originalSize; private void Awake() { instance = this;//一开始就将instance赋上值 } void
Start() { originalSize = mask.rectTransform.rect.width;//存Mask的宽度 } // Update
is called once per frame void Update() { } public void SetValue(float value) {
mask.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,
originalSize * value);//这里是修改值的函数,到Rubycontroller脚本会直接调用 } }
下面是Ruby的代码
MyUIHealthBar.instance.SetValue(currentHealth / (float)maxHealth);
就这么一行,是的,就这么一行
因为我们的static已经将instace公开了,人人都可以调用,所以我们只需要附上类名就可以直接调用这个控制遮罩宽度的函数了,通过控制Mask遮罩,太达到一个血条增减的效果。