using UnityEngine; 
 | 
using UnityEngine.UI; 
 | 
using UnityEngine.Events; 
 | 
using System.Collections.Generic; 
 | 
  
 | 
public class HeroHeadBaseCell : MonoBehaviour 
 | 
{ 
 | 
  
 | 
    Button m_ClickBtn; 
 | 
    Button clickBtn 
 | 
    { 
 | 
        get 
 | 
        { 
 | 
            if (m_ClickBtn == null) 
 | 
            { 
 | 
                m_ClickBtn = this.transform.GetComponent<Button>("Container_HeadCell"); 
 | 
            } 
 | 
            return m_ClickBtn; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    Image m_QualityBG; 
 | 
    Image qualityBG 
 | 
    { 
 | 
        get 
 | 
        { 
 | 
            if (m_QualityBG == null) 
 | 
            { 
 | 
                m_QualityBG = this.transform.GetComponent<Image>("Container_HeadCell/qualityIcon"); 
 | 
            } 
 | 
            return m_QualityBG; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    Image m_HeroIcon; 
 | 
    Image heroIcon 
 | 
    { 
 | 
        get 
 | 
        { 
 | 
            if (m_HeroIcon == null) 
 | 
            { 
 | 
                m_HeroIcon = this.transform.GetComponent<Image>("Container_HeadCell/heroIcon"); 
 | 
            } 
 | 
            return m_HeroIcon; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    Transform m_StarRect; 
 | 
    Transform starRect 
 | 
    { 
 | 
        get 
 | 
        { 
 | 
            if (m_StarRect == null) 
 | 
            { 
 | 
                m_StarRect = this.transform.Find("Container_HeadCell/stars"); 
 | 
            } 
 | 
            return m_StarRect; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    List<Image> m_StarsImg; 
 | 
    List<Image> starsImg 
 | 
    { 
 | 
        get 
 | 
        { 
 | 
            if (m_StarsImg == null) 
 | 
            { 
 | 
                m_StarsImg = new List<Image>(); 
 | 
                for (int i = 0; i < 5; i++) 
 | 
                { 
 | 
                    var star = this.transform.GetComponent<Image>("Container_HeadCell/stars/star" + i); 
 | 
                    m_StarsImg.Add(star); 
 | 
                } 
 | 
            } 
 | 
            return m_StarsImg; 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
    Image m_CountryImg; 
 | 
    Image countryImg 
 | 
    { 
 | 
        get 
 | 
        { 
 | 
            if (m_CountryImg == null) 
 | 
            { 
 | 
                m_CountryImg = this.transform.GetComponent<Image>("Container_HeadCell/country"); 
 | 
            } 
 | 
            return m_CountryImg; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    Transform m_AwakeLvRect; 
 | 
    Transform awakeLvRect 
 | 
    { 
 | 
        get 
 | 
        { 
 | 
            if (m_AwakeLvRect == null) 
 | 
            { 
 | 
                m_AwakeLvRect = this.transform.Find("Container_HeadCell/awakelv"); 
 | 
            } 
 | 
            return m_AwakeLvRect; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    Text m_AwakeLvText; 
 | 
    Text awakeLvText 
 | 
    { 
 | 
        get 
 | 
        { 
 | 
            if (m_AwakeLvText == null) 
 | 
            { 
 | 
                m_AwakeLvText = this.transform.GetComponent<Text>("Container_HeadCell/awakelv/lv"); 
 | 
            } 
 | 
            return m_AwakeLvText; 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
    Text m_LvText; 
 | 
    Text lvText 
 | 
    { 
 | 
        get 
 | 
        { 
 | 
            if (m_LvText == null) 
 | 
            { 
 | 
                m_LvText = this.transform.GetComponent<Text>("Container_HeadCell/lv"); 
 | 
            } 
 | 
            return m_LvText; 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
    void Awake() 
 | 
    { 
 | 
        LoadPrefab(); 
 | 
    } 
 | 
  
 | 
    // 武将小头像,(职业和名称不再此管理,各个界面排版不同) 
 | 
    public void Init(int heroID, int skinID, int star = 0, int awakelv = 0, int lv = 0, UnityAction onclick = null) 
 | 
    { 
 | 
        LoadPrefab();   //存在被卸载的可能,重新加载 
 | 
        clickBtn.AddListener(onclick); 
 | 
        var heroConfig = HeroConfig.Get(heroID); 
 | 
        qualityBG.SetSprite("heroheadBG" + heroConfig.Quality); 
 | 
        // int skinID = 0; 
 | 
        // if (heroGuid != "") 
 | 
        // { 
 | 
        //     skinID = HeroManager.Instance.GetHero(heroGuid).SkinID; 
 | 
        // } 
 | 
        // else 
 | 
        // { 
 | 
        //     skinID = heroConfig.SkinIDList[0]; 
 | 
        // } 
 | 
        var sprite = UILoader.LoadSprite("HeroHead", HeroSkinConfig.Get(skinID).SquareIcon); 
 | 
        if (sprite == null) 
 | 
        { 
 | 
            // 内网未配置时 
 | 
            heroIcon.SetSprite("herohead_default"); 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            heroIcon.overrideSprite = sprite; 
 | 
        } 
 | 
  
 | 
  
 | 
        if (star == 0) 
 | 
        { 
 | 
            starRect.SetActive(false); 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            starRect.SetActive(true); 
 | 
            for (int i = 0; i < starsImg.Count; i++) 
 | 
            { 
 | 
                if ((star - 1) % starsImg.Count >= i) 
 | 
                { 
 | 
                    starsImg[i].SetActive(true); 
 | 
                    starsImg[i].SetSprite("herostar" + (((star - 1) / starsImg.Count) + 1) * starsImg.Count); 
 | 
                } 
 | 
                else 
 | 
                { 
 | 
                    starsImg[i].SetActive(false); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
  
 | 
        countryImg.SetSprite(HeroUIManager.Instance.GetCountryIconName(heroConfig.Country)); 
 | 
        lvText.text = lv == 0 ? "": Language.Get("L1094") + lv; 
 | 
  
 | 
        awakeLvRect.SetActive(awakelv > 0); 
 | 
        awakeLvText.text = awakelv.ToString(); 
 | 
  
 | 
  
 | 
    } 
 | 
  
 | 
    GameObject cellContainer; 
 | 
    protected void LoadPrefab() 
 | 
    { 
 | 
        if (cellContainer != null) 
 | 
            return; 
 | 
  
 | 
        var tmp = transform.Find("Container_HeadCell"); 
 | 
        if (tmp != null) 
 | 
        { 
 | 
            cellContainer = tmp.gameObject; 
 | 
            return; 
 | 
        } 
 | 
        if (cellContainer == null) 
 | 
        { 
 | 
            cellContainer = UIUtility.CreateWidget("HeroHeadBaseCell", "Container_HeadCell"); 
 | 
  
 | 
            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; 
 | 
    } 
 | 
} 
 |