yyl
8 天以前 b2d7bb59dc37c7b350786b076ee2f344b7c8911f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
 
public class SkillBaseCell : 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/type");
            }
            return m_SkillType;
        }
    }
 
    void Awake()
    {
        LoadPrefab();
    }
 
    public void Init(int skillID, UnityAction onclick = null, bool showType = false)
    {
        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();
        });
 
        if (showType)
        {
            skillType.text = Language.Get(config.FuncType == 1 ? "HeroSkillType_1" : "HeroSkillType_2");
        }
        else
        { 
            skillType.text = string.Empty;
        }
    }
 
    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("SkillBaseCell", "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>();
        scale = parentRect.sizeDelta.x / rect.sizeDelta.x;
        cellContainer.transform.localScale = new Vector3(scale, scale, scale);
    }
}