using UnityEngine;
|
using UnityEngine.UI;
|
using UnityEngine.Events;
|
using System.Collections.Generic;
|
|
public class HeroHeadBaseNoTrainCell : MonoBehaviour
|
{
|
|
Button m_ClickBtn;
|
Button clickBtn
|
{
|
get
|
{
|
if (m_ClickBtn == null)
|
{
|
m_ClickBtn = this.transform.GetComponent<Button>("Container_HeadCell");
|
}
|
return m_ClickBtn;
|
}
|
}
|
|
ImageEx m_QualityBG;
|
ImageEx qualityBG
|
{
|
get
|
{
|
if (m_QualityBG == null)
|
{
|
m_QualityBG = this.transform.GetComponent<ImageEx>("Container_HeadCell/qualityIcon");
|
}
|
return m_QualityBG;
|
}
|
}
|
|
ImageEx m_HeroIcon;
|
ImageEx heroIcon
|
{
|
get
|
{
|
if (m_HeroIcon == null)
|
{
|
m_HeroIcon = this.transform.GetComponent<ImageEx>("Container_HeadCell/heroIcon");
|
}
|
return m_HeroIcon;
|
}
|
}
|
|
|
|
Image m_CountryImg;
|
Image countryImg
|
{
|
get
|
{
|
if (m_CountryImg == null)
|
{
|
m_CountryImg = this.transform.GetComponent<Image>("Container_HeadCell/country");
|
}
|
return m_CountryImg;
|
}
|
}
|
|
Image m_JobImg;
|
Image jobImg
|
{
|
get
|
{
|
if (m_JobImg == null)
|
{
|
m_JobImg = this.transform.GetComponent<Image>("Container_HeadCell/name/job");
|
}
|
return m_JobImg;
|
}
|
}
|
|
Text m_NameText;
|
Text nameText
|
{
|
get
|
{
|
if (m_NameText == null)
|
{
|
m_NameText = this.transform.GetComponent<Text>("Container_HeadCell/name");
|
}
|
return m_NameText;
|
}
|
}
|
|
void Awake()
|
{
|
LoadPrefab();
|
}
|
|
// 武将小头像,(职业和名称不再此管理,各个界面排版不同)
|
public void Init(int heroID, bool _gray = false, UnityAction onclick = null)
|
{
|
LoadPrefab(); //存在被卸载的可能,重新加载
|
clickBtn.AddListener(onclick);
|
var heroConfig = HeroConfig.Get(heroID);
|
qualityBG.SetSprite("heroheadBG" + heroConfig.Quality);
|
|
var sprite = UILoader.LoadSprite("HeroHead", HeroSkinConfig.Get(heroConfig.SkinIDList[0]).SquareIcon);
|
heroIcon.overrideSprite = sprite;
|
heroIcon.gray = _gray;
|
qualityBG.gray = _gray;
|
|
countryImg.SetSprite(HeroUIManager.Instance.GetCountryIconName(heroConfig.Country));
|
jobImg.SetSprite(HeroUIManager.Instance.GetJobIconName(heroConfig.Class));
|
nameText.text = heroConfig.Name;
|
|
|
}
|
|
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("HeroHeadBaseNoTrainCell", "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;
|
}
|
}
|