UGUI Slider

1.新建一个Cube命名为Player,在上面挂一个脚本命名为Player,脚本内容如下:

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using UnityEngine.UI;
 5 
 6 public class Player : MonoBehaviour {
 7 
 8     public float speed;
 9 
10     void Update ()
11     {
12         transform.Rotate(Vector3.forward * Time.deltaTime * speed);
13     }
14 
15     public void ChangeSpeed(float newSpeed)
16     {
17         this.speed = newSpeed;
18         print(speed);
19     }
20 }

2.新建一个Slider,

  改变Direction可以改变Slider的方向,MinValue和MaxValue可以改变最大值最小值,WholeNumbers可以控制      Value的值是不是为整数.   设置成图示那样.

3.在OnValueChange那添加一个事件,把Player拖进来在后面选择Player.ChangeSpeed,那时ChangeSpeed上面有个灰色不可选的  Dynamic float,指的就是拖动Slider时Value的值,在拖动时会把Value的值传给ChangeSpeed的参数也就是newSpeed.

4.运行,会发现打印的speed的值跟Value值一样,拖动Slider控制Player的旋转速度.

原文地址:https://www.cnblogs.com/Peng18233754457/p/8250793.html