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