| using UnityEngine; | 
| using UnityEngine.UI; | 
| using UnityEngine.Events; | 
|   | 
| public class SkillWordCell : MonoBehaviour | 
| { | 
|   | 
|     Image m_SkillIcon; | 
|     Image skillIcon | 
|     { | 
|         get | 
|         { | 
|             if (m_SkillIcon == null) | 
|             { | 
|                 m_SkillIcon = this.transform.GetComponent<Image>("Container_SkillCell/Img_Icon"); | 
|             } | 
|             return m_SkillIcon; | 
|         } | 
|     } | 
|   | 
|     Button m_SkillBtn; | 
|     Button skillBtn | 
|     { | 
|         get | 
|         { | 
|             if (m_SkillBtn == null) | 
|             { | 
|                 m_SkillBtn = this.transform.GetComponent<Button>("Container_SkillCell"); | 
|             } | 
|             return m_SkillBtn; | 
|         } | 
|     } | 
|   | 
|   | 
|     Text m_SkillType; | 
|     Text skillType | 
|     { | 
|         get | 
|         { | 
|             if (m_SkillType == null) | 
|             { | 
|                 m_SkillType = this.transform.GetComponent<Text>("Container_SkillCell/name"); | 
|             } | 
|             return m_SkillType; | 
|         } | 
|     } | 
|   | 
|     void Awake() | 
|     { | 
|         LoadPrefab(); | 
|     } | 
|   | 
|     public void Init(int skillID, UnityAction onclick = null, bool showType = false) | 
|     { | 
|         LoadPrefab();   //存在被卸载的可能,重新加载 | 
|         var config = SkillConfig.Get(skillID); | 
|         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(); | 
|         }); | 
|   | 
|         skillType.text = config.SkillName; | 
|     } | 
|   | 
|     GameObject cellContainer; | 
|     protected void LoadPrefab() | 
|     { | 
|         if (cellContainer != null) | 
|             return; | 
|   | 
|         var tmp = transform.Find("Container_SkillCell"); | 
|         if (tmp != null) | 
|         { | 
|             cellContainer = tmp.gameObject; | 
|             return; | 
|         } | 
|         if (cellContainer == null) | 
|         { | 
|             cellContainer = UIUtility.CreateWidget("SkillWordCell", "Container_SkillCell"); | 
|   | 
|             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>(); | 
|         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; | 
|     } | 
| } |