本文整理汇总了C#中UnityEngine.UI.Image.GetComponent方法的典型用法代码示例。如果您正苦于以下问题:C# Image.GetComponent方法的具体用法?C# Image.GetComponent怎么用?C# Image.GetComponent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.UI.Image
的用法示例。
在下文中一共展示了Image.GetComponent方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetHealthBar
private void SetHealthBar(Text healthBarPercent, Image healthBar, int healthValue, int totalHealthValue)
{
int healthPercent = (int)(((float)healthValue / (float)totalHealthValue) * 100);
healthBarPercent.text = (healthPercent.ToString() + "%");
float width = (float)healthPercent;
float height = healthBar.GetComponent<RectTransform>().rect.height;
width = Mathf.Clamp(width, 0.0f, 100.0f);
healthBar.GetComponent<RectTransform>().sizeDelta = new Vector2(width, height);
if (healthPercent >= 80)
SetColor(healthBar, colorVeryHighHealth);
else if (healthPercent >= 60)
SetColor(healthBar, colorHighHealth);
else if (healthPercent >= 40)
SetColor(healthBar, colorMediumHealth);
else if (healthPercent >= 20)
SetColor(healthBar, colorLowHealth);
else
SetColor(healthBar, colorVeryLowHealth);
}
示例2: fitImageInView
private void fitImageInView(Image image)
{
var rect = image.GetComponent<RectTransform>();
float width = image.mainTexture.width / (float)Screen.width, height = image.mainTexture.height / (float)Screen.height;
var size = MathUtilities.FitInView(width, height, 1, 1);
rect.anchorMin = new Vector2(-(size.x*.5f) + .5f, -(size.y*.5f) + .5f);
rect.anchorMax = new Vector2(-(size.x*.5f) + .5f + size.x, -(size.y*.5f) + .5f + size.y);
rect.offsetMin = Vector2.zero;
rect.offsetMax = Vector2.zero;
}