using UnityEngine;
using UnityEngine.UI;
public class ImageTextSelfAdaption : MonoBehaviour
{
public Image m_aImage;
public Text m_aText;
float m_aImageWidth;
float m_aTextWidth;
float time;
private void Start()
{
time = Time.deltaTime;
m_aImageWidth = m_aImage.rectTransform.rect.width;
Reresh();
}
public void SetData(Sprite aSprite, float fValue)
{
m_aImage.sprite = aSprite;
m_aText.text = fValue.ToString();
Invoke("Reresh", time);
}
public void Reresh()
{
if (m_aTextWidth != m_aText.rectTransform.rect.width
|| m_aImageWidth != m_aImage.rectTransform.rect.width)
{
if(m_aImageWidth != m_aImage.rectTransform.rect.width)
m_aImageWidth = m_aImage.rectTransform.rect.width;
if (m_aTextWidth != m_aText.rectTransform.rect.width)
m_aTextWidth = m_aText.rectTransform.rect.width;
m_aImage.rectTransform.anchoredPosition = new Vector2(-m_aTextWidth / 2,
m_aImage.rectTransform.anchoredPosition.y);
m_aText.rectTransform.anchoredPosition = new Vector2(m_aImageWidth / 2,
m_aText.rectTransform.anchoredPosition.y);
}
}
}
|