unity uGui绑定事件

Button

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

public class LearnUIButton : MonoBehaviour
{
    public Button button;
    public void Awake()
    {
        button.onClick.AddListener(OnButtonClick);
    }


    public void OnButtonClick()
    {
        Debug.Log("按钮被点击了");
    }

}

Toggle

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

public class LearnToggle : MonoBehaviour
{
    public Toggle toggle;
    public void Awake()
    {
        toggle.onValueChanged.AddListener((this.OnToggleValueChange));
    }
    public void OnToggleValueChange(bool isOn)
    {
        Debug.Log("ToggleisOn:" + isOn);
    }
}

Slider

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

public class LearnSlider : MonoBehaviour
{
    // Start is called before the first frame update
    public Slider slider;
    public void Awake()
    {
        slider.onValueChanged.AddListener(this.OnSlideValueChanged);
    }
    public void OnSlideValueChanged(float f)
    {
        Debug.Log("slideValue:" + f);

    }

}

Dropdown

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

public class LearnDropdown : MonoBehaviour
{
    public Dropdown dropdown;
    public void Awake()
    {
        dropdown.onValueChanged.AddListener(this.OnValueChanged);
    }
    public void OnValueChanged(int value) {
        Debug.Log("DropdownValue:" + value);
    }

}

Scrollbar

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

public class LearnScrollBar : MonoBehaviour
{
    public Scrollbar scrollbar;
    public void Awake()
    {
        scrollbar.onValueChanged.AddListener(this.OnValueChanged);
    }
    public void OnValueChanged(float f)
    {
        Debug.Log("srcollBarValue:" + f);
    }

}

为每个UI分别添加C#脚本,然后每个ui比如dropdown绑定代码中public Dropdown dropdown
在这里插入图片描述
然后play
分别操作相应控件,控制台就会打印出相应信息
如下
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值