Update UIExtension.cs

This commit is contained in:
ALEXTANG
2023-06-25 15:26:08 +08:00
parent 990e17a6cc
commit b60b9fff42

View File

@@ -7,6 +7,7 @@ using TEngine;
public static class UIExtension public static class UIExtension
{ {
#region SetActive #region SetActive
public static void SetActive(this GameObject go, bool value, ref bool cacheValue) public static void SetActive(this GameObject go, bool value, ref bool cacheValue)
{ {
if (go != null && value != cacheValue) if (go != null && value != cacheValue)
@@ -15,6 +16,7 @@ public static class UIExtension
go.SetActive(value); go.SetActive(value);
} }
} }
#endregion #endregion
public static IEnumerator FadeToAlpha(this CanvasGroup canvasGroup, float alpha, float duration, public static IEnumerator FadeToAlpha(this CanvasGroup canvasGroup, float alpha, float duration,
@@ -110,4 +112,46 @@ public static class UIExtension
return hadMouseDown; return hadMouseDown;
} }
public static void SetSprite(this Image image, string spriteName, UIBase uiBase, bool isSetNativeSize = false)
{
if (image == null)
{
return;
}
if (string.IsNullOrEmpty(spriteName))
{
image.sprite = null;
}
else
{
image.sprite = uiBase.LoadAsset<Sprite>(spriteName);
if (isSetNativeSize)
{
image.SetNativeSize();
}
}
}
public static void SetSprite(this UIBase uiBase, string spriteName, Image image, bool isSetNativeSize = false)
{
if (image == null)
{
return;
}
if (string.IsNullOrEmpty(spriteName))
{
image.sprite = null;
}
else
{
image.sprite = uiBase.LoadAsset<Sprite>(spriteName);
if (isSetNativeSize)
{
image.SetNativeSize();
}
}
}
} }