2024-10-23 17:30:59 +00:00
|
|
|
|
#if UNITY_EDITOR
|
2024-11-06 17:45:57 +00:00
|
|
|
|
using Sirenix.OdinInspector;
|
2024-10-21 16:20:39 +00:00
|
|
|
|
using UguiToolkit.Editor;
|
2024-11-06 17:45:57 +00:00
|
|
|
|
using Unity.Mathematics;
|
2024-10-23 17:30:59 +00:00
|
|
|
|
using UnityEditor;
|
2024-10-21 16:20:39 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
2024-11-06 17:45:57 +00:00
|
|
|
|
namespace UguiToolkit.Editor
|
2024-10-21 16:20:39 +00:00
|
|
|
|
{
|
2024-11-06 17:45:57 +00:00
|
|
|
|
public class ImageEntity : BaseEntity<Image, LayoutInfo.ImageInfo>
|
2024-10-21 16:20:39 +00:00
|
|
|
|
{
|
2024-11-06 17:45:57 +00:00
|
|
|
|
[ShowInInspector]
|
|
|
|
|
private float rotiation;
|
|
|
|
|
[ShowInInspector]
|
|
|
|
|
private float2 scale;
|
|
|
|
|
[ShowInInspector]
|
|
|
|
|
private bool similarityCalc;
|
|
|
|
|
[ShowInInspector]
|
|
|
|
|
private bool needFillTransform;
|
2024-10-23 17:30:59 +00:00
|
|
|
|
|
2024-11-06 17:45:57 +00:00
|
|
|
|
private Image m_previewImage;
|
2024-10-23 17:30:59 +00:00
|
|
|
|
|
2024-12-01 16:33:27 +00:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|
|
|
|
public void SetTransform(float rotiation, float2 scale, bool similarityCalc)
|
2024-11-06 17:45:57 +00:00
|
|
|
|
{
|
2024-12-01 16:33:27 +00:00
|
|
|
|
this.rotiation = rotiation;
|
|
|
|
|
this.scale = scale;
|
|
|
|
|
this.similarityCalc = similarityCalc;
|
2024-11-06 17:45:57 +00:00
|
|
|
|
|
2024-12-01 16:33:27 +00:00
|
|
|
|
this.needFillTransform = true;
|
2024-11-06 17:45:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-01 16:33:27 +00:00
|
|
|
|
private void LoadImageFromFile(string path)
|
2024-10-23 17:30:59 +00:00
|
|
|
|
{
|
2024-11-06 17:45:57 +00:00
|
|
|
|
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;
|
2024-12-02 17:47:50 +00:00
|
|
|
|
m_previewImage.color = new Color(1, 1, 1, 0.5f);
|
2024-11-06 17:45:57 +00:00
|
|
|
|
m_previewImage.SetNativeSize();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("File not found at path: " + path);
|
|
|
|
|
}
|
2024-10-23 17:30:59 +00:00
|
|
|
|
}
|
2024-11-06 17:45:57 +00:00
|
|
|
|
|
2024-12-01 16:33:27 +00:00
|
|
|
|
protected override void OnApplyData(Image ui)
|
2024-11-06 17:45:57 +00:00
|
|
|
|
{
|
2024-12-01 16:33:27 +00:00
|
|
|
|
ApplyTransform(ui.transform);
|
2024-11-06 17:45:57 +00:00
|
|
|
|
|
2024-12-01 16:33:27 +00:00
|
|
|
|
if (ElementInfo.HaveSlice)
|
|
|
|
|
{
|
|
|
|
|
ui.type = Image.Type.Sliced;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void InitPreview()
|
|
|
|
|
{
|
|
|
|
|
if (ElementInfo == null) return;
|
|
|
|
|
|
|
|
|
|
if (!TryGetComponent<Image>(out m_previewImage))
|
|
|
|
|
{
|
|
|
|
|
m_previewImage = gameObject.AddComponent<Image>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoadImageFromFile(ElementInfo.imgPath);
|
|
|
|
|
ApplyTransform(transform);
|
2024-11-06 17:45:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void ApplyTransform(Transform tf)
|
2024-10-23 17:30:59 +00:00
|
|
|
|
{
|
2024-11-06 17:45:57 +00:00
|
|
|
|
if (needFillTransform)
|
|
|
|
|
{
|
|
|
|
|
if (similarityCalc)
|
|
|
|
|
{
|
|
|
|
|
var rt = tf as RectTransform;
|
|
|
|
|
rt.anchorMax = rt.anchorMin;
|
|
|
|
|
rt.sizeDelta = new Vector2(ElementInfo.w, ElementInfo.h);
|
|
|
|
|
|
|
|
|
|
rt.rotation = Quaternion.identity;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var rt = tf as RectTransform;
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>Ϊ0.5 0.5
|
|
|
|
|
rt.pivot = new Vector2(0.5f, 0.5f);
|
|
|
|
|
rt.rotation = Quaternion.Euler(0, 0, rotiation * -1);
|
|
|
|
|
|
|
|
|
|
// size
|
|
|
|
|
var img = tf.GetComponent<Image>();
|
|
|
|
|
img.SetNativeSize();
|
|
|
|
|
rt.anchorMax = rt.anchorMin;
|
|
|
|
|
rt.sizeDelta = new Vector2(rt.sizeDelta.x * scale.x, rt.sizeDelta.y * scale.y);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var position = ElementInfo.Position;
|
|
|
|
|
tf.position = StageManager.Instance.PrefabContentsRoot.transform.TransformPoint(new Vector3(position.x, position.y, 0));
|
2024-10-23 17:30:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-21 16:20:39 +00:00
|
|
|
|
}
|
2024-10-23 17:30:59 +00:00
|
|
|
|
#endif
|