hch
2025-11-21 eb27e5fd31f73b998a4bbd85511a31e40b8c61b7
Main/Utility/ComponentExtersion.cs
@@ -255,8 +255,44 @@
        }
    }
    public static void SetInteractable(this Button _btn, Text _btnText, bool _interactable)
    public static void SetColorful(this GroupButtonEx _btn, Text _btnTxt, bool _colorful, int pattern = 0)
    {
        if (_btn != null)
        {
            if (_btn.selectIcon != null)
            {
                _btn.selectIcon.gray = !_colorful;
            }
            if (_btn.unSelectIcon != null)
            {
                _btn.unSelectIcon.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)
        {
@@ -267,10 +303,15 @@
                imageEx.gray = !_interactable;
            }
        }
        if (_btnText == null)
            _btnText = _btn.GetComponentInChildren<Text>();
        if (_btnText != null)
        {
            _btnText.color = UIHelper.GetUIColor(_interactable ? TextColType.NavyBrown : TextColType.White);
            //_btnText.color = _btnText.color.SetA(_interactable ? 1 : 0.5f);
            //false 灰色,true 原色
            if (!_interactable)
                _btnText.text = UIHelper.AppendColor(TextColType.NavyGray, _btnText.text);    //不改变组件颜色,只改变显示颜色
            else
                _btnText.text = UIHelper.AppendColor(_btnText.color, UIHelper.RemoveColor(_btnText.text));
        }
    }
@@ -279,6 +320,12 @@
    {
        if (_image == null)
        {
            return;
        }
        if (string.IsNullOrEmpty(_id))
        {
            Debug.LogError("Image SetSprite id is null or empty " + _id);
            return;
        }
@@ -294,6 +341,12 @@
            return;
        }
        if (string.IsNullOrEmpty(_id))
        {
            Debug.LogError("TextImage SetSprite id is null or empty " + _id);
            return;
        }
        var sprite = UILoader.LoadSprite(_id);
        _textImage.sprite = sprite;
    }
@@ -306,7 +359,14 @@
            return;
        }
        if (string.IsNullOrEmpty(iconName))
        {
            Debug.LogError("SetOrgSprite iconName is null or empty " + iconName);
            return;
        }
        var sprite = UILoader.LoadSprite(folderName, iconName);
        if (null == sprite) return;
        _image.overrideSprite = sprite;
    }
@@ -320,6 +380,12 @@
        var itemConfig = ItemConfig.Get(itemID);
        if (itemConfig == null)
        {
            return;
        }
        if (string.IsNullOrEmpty(itemConfig.IconKey))
        {
            Debug.LogError("SetItemSprite IconKey is null or empty for itemID " + itemID);
            return;
        }
@@ -337,6 +403,12 @@
        var skillConfig = SkillConfig.Get(skillID);
        if (skillConfig == null)
        {
            return;
        }
        if (string.IsNullOrEmpty(skillConfig.IconName))
        {
            Debug.LogError("SetSkillSprite IconName is null or empty for skillID " + skillID);
            return;
        }
@@ -370,4 +442,22 @@
        var texture = UILoader.LoadTexture2D(_id);
        _image.texture = texture;
    }
    public static void SetTexture2DPNG(this RawImage _image, string _id)
    {
        if (_image == null)
        {
            return;
        }
        if (string.IsNullOrEmpty(_id))
        {
            Debug.LogError("SetTexture2DPNG id is null or empty " + _id);
            return;
        }
        var texture = UILoader.LoadTexture2DPNG(_id);
        _image.texture = texture;
    }
}