15 private bool m_AutoHideIconWhenLabelEmpty =
false;
16 private bool m_LabelAutoSize =
true;
17 private float m_LabelPaddingLeftRight = 3f;
18 private float m_LabelPaddingTopBottom = 3f;
20 private RectTransform m_LabelRect;
21 private RectTransform m_IconRect;
22 private RectTransform m_ObjectRect;
23 private Vector3 m_IconOffest;
26 private Image m_IconImage;
28 public GameObject gameObject
30 get {
return m_GameObject; }
34 m_ObjectRect = value.GetComponent<RectTransform>();
39 get {
return m_IconImage; }
40 set { SetIcon(value); }
44 get {
return m_LabelText; }
48 if (value !=
null) m_LabelRect = m_LabelText.gameObject.GetComponent<RectTransform>();
52 public bool autoHideIconWhenLabelEmpty {
set { m_AutoHideIconWhenLabelEmpty = value; } }
53 public bool isIconActive {
get;
private set; }
59 public void SetLabel(GameObject labelObj,
bool autoSize,
float paddingLeftRight,
float paddingTopBottom)
61 m_GameObject = labelObj;
62 m_LabelAutoSize = autoSize;
63 m_LabelPaddingLeftRight = paddingLeftRight;
64 m_LabelPaddingTopBottom = paddingTopBottom;
66 m_LabelRect = m_LabelText.gameObject.GetComponent<RectTransform>();
67 m_ObjectRect = labelObj.GetComponent<RectTransform>();
71 public void SetAutoSize(
bool flag)
73 m_LabelAutoSize = flag;
76 public void SetIcon(Image image)
81 m_IconRect = m_IconImage.GetComponent<RectTransform>();
85 public void SetIconSprite(Sprite sprite)
87 if (m_IconImage !=
null) m_IconImage.sprite = sprite;
90 public void SetIconSize(
float width,
float height)
92 if (m_IconRect !=
null) m_IconRect.sizeDelta =
new Vector3(width, height);
95 public void UpdateIcon(
IconStyle iconStyle, Sprite sprite =
null)
97 if (m_IconImage ==
null)
return;
98 SetIconActive(iconStyle.
show);
101 m_IconImage.sprite = sprite ==
null ? iconStyle.
sprite : sprite;
102 m_IconImage.color = iconStyle.
color;
103 m_IconRect.sizeDelta =
new Vector2(iconStyle.
width, iconStyle.
height);
104 m_IconOffest = iconStyle.
offset;
105 m_Align = iconStyle.
align;
109 m_IconRect.SetSiblingIndex(0);
111 m_IconRect.SetSiblingIndex(m_GameObject.transform.childCount - 1);
115 public float GetLabelWidth()
117 if (m_LabelRect)
return m_LabelRect.sizeDelta.x;
121 public float GetLabelHeight()
123 if (m_LabelRect)
return m_LabelRect.sizeDelta.y;
127 public void SetLabelColor(Color color)
129 if (m_LabelText !=
null) m_LabelText.SetColor(color);
132 public void SetLabelRotate(
float rotate)
134 if (m_LabelText !=
null) m_LabelText.SetLocalEulerAngles(
new Vector3(0, 0, rotate));
137 public void SetPosition(Vector3 position)
139 if (m_GameObject !=
null)
141 m_GameObject.transform.localPosition = position;
145 public void SetLabelPosition(Vector3 position)
147 if (m_LabelRect) m_LabelRect.localPosition = position;
150 public void SetActive(
bool flag)
152 if (m_GameObject) ChartHelper.SetActive(m_GameObject, flag);
154 public void SetLabelActive(
bool flag)
156 if (m_LabelText !=
null) m_LabelText.SetActive(flag);
158 public void SetIconActive(
bool flag)
161 if (m_IconImage) ChartHelper.SetActive(m_IconImage, flag);
164 public bool SetText(
string text)
166 if (m_LabelRect ==
null)
return false;
167 if (m_LabelText !=
null && !m_LabelText.GetText().Equals(text))
169 m_LabelText.SetText(text);
172 var newSize =
string.IsNullOrEmpty(text) ? Vector2.zero :
173 new Vector2(m_LabelText.GetPreferredWidth() + m_LabelPaddingLeftRight * 2,
174 m_LabelText.GetPreferredHeight() + m_LabelPaddingTopBottom * 2);
175 var sizeChange = newSize.x != m_LabelRect.sizeDelta.x || newSize.y != m_LabelRect.sizeDelta.y;
178 m_LabelRect.sizeDelta = newSize;
184 if (m_AutoHideIconWhenLabelEmpty && isIconActive)
186 ChartHelper.SetActive(m_IconImage.gameObject, !
string.IsNullOrEmpty(text));
192 private void AdjustIconPos()
194 if (m_IconImage && m_IconRect)
200 switch (m_LabelText.alignment)
202 case TextAnchor.LowerLeft:
203 case TextAnchor.UpperLeft:
204 case TextAnchor.MiddleLeft:
205 iconX = -m_ObjectRect.sizeDelta.x / 2 - m_IconRect.sizeDelta.x / 2;
207 case TextAnchor.LowerRight:
208 case TextAnchor.UpperRight:
209 case TextAnchor.MiddleRight:
210 iconX = m_ObjectRect.sizeDelta.x / 2 - m_LabelText.GetPreferredWidth() - m_IconRect.sizeDelta.x / 2;
212 case TextAnchor.LowerCenter:
213 case TextAnchor.UpperCenter:
214 case TextAnchor.MiddleCenter:
215 iconX = -m_LabelText.GetPreferredWidth() / 2 - m_IconRect.sizeDelta.x / 2;
220 switch (m_LabelText.alignment)
222 case TextAnchor.LowerLeft:
223 case TextAnchor.UpperLeft:
224 case TextAnchor.MiddleLeft:
225 iconX = m_ObjectRect.sizeDelta.x / 2 + m_IconRect.sizeDelta.x / 2;
227 case TextAnchor.LowerRight:
228 case TextAnchor.UpperRight:
229 case TextAnchor.MiddleRight:
230 iconX = m_IconRect.sizeDelta.x / 2;
232 case TextAnchor.LowerCenter:
233 case TextAnchor.UpperCenter:
234 case TextAnchor.MiddleCenter:
235 iconX = m_LabelText.GetPreferredWidth() / 2 + m_IconRect.sizeDelta.x / 2;
240 m_IconRect.anchoredPosition = m_IconOffest +
new Vector3(iconX, 0);