UGUI注册组件回调函数

用工具类让Button、Toggle等组件更好的添加不带自定义参数和带自定义参数的UnityAction回调。
/// 
    /// 添加按钮点击事件
    /// 
    /// 
    /// 
    public static void AddOnClickHandler(this Button btn, UnityAction action)
    {
        if (btn != null)
        {
            btn.onClick.RemoveAllListeners();
            btn.onClick.AddListener(action);
        }
    }
    /// 
    /// 添加带参数的按钮点击事件
    /// 
    /// 
    /// 
    /// 
    /// 
    public static void AddOnClickHandler(this Button btn, UnityAction action, T t)
    {
        if (btn != null)
        {
            btn.onClick.RemoveAllListeners();
            btn.onClick.AddListener(() =>
            {
                action(t);
            });
        }
    }
    /// 
    /// 添加Toggle值修改事件
    /// 
    /// 
    /// 
    public static void AddOnValueChangedHandler(this Toggle tog, UnityAction action)
    {
        if (tog != null)
        {
            tog.onValueChanged.RemoveAllListeners();
            tog.onValueChanged.AddListener(action);
        }
    }
    /// 
    /// 添加带参数的Toggle值修改事件
    /// 
    /// 
    /// 
    /// 
    /// 
    public static void AddOnValueChangedHandler(this Toggle tog, UnityAction action, T t)
    {
        if (tog != null)
        {
            tog.onValueChanged.RemoveAllListeners();
            tog.onValueChanged.AddListener(p =>
            {
                action(p, t);
            });
        }
    }
    /// 
    /// 添加Slider值修改事件
    /// 
    /// 
    /// 
    public static void AddOnValueChangedHandler(this Slider slider,UnityAction action)
    {
        if (slider != null)
        {
            slider.onValueChanged.RemoveAllListeners();
            slider.onValueChanged.AddListener(action);
        }
    }
    /// 
    /// 添加带参数的Slider值修改事件
    /// 
    /// 
    /// 
    /// 
    /// 
    public static void AddOnValueChangedHandler(this Slider slider, UnityAction action, T t)
    {
        if (slider != null)
        {
            slider.onValueChanged.RemoveAllListeners();
            slider.onValueChanged.AddListener(p =>
            {
                action(p, t);
            });
        }
    }
    /// 
    /// 添加dropdown值修改事件
    /// 
    /// 
    /// 
    public static void AddOnValueChangedHandler(this Dropdown dropdown, UnityAction action)
    {
        if (dropdown != null)
        {
            dropdown.onValueChanged.RemoveAllListeners();
            dropdown.onValueChanged.AddListener(action);
        }
    }
    /// 
    /// 添加带参数的dropdown值修改事件
    /// 
    /// 
    /// 
    /// 
    /// 
    public static void AddOnValueChangedHandler(this Dropdown dropdown, UnityAction action, T t)
    {
        if (dropdown != null)
        {
            dropdown.onValueChanged.RemoveAllListeners();
            dropdown.onValueChanged.AddListener(p =>
            {
                action(p, t);
            });
        }
    }
    /// 
    /// 添加InputField值修改事件
    /// 
    /// 
    /// 
    public static void AddOnValueChangedHandler(this InputField input, UnityAction action)
    {
        if (input != null)
        {
            input.onValueChanged.RemoveAllListeners();
            input.onValueChanged.AddListener(action);
        }
    }
    /// 
    /// 添加带参数的InputField值的修改事件
    /// 
    /// 
    /// 
    /// 
    /// 
    public static void AddOnValueChangedHandler(this InputField input, UnityAction action, T t)
    {
        if (input != null)
        {
            input.onValueChanged.RemoveAllListeners();
            input.onValueChanged.AddListener(p =>
            {
                action(p, t);
            });
        }
    }
    /// 
    /// 添加InputField编辑完成事件
    /// 
    /// 
    /// 
    public static void AddOnEndEditHandler(this InputField input, UnityAction action)
    {
        if (input != null)
        {
            input.onEndEdit.RemoveAllListeners();
            input.onEndEdit.AddListener(action);
        }
    }
    /// 
    /// 添加带参数的InputField编辑完成事件
    /// 
    /// 
    /// 
    /// 
    /// 
    public static void AddOnEndEditHandler(this InputField input, UnityAction action, T t)
    {
        if (input != null)
        {
            input.onEndEdit.RemoveAllListeners();
            input.onEndEdit.AddListener(p =>
            {
                action(p, t);
            });
        }
    }

你可能感兴趣的:(unity)