Files
2026-05-26 16:15:54 +08:00

62 lines
1.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using asap.core;
using UnityEngine;
using UnityEngine.UI;
public class UIImage : Image
{
ShowImageData imageData;
IUIService uiService;
public void Sprite(string spriteName)
{
if (imageData == null)
{
imageData = new ShowImageData(this);
}
if (uiService == null)
{
uiService = GContext.container.Resolve<IUIService>();
}
if (uiService != null)
{
imageData.SetName(spriteName);
uiService.SetImageSprite(imageData);
}
else
{
Debug.LogError("IUIService is not resolved.");
}
}
}
public class ShowImageData : IShowImageData
{
public string spriteName { set; get; }
public Image image { set; get; }
string otherName;
/// <summary>
/// image 赋值必须新new一个对象防止sprite 给的一个错误的对象
/// </summary>
/// <param name="image"></param>
public ShowImageData(Image image)
{
this.image = image;
}
public ShowImageData(Image image, string spriteName)
{
this.image = image;
SetName(spriteName);
}
public void SetName(string spriteName)
{
this.spriteName = spriteName;
otherName = spriteName + "(Clone)";
}
public void SetImage(Sprite sprite)
{
if (image != null && (sprite.name == otherName || sprite.name == spriteName))
{
image.sprite = sprite;
}
}
}