46 lines
1.2 KiB
C#
46 lines
1.2 KiB
C#
#if UNITY_EDITOR
|
|
using UguiToolkit.Editor;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class ImageEntity : BaseEntity<Image, LayoutInfo.ImageInfo>
|
|
{
|
|
private Image m_previewImage;
|
|
|
|
protected override void OnApplyData(Image ui, LayoutInfo.ImageInfo elementInfo)
|
|
{
|
|
|
|
}
|
|
|
|
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); // ¼ÓÔØÍ¼Æ¬Êý¾Ýµ½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);
|
|
}
|
|
}
|
|
}
|
|
#endif |