Slider Bar

前置准备

Canvas 设定请参照先前文章的「Canvas 设定」环节: 新建一个 UI 视窗

建立一个 Slider (拖曳杆)

「阶层」视窗按下滑鼠右键,「UI」「Slider」

将 Slider 拉长点

设定 Slider 的属性

  • Min Value – 拖曳杆的最小数值
  • Max Value – 拖曳杆的最大数值
  • 整数 – 代表出现的数字只能是整数
  • Value – 预设的数值

再建立一个「文字」物件用来显示数值,并设定一下文字属性(颜色、大小)

显示效果

建立一个脚本「SliderValueToText」

编辑脚本并且加入以下程式码

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

public class SliderValueToText : MonoBehaviour
{
    public Slider sliderUI;
    private Text textSliderValue;
    // Start is called before the first frame update
    void Start()
    {
        textSliderValue = GetComponent<Text>();
    }
    public void ShowSliderValue()
    {
        string slidermessage = "Slider Value = " + sliderUI.value;
        textSliderValue.text = slidermessage;
    }
    // Update is called once per frame
    void Update()
    {
        ShowSliderValue();
    }
}

储存脚本

将脚本拉进去「Text」物件底下

将「Slider UI」设定为「Slider」

完成,看看结果

SHXJ
Latest posts by SHXJ (see all)

在〈Slider Bar〉中有 1 则留言

发布留言