com.soviby.unity.ui.ugui-to.../Assets/Editor/Entity/ImageEntity.cs

132 lines
4.3 KiB
C#
Raw Normal View History

2024-10-23 17:30:59 +00:00
#if UNITY_EDITOR
2024-11-06 17:45:57 +00:00
using Sirenix.OdinInspector;
2024-12-06 10:46:00 +00:00
using System;
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-12-09 11:18:10 +00:00
[ShowInInspector]
private Matrix4x4 lastTransformMatrix;
2024-10-23 17:30:59 +00:00
2024-12-10 10:14:38 +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
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>();
2024-12-17 03:07:00 +00:00
m_previewImage.type = Image.Type.Simple;
2024-12-01 16:33:27 +00:00
}
LoadImageFromFile(ElementInfo.imgPath);
ApplyTransform(transform);
2024-11-06 17:45:57 +00:00
}
2024-12-10 10:14:38 +00:00
protected override void OnApplyTransform(Transform tf)
2024-10-23 17:30:59 +00:00
{
2024-12-06 10:46:00 +00:00
var rt = tf as RectTransform;
2024-11-06 17:45:57 +00:00
if (needFillTransform)
{
2024-12-06 10:46:00 +00:00
var pos = ElementInfo.Position;
var worldPos = StageManager.Instance.PrefabContentsRoot.transform.TransformPoint(new Vector3(pos.x, pos.y, 0));
2024-11-06 17:45:57 +00:00
2024-12-06 10:46:00 +00:00
var oldPiovt = rt.pivot;
rt.pivot = new Vector2(0.5f, 0.5f);
2024-12-17 03:07:00 +00:00
Vector2 size = new Vector2(ElementInfo.w, ElementInfo.h);
2024-12-06 10:46:00 +00:00
Quaternion rotation = Quaternion.identity;
if (!similarityCalc)
2024-11-06 17:45:57 +00:00
{
2024-12-06 10:46:00 +00:00
if (rt.TryGetComponent<Image>(out var img) && img.sprite && img.sprite.texture)
{
var texture = img.sprite.texture;
if (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(texture)))
{
size = new Vector2(texture.width * scale.x, texture.height * scale.y);
}
}
rotation = Quaternion.Euler(0, 0, rotiation * -1);
2024-11-06 17:45:57 +00:00
}
2024-12-06 10:46:00 +00:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
2024-12-17 03:07:00 +00:00
rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x);
rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y);
2024-12-06 10:46:00 +00:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
rt.position = worldPos;
rt.rotation = rotation;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>
var oldRect = rt.rect;
2024-12-17 03:07:00 +00:00
var oldLocalPosition = rt.localPosition;
2024-12-06 10:46:00 +00:00
rt.pivot = oldPiovt;
2024-12-17 03:07:00 +00:00
rt.localPosition = oldLocalPosition;
2024-12-06 10:46:00 +00:00
var offsetRectPos = oldRect.position - rt.rect.position;
rt.Translate(offsetRectPos);
}
else
{
var pos = ElementInfo.Position;
var worldPos = StageManager.Instance.PrefabContentsRoot.transform.TransformPoint(new Vector3(pos.x, pos.y, 0));
rt.position = worldPos;
2024-11-06 17:45:57 +00:00
}
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