zoukankan      html  css  js  c++  java
  • Unity 实现进度条

    (本次实现,底框图片的进度条满。如果底框图片进度条空,实现方法一样,只是相应锚点方向会相反。)

    效果预览

    放上进度条的底框

    添加Mask

    设置Pivot在最右边,使得Mask固定在右边,当Mask宽度缩短时,内容是从左边露出来。

    叠加空进度条

    关闭Mask的 “Show Mask Graphic”。

    现在你从左边拖拽Mask的宽度,会看到底部的绿色进度显示出来。

    所以,关键是控制 Mask的宽度就可以实现进度条了。

    新建脚本 UIQuestProgress.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    
    [ExecuteInEditMode]
    public class UIQuestProgress : MonoBehaviour
    {
        public Image mask;
        public float value;
        float originalSize;
    
    
        // Start is called before the first frame update
        void Start()
        {
            originalSize = mask.rectTransform.rect.width;
        }
    
    
        // Update is called once per frame
        void Update()
        {
            setValue(value);
        }
    
    
        /// <summary>
        /// 完成进度。 取值范围 0~1.0
        /// </summary>
        void setValue(float value)
        {
            var unfinishValue = Mathf.Max(0, 1.0f - value);
           
            mask.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, originalSize * unfinishValue);
        }
    }
    

    应用UIQuestProgress.cs脚本

    修改value就可以看到进度条变化了。

  • 相关阅读:
    day 12 元组的魔法
    day 12 列表的魔法,及灰魔法
    day 11 Python课上练习解释与基础知识练习题试题一
    day 11 rang的用法和练习
    day 10 字符串的魔法
    day1 Python可变与不可变类型
    day1 数据类型
    Math对象
    Calendar对象
    Date对象
  • 原文地址:https://www.cnblogs.com/ZJT7098/p/Unity.html
Copyright © 2011-2022 走看看