using UnityEngine;
|
using UnityEngine.UI;
|
using UnityEngine.Events;
|
using System;
|
|
|
public static class ComponentExtersion
|
{
|
|
public static Component FindComponent(this Component _component, string _type, string _path)
|
{
|
try
|
{
|
if (_component == null)
|
{
|
return null;
|
}
|
|
var transform = _component.transform.Find(_path);
|
if (transform == null)
|
{
|
return null;
|
}
|
|
return transform.GetComponent(_type);
|
}
|
catch (Exception ex)
|
{
|
DebugEx.Log(ex);
|
return null;
|
}
|
|
}
|
|
public static T AddMissingComponent<T>(this Component _compoent) where T : Component
|
{
|
if (_compoent == null)
|
{
|
return null;
|
}
|
|
T component = _compoent.GetComponent<T>();
|
if (component == null)
|
{
|
component = _compoent.gameObject.AddComponent<T>();
|
}
|
|
return component;
|
}
|
|
public static T AddMissingComponent<T>(this Transform _transform) where T : Component
|
{
|
if (_transform == null)
|
{
|
return null;
|
}
|
|
T component = _transform.GetComponent<T>();
|
if (component == null)
|
{
|
component = _transform.gameObject.AddComponent<T>();
|
}
|
|
return component;
|
}
|
|
public static T AddMissingComponent<T>(this GameObject _gameObject) where T : Component
|
{
|
if (_gameObject == null)
|
{
|
return null;
|
}
|
|
T component = _gameObject.GetComponent<T>();
|
if (component == null)
|
{
|
component = _gameObject.AddComponent<T>();
|
}
|
|
return component;
|
}
|
|
|
public static void AddListener(this Button _button, UnityAction _action)
|
{
|
if (_button == null)
|
{
|
return;
|
}
|
_button.onClick.RemoveAllListeners();
|
//_button.onClick.RemoveListener(_action);
|
_button.onClick.AddListener(_action);
|
}
|
|
|
public static void RemoveAllListeners(this Button _button)
|
{
|
if (_button == null)
|
{
|
return;
|
}
|
_button.onClick.RemoveAllListeners();
|
}
|
|
|
public static void SetListener(this Button button, UnityAction action)
|
{
|
if (button == null)
|
{
|
return;
|
}
|
|
button.onClick.RemoveAllListeners();
|
button.AddListener(action);
|
}
|
|
public static void AddListener(this Toggle _toggle, UnityAction<bool> _action)
|
{
|
if (_toggle == null)
|
{
|
return;
|
}
|
_toggle.onValueChanged.AddListener(_action);
|
}
|
|
|
public static void SetListener(this Toggle toggle, UnityAction<bool> action)
|
{
|
if (toggle == null)
|
{
|
return;
|
}
|
|
toggle.onValueChanged.RemoveAllListeners();
|
toggle.onValueChanged.AddListener(action);
|
}
|
|
public static void RemoveAllListeners(this Toggle _toggle)
|
{
|
if (_toggle == null)
|
{
|
return;
|
}
|
_toggle.onValueChanged.RemoveAllListeners();
|
}
|
|
|
public static void AddListener(this Slider _slider, UnityAction<float> _action)
|
{
|
if (_slider == null)
|
{
|
return;
|
}
|
_slider.onValueChanged.AddListener(_action);
|
}
|
|
|
public static void SetListener(this Slider slider, UnityAction<float> action)
|
{
|
if (slider == null)
|
{
|
return;
|
}
|
|
slider.onValueChanged.RemoveAllListeners();
|
slider.onValueChanged.AddListener(action);
|
}
|
|
public static void RemoveAllListeners(this Slider _slider)
|
{
|
if (_slider == null)
|
{
|
return;
|
}
|
_slider.onValueChanged.RemoveAllListeners();
|
}
|
|
|
public static void AddListener(this InputField _inputField, UnityAction<string> _action)
|
{
|
if (_inputField == null)
|
{
|
return;
|
}
|
_inputField.onValueChanged.AddListener(_action);
|
}
|
|
public static void SetListener(this InputField inputField, UnityAction<string> action)
|
{
|
if (inputField == null)
|
{
|
return;
|
}
|
|
inputField.onValueChanged.RemoveAllListeners();
|
inputField.onValueChanged.AddListener(action);
|
}
|
|
|
public static void RemoveAllListeners(this InputField _inputField)
|
{
|
if (_inputField == null)
|
{
|
return;
|
}
|
_inputField.onValueChanged.RemoveAllListeners();
|
}
|
|
|
public static void SetEnable(this Button _btn, Text _btnTxt, bool _enable, EnableButtonConfig.EnableButtonType _type =
|
EnableButtonConfig.EnableButtonType.Default)
|
{
|
EnableButtonConfig.SetEnable(_btn, _btnTxt, _enable, _type);
|
}
|
|
|
public static void SetColorful(this Button _btn, Text _btnTxt, bool _colorful, int pattern = 0)
|
{
|
if (_btn != null)
|
{
|
var imageEx = _btn.image as ImageEx;
|
if (imageEx != null)
|
{
|
imageEx.gray = !_colorful;
|
}
|
}
|
if (_btnTxt != null)
|
{
|
switch (pattern)
|
{
|
case 1:
|
_btnTxt.color = UIHelper.GetUIColor(_colorful ? TextColType.LightYellow : TextColType.White);
|
break;
|
case 2:
|
_btnTxt.color = UIHelper.GetUIColor(_colorful ? TextColType.Green : TextColType.White);
|
break;
|
default:
|
_btnTxt.color = UIHelper.GetUIColor(_colorful ? TextColType.NavyBrown : TextColType.White);
|
break;
|
}
|
}
|
}
|
|
|
public static void SetInteractable(this Button _btn, Text _btnText, bool _interactable)
|
{
|
if (_btn != null)
|
{
|
_btn.interactable = _interactable;
|
var imageEx = _btn.image as ImageEx;
|
if (imageEx != null)
|
{
|
imageEx.gray = !_interactable;
|
}
|
}
|
if (_btnText != null)
|
{
|
_btnText.color = UIHelper.GetUIColor(_interactable ? TextColType.NavyBrown : TextColType.White);
|
//_btnText.color = _btnText.color.SetA(_interactable ? 1 : 0.5f);
|
}
|
}
|
|
|
public static void SetSprite(this Image _image, string _id)
|
{
|
if (_image == null)
|
{
|
return;
|
}
|
|
var sprite = UILoader.LoadSprite(_id);
|
_image.overrideSprite = sprite;
|
}
|
|
|
public static void SetSprite(this TextImage _textImage, string _id)
|
{
|
if (_textImage == null)
|
{
|
return;
|
}
|
|
var sprite = UILoader.LoadSprite(_id);
|
_textImage.sprite = sprite;
|
}
|
|
|
public static void SetActive(this Component compoent, bool active)
|
{
|
if (compoent != null)
|
{
|
if (active && !compoent.gameObject.activeSelf)
|
{
|
compoent.gameObject.SetActive(true);
|
}
|
else if (!active && compoent.gameObject.activeSelf)
|
{
|
compoent.gameObject.SetActive(false);
|
}
|
}
|
}
|
|
}
|