【Unity】Toggle拡張メソッド

DEVELOP, Unity

using UnityEngine;
using UnityEngine.UI;
using System.Reflection;
 
namespace mira
{
    /// <summary>
    /// Toggle拡張メソッド
    /// </summary>
    public static class ToggleExtensions
    {
        private static MethodInfo toggleSetMethod;
         
        static ToggleExtensions()
        {
            var methods = typeof(Toggle).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
            for (int i = 0; i < methods.Length; ++i)
            {
                if (methods[i].Name == "Set" && methods[i].GetParameters().Length == 2)
                {
                    toggleSetMethod = methods[i];
                    break;
                }
            }
        }
 
        /// <summary>
        /// イベントのコールバック無しでToggleの値を変更する
        /// </summary>
        public static void SetIsOnWithoutNotify(this Toggle _toggle, bool _value)
        {
            toggleSetMethod.Invoke(_toggle, new object[] {_value, false});
        }
 
        /// <summary>
    	  /// クリックアクション設定(引数なし)
        /// </summary>
        public static void SetOnAction(this Toggle self, UnityEngine.Events.UnityAction _onAction)
        {
            if (self == null)
            {
                Debug.LogWarning("[ToggleExtensions::SetClickActionOn] The object was null.");
                return;
            }
 
            self.onValueChanged.AddListener((_isOn) =>
            {
                if(_isOn)
                {
                    _onAction.Call();
                }
            });
        }
 
        /// <summary>
        /// クリックアクション設定(引数1つ)
        /// </summary>
        public static void SetOnAction<T>(this Toggle self, UnityEngine.Events.UnityAction<T> _onAction, T arg)
        {
            if (self == null)
            {
                Debug.LogWarning("[ToggleExtensions::SetClickActionOn] The object was null.");
                return;
            }
 
            self.onValueChanged.AddListener((_isOn) =>
            {
                if (_isOn)
                {
                    _onAction.Call(arg);
                }
            });
        }
 
        /// <summary>
    	  /// クリックアクション設定(引数なし)
        /// </summary>
        public static void SetOffAction(this Toggle self, UnityEngine.Events.UnityAction _onAction)
        {
            if (self == null)
            {
                Debug.LogWarning("[ToggleExtensions::SetClickActionOff] The object was null.");
                return;
            }
 
            self.onValueChanged.AddListener((_isOn) =>
            {
                if (!_isOn)
                {
                    _onAction.Call();
                }
            });
        }
 
        /// <summary>
        /// クリックアクション設定(引数1つ)
        /// </summary>
        public static void SetOffAction<T>(this Toggle self, UnityEngine.Events.UnityAction<T> _onAction, T arg)
        {
            if (self == null)
            {
                Debug.LogWarning("[ToggleExtensions::SetClickActionOff] The object was null.");
                return;
            }
 
            self.onValueChanged.AddListener((_isOn) =>
            {
                if (!_isOn)
                {
                    _onAction.Call(arg);
                }
            });
        }
 
        /// <summary>
    	  /// クリックアクション設定(引数なし)
        /// </summary>
        public static void SetOnOffAction(this Toggle self, UnityEngine.Events.UnityAction<bool> _onAction)
        {
            if(self == null)
            {
                Debug.LogWarning("[ToggleExtensions::SetClickActionOnOff] The object was null.");
                return;
            }
 
            self.onValueChanged.AddListener((_isOn)=>
            {
                _onAction.Call(self.isOn);
            });
        }
 
        /// <summary>
        /// クリックアクション設定(引数1つ)
        /// </summary>
        public static void SetOnOffAction<T>(this Toggle self, UnityEngine.Events.UnityAction<bool, T> _onAction, T arg)
    	 {
            if(self == null)
            {
                Debug.LogWarning("[ToggleExtensions::SetClickActionOnOff] The object was null.");
                return;
            }
 
            self.onValueChanged.AddListener((_isOn)=>
            {
                _onAction.Call(self.isOn, arg);
            });
        }
 
        /// <summary>
        /// アクションの削除設定。
        /// </summary>
        public static void ClearAction(this Toggle self)
        {
            if(self == null)
            {
                Debug.LogWarning("[ToggleExtensions::SetClickAction] The object was null.");
                return;
            } 
            self.onValueChanged.RemoveAllListeners();
        }
    }
}

Posted by kazupon