16 private string m_Name;
17 private string m_LegendName;
18 private GameObject m_GameObject;
19 private Button m_Button;
22 private Image m_TextBackground;
23 private RectTransform m_Rect;
24 private RectTransform m_IconRect;
25 private RectTransform m_TextRect;
26 private RectTransform m_TextBackgroundRect;
27 private float m_Gap = 0f;
28 private float m_LabelPaddingLeftRight = 0f;
29 private float m_LabelPaddingTopBottom = 0f;
30 private bool m_LabelAutoSize =
true;
32 public int index {
get {
return m_Index; }
set { m_Index = value; } }
33 public string name {
get {
return m_Name; }
set { m_Name = value; } }
34 public string legendName {
get {
return m_LegendName; }
set { m_LegendName = value; } }
35 public GameObject gameObject {
get {
return m_GameObject; } }
36 public Button button {
get {
return m_Button; } }
41 if (m_IconRect && m_TextBackgroundRect)
43 return m_IconRect.sizeDelta.x + m_Gap + m_TextBackgroundRect.sizeDelta.x;
56 if (m_IconRect && m_TextBackgroundRect)
58 return Mathf.Max(m_IconRect.sizeDelta.y, m_TextBackgroundRect.sizeDelta.y);
67 public void SetObject(GameObject obj)
70 m_Button = obj.GetComponent<Button>();
71 m_Rect = obj.GetComponent<RectTransform>();
72 m_Icon = obj.transform.Find(
"icon").gameObject.GetComponent<Image>();
73 m_TextBackground = obj.transform.Find(
"content").gameObject.GetComponent<Image>();
75 m_IconRect = m_Icon.gameObject.GetComponent<RectTransform>();
76 m_TextRect = m_Text.gameObject.GetComponent<RectTransform>();
77 m_TextBackgroundRect = m_TextBackground.gameObject.GetComponent<RectTransform>();
80 public void SetButton(Button button)
85 public void SetIcon(Image icon)
95 public void SetTextBackground(Image image)
97 m_TextBackground = image;
100 public void SetIconSize(
float width,
float height)
104 m_IconRect.sizeDelta =
new Vector2(width, height);
108 public Rect GetIconRect()
110 if (m_GameObject && m_IconRect)
112 var pos = m_GameObject.transform.localPosition;
113 var sizeDelta = m_IconRect.sizeDelta;
114 var y = pos.y - (m_Rect.sizeDelta.y - sizeDelta.y) / 2 - sizeDelta.y;
115 return new Rect(pos.x, y, m_IconRect.sizeDelta.x, m_IconRect.sizeDelta.y);
123 public Color GetIconColor()
125 if (m_Icon)
return m_Icon.color;
126 else return Color.clear;
130 public void SetIconColor(Color color)
134 m_Icon.color = color;
138 public void SetIconImage(Sprite image)
142 m_Icon.sprite = image;
146 public void SetIconActive(
bool active)
150 m_Icon.gameObject.SetActive(active);
154 public void SetContentColor(Color color)
158 m_Text.SetColor(color);
162 public void SetContentBackgroundColor(Color color)
164 if (m_TextBackground)
166 m_TextBackground.color = color;
170 public void SetContentPosition(Vector3 offset)
173 if (m_TextBackgroundRect)
175 var posX = m_IconRect.sizeDelta.x + offset.x;
176 m_TextBackgroundRect.anchoredPosition3D =
new Vector3(posX, offset.y, 0);
180 public bool SetContent(
string content)
182 if (m_Text !=
null && !m_Text.GetText().Equals(content))
184 m_Text.SetText(content);
187 var newSize =
string.IsNullOrEmpty(content) ? Vector2.zero :
188 new Vector2(m_Text.GetPreferredWidth(), m_Text.GetPreferredHeight());
189 var sizeChange = newSize.x != m_TextRect.sizeDelta.x || newSize.y != m_TextRect.sizeDelta.y;
192 m_TextRect.sizeDelta = newSize;
193 m_TextRect.anchoredPosition3D =
new Vector3(m_LabelPaddingLeftRight, 0);
194 m_TextBackgroundRect.sizeDelta =
new Vector2(m_Text.GetPreferredWidth() + m_LabelPaddingLeftRight * 2,
195 m_Text.GetPreferredHeight() + m_LabelPaddingTopBottom * 2 - 4);
196 m_Rect.sizeDelta =
new Vector3(width, height);
204 public void SetPosition(Vector3 position)
208 m_GameObject.transform.localPosition = position;
212 public void SetActive(
bool active)
216 m_GameObject.SetActive(active);