yyl
2026-05-11 51b0f6ed9f4e1d3bb6f8144470b46908c7699a96
Main/System/SkillUI/SkillBaseCell.cs
@@ -1,6 +1,7 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using Cysharp.Threading.Tasks;
public class SkillBaseCell : MonoBehaviour
{
@@ -45,29 +46,63 @@
        }
    }
    void Awake()
    Image m_WordBG;
    Image wordBG
    {
        LoadPrefab();
        get
        {
            if (m_WordBG == null)
            {
                m_WordBG = this.transform.GetComponent<Image>("Container_SkillCell/Image");
            }
            return m_WordBG;
        }
    }
    private void Init(int skillID, UnityAction onclick, bool showType = false)
    void Awake()
    {
        LoadPrefab().Forget();
    }
    public async UniTask Init(int skillID, UnityAction onclick = null, bool showType = false)
    {
        await LoadPrefab();   //存在被卸载的可能,重新加载
        if (this == null) return;
        var config = SkillConfig.Get(skillID);
        skillIcon.SetSprite(config.IconName);
        skillBtn.AddListener(onclick);
        if (config == null)
        {
            Debug.LogErrorFormat("技能未配置 : {0}", skillID);
            return;
        }
        skillIcon.SetOrgSprite(config.IconName, "SkillIcon");
#if UNITY_EDITOR
        if (string.IsNullOrEmpty(config.IconName))
        {
            //内网测试
            skillIcon.SetOrgSprite("skillicondefault", "SkillIcon");
        }
#endif
        skillBtn.AddListener(()=>
        {
            onclick?.Invoke();
        });
        if (showType)
        {
            skillType.text = Language.Get(config.FuncType == 23 ? "HeroSkillType_1" : "HeroSkillType_2");
            skillType.text = Language.Get(config.FuncType == 1 ? "HeroSkillType_1" : "HeroSkillType_2");
            wordBG.SetActive(true);
        }
        else
        {
        {
            skillType.text = string.Empty;
            wordBG.SetActive(false);
        }
    }
    GameObject cellContainer;
    protected void LoadPrefab()
    protected async UniTask LoadPrefab()
    {
        if (cellContainer != null)
            return;
@@ -78,23 +113,45 @@
            cellContainer = tmp.gameObject;
            return;
        }
        if (cellContainer == null)
        {
            cellContainer = UIUtility.CreateWidget("SkillBaseCell", "Container_SkillCell");
            if (cellContainer != null)
            {
                cellContainer.transform.SetParentEx(this.transform, Vector3.zero, Quaternion.identity, Vector3.one);
                cellContainer.transform.SetAsFirstSibling();
            }
        var inst = await UIUtility.CreateWidget("SkillBaseCell", "Container_SkillCell");
        if (this == null)
        {
            if (null != inst) DestroyImmediate(inst);
            return;
        }
        if (cellContainer != null)
        {
            DestroyImmediate(inst);
            return;
        }
        cellContainer = inst;
        if (cellContainer != null)
        {
            cellContainer.transform.SetParentEx(this.transform, Vector3.zero, Quaternion.identity, Vector3.one);
            cellContainer.transform.SetAsFirstSibling();
        }
        
        //缩放到和父rect一样大
        var scale = 1f;
        var rect = cellContainer.GetComponent<RectTransform>();
        var parentRect = transform.GetComponent<RectTransform>();
        scale = parentRect.sizeDelta.x / rect.sizeDelta.x;
        cellContainer.transform.localScale = new Vector3(scale, scale, scale);
        float width = parentRect.sizeDelta.x;
        if (width <= 0f)
        {
            //外部控制了尺寸获取为0
            GridLayoutGroup grid = GetComponentInParent<GridLayoutGroup>();
            if (grid != null)
            {
                width = grid.cellSize.x;
            }
        }
        scale = width / rect.sizeDelta.x;
        cellContainer.transform.localScale = cellContainer.transform.localScale * scale;
    }
}