2024-10-23 17:30:59 +00:00
|
|
|
|
#if UNITY_EDITOR
|
2024-10-21 16:20:39 +00:00
|
|
|
|
using UguiToolkit.Editor;
|
2024-10-23 17:30:59 +00:00
|
|
|
|
using UnityEditor;
|
2024-10-21 16:20:39 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
public class ImageEntity : BaseEntity<Image, LayoutInfo.ImageInfo>
|
|
|
|
|
{
|
2024-10-23 17:30:59 +00:00
|
|
|
|
private Image m_previewImage;
|
|
|
|
|
|
2024-10-21 16:20:39 +00:00
|
|
|
|
protected override void OnApplyData(Image ui, LayoutInfo.ImageInfo elementInfo)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2024-10-23 17:30:59 +00:00
|
|
|
|
|
|
|
|
|
public void InitPreviewImage() {
|
|
|
|
|
if (ElementInfo == null) return;
|
|
|
|
|
|
|
|
|
|
if (!TryGetComponent<Image>(out m_previewImage))
|
|
|
|
|
{
|
|
|
|
|
m_previewImage = gameObject.AddComponent<Image>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoadImageFromFile(ElementInfo.imgPath);
|
|
|
|
|
ApplyTransform(transform);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LoadImageFromFile(string path)
|
|
|
|
|
{
|
|
|
|
|
if (System.IO.File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
byte[] fileData = System.IO.File.ReadAllBytes(path);
|
|
|
|
|
Texture2D texture = new Texture2D(2, 2);
|
|
|
|
|
texture.LoadImage(fileData); // <20><><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC><EFBFBD>ݵ<EFBFBD>Texture2D
|
|
|
|
|
|
|
|
|
|
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
|
|
|
|
|
m_previewImage.sprite = sprite;
|
|
|
|
|
m_previewImage.SetNativeSize();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("File not found at path: " + path);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-21 16:20:39 +00:00
|
|
|
|
}
|
2024-10-23 17:30:59 +00:00
|
|
|
|
#endif
|