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) { Debug.Log(ex); return null; } } public static T AddMissingComponent(this Component _compoent) where T : Component { if (_compoent == null) { return null; } T component = _compoent.GetComponent(); if (component == null) { component = _compoent.gameObject.AddComponent(); } return component; } public static T AddMissingComponent(this Transform _transform) where T : Component { if (_transform == null) { return null; } T component = _transform.GetComponent(); if (component == null) { component = _transform.gameObject.AddComponent(); } return component; } public static T AddMissingComponent(this GameObject _gameObject) where T : Component { if (_gameObject == null) { return null; } T component = _gameObject.GetComponent(); if (component == null) { component = _gameObject.AddComponent(); } 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 _action) { if (_toggle == null) { return; } _toggle.onValueChanged.RemoveAllListeners(); _toggle.onValueChanged.AddListener(_action); } public static void SetListener(this Toggle toggle, UnityAction 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 _action) { if (_slider == null) { return; } _slider.onValueChanged.AddListener(_action); } public static void SetListener(this Slider slider, UnityAction 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 _action) { if (_inputField == null) { return; } _inputField.onValueChanged.AddListener(_action); } public static void SetListener(this InputField inputField, UnityAction 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 SetListener(this Dropdown dropdown, UnityAction action) { if (dropdown == null) { return; } dropdown.onValueChanged.RemoveAllListeners(); dropdown.onValueChanged.AddListener(action); } // 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.LightWhite : TextColType.White); break; case 2: _btnTxt.color = UIHelper.GetUIColor(_colorful ? TextColType.Green : TextColType.White); break; default: //false 灰色,true 原色 if (!_colorful) _btnTxt.text = UIHelper.AppendColor(TextColType.NavyGray, _btnTxt.text); //不改变组件颜色,只改变显示颜色 else _btnTxt.text = UIHelper.AppendColor(_btnTxt.color, _btnTxt.text); break; } } } // 设置按钮是否可点击,且取第一个文本组件置灰或置原色 // 要先设置文本再调用该函数 因为没有改变文本组件的颜色避免颜色被还原,同理恢复可以不用改变颜色改文字即可 // 更多功能请使用SetColorful public static void SetInteractable(this Button _btn, bool _interactable, Text _btnText = null) { if (_btn != null) { _btn.interactable = _interactable; var imageEx = _btn.image as ImageEx; if (imageEx != null) { imageEx.gray = !_interactable; } } if (_btnText == null) _btnText = _btn.GetComponentInChildren(); if (_btnText != null) { //false 灰色,true 原色 if (!_interactable) _btnText.text = UIHelper.AppendColor(TextColType.NavyGray, _btnText.text); //不改变组件颜色,只改变显示颜色 else _btnText.text = UIHelper.AppendColor(_btnText.color, UIHelper.RemoveColor(_btnText.text)); } } //通过ICON表加载 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; } //通过图片名加载, 如物品表 技能表等,节省在Icon表做多余配置 public static void SetOrgSprite(this Image _image, string iconName, string folderName = "icon") { if (_image == null) { return; } var sprite = UILoader.LoadSprite(folderName, iconName); _image.overrideSprite = sprite; } public static void SetItemSprite(this Image _image, int itemID) { if (_image == null) { return; } var itemConfig = ItemConfig.Get(itemID); if (itemConfig == null) { return; } var sprite = UILoader.LoadSprite("icon", itemConfig.IconKey); _image.overrideSprite = sprite; } public static void SetSkillSprite(this Image _image, int skillID) { if (_image == null) { return; } var skillConfig = SkillConfig.Get(skillID); if (skillConfig == null) { return; } var sprite = UILoader.LoadSprite("SkillIcon", skillConfig.IconName); _image.overrideSprite = 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); } } } public static void SetTexture2D(this RawImage _image, string _id) { if (_image == null) { return; } var texture = UILoader.LoadTexture2D(_id); _image.texture = texture; } public static void SetTexture2DPNG(this RawImage _image, string _id) { if (_image == null) { return; } var texture = UILoader.LoadTexture2DPNG(_id); _image.texture = texture; } }