2024-10-23 17:30:59 +00:00
|
|
|
|
#if UNITY_EDITOR
|
2024-10-08 15:19:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Unity.Mathematics;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Sirenix.OdinInspector;
|
2024-10-21 16:20:39 +00:00
|
|
|
|
using static UguiToolkit.Editor.LayoutInfo;
|
2024-11-04 18:05:34 +00:00
|
|
|
|
using System.IO;
|
2024-10-08 15:19:05 +00:00
|
|
|
|
|
|
|
|
|
namespace UguiToolkit.Editor
|
|
|
|
|
{
|
2024-10-23 17:30:59 +00:00
|
|
|
|
[Serializable]
|
2024-10-08 15:19:05 +00:00
|
|
|
|
public class CacheScriptObject : SerializedScriptableObject
|
|
|
|
|
{
|
2024-10-23 17:30:59 +00:00
|
|
|
|
[SerializeField]
|
2024-10-08 15:19:05 +00:00
|
|
|
|
public Dictionary<GameObject, PanelCache> panelCaches = new();
|
2024-11-04 18:05:34 +00:00
|
|
|
|
|
|
|
|
|
public static LayoutInfo PaserLayout(string layoutInfoFilePath, string targetImgDirPath)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(layoutInfoFilePath))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("layoutInfoFilePath 为空");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
ILayoutParser layoutParser = new DefaultLayoutParser();
|
|
|
|
|
using (StreamReader reader = File.OpenText(layoutInfoFilePath))
|
|
|
|
|
{
|
|
|
|
|
var jsonData = reader.ReadToEnd();
|
|
|
|
|
var layoutInfo = layoutParser.Parser(jsonData);
|
|
|
|
|
|
|
|
|
|
// 对img路径进行修正
|
|
|
|
|
string projectPath = Directory.GetParent(Application.dataPath).FullName;
|
|
|
|
|
|
|
|
|
|
foreach (var elementInfo in layoutInfo)
|
|
|
|
|
{
|
|
|
|
|
var imgInfo = elementInfo as LayoutInfo.ImageInfo;
|
|
|
|
|
if (imgInfo != null)
|
|
|
|
|
{
|
|
|
|
|
imgInfo.imgPath = System.IO.Path.Join(targetImgDirPath, imgInfo.imgPath) + ".png";
|
|
|
|
|
imgInfo.imgPath = Path.GetRelativePath(projectPath, imgInfo.imgPath).Replace("\\", "/");
|
2024-12-01 16:33:27 +00:00
|
|
|
|
|
|
|
|
|
if (imgInfo.HaveSlice)
|
|
|
|
|
{
|
|
|
|
|
imgInfo.imgSlicePath = System.IO.Path.Join(targetImgDirPath, imgInfo.imgSlicePath) + ".png";
|
|
|
|
|
imgInfo.imgSlicePath = Path.GetRelativePath(projectPath, imgInfo.imgSlicePath).Replace("\\", "/");
|
|
|
|
|
}
|
2024-11-04 18:05:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return layoutInfo;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 16:16:28 +00:00
|
|
|
|
public static void CalcRotScaleInfos(string srcImgDirPath, string targetImgDirPath, float distanceDifference, Action<Dictionary<string, List<RotScaleInfoItem>>> callback)
|
2024-11-04 18:05:34 +00:00
|
|
|
|
{
|
|
|
|
|
// 执行cmd
|
2024-11-07 16:16:28 +00:00
|
|
|
|
CommandHelper.CalcRotScale(srcImgDirPath, targetImgDirPath, distanceDifference,(jsonData) =>
|
2024-11-04 18:05:34 +00:00
|
|
|
|
{
|
|
|
|
|
if (jsonData == null || jsonData.data == null) return;
|
|
|
|
|
Dictionary<string, List<RotScaleInfoItem>> rotScaleInfos = new();
|
|
|
|
|
|
|
|
|
|
string projectPath = Directory.GetParent(Application.dataPath).FullName;
|
|
|
|
|
foreach (var kv in jsonData.data)
|
|
|
|
|
{
|
|
|
|
|
List<RotScaleInfoItem> rotScaleItemList = new();
|
|
|
|
|
rotScaleInfos[Path.GetRelativePath(projectPath, kv.Key).Replace("\\", "/")] = rotScaleItemList;
|
|
|
|
|
foreach (var jsonItemData in kv.Value)
|
|
|
|
|
{
|
|
|
|
|
rotScaleItemList.Add(new()
|
|
|
|
|
{
|
|
|
|
|
imgPath = Path.GetRelativePath(projectPath, jsonItemData.targetPath).Replace("\\", "/"),
|
|
|
|
|
rotiation = jsonItemData.rot,
|
|
|
|
|
scale = new float2(jsonItemData.scale[0], jsonItemData.scale[1]),
|
|
|
|
|
similarityCalc = jsonItemData.similarityCalc
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(rotScaleInfos);
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-10-08 15:19:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
|
public class PanelCache
|
|
|
|
|
{
|
2024-11-28 17:06:27 +00:00
|
|
|
|
//[LabelText("源图片文件夹路径"), FolderPath]
|
|
|
|
|
//public string srcImgDirPath;
|
2024-10-08 15:19:05 +00:00
|
|
|
|
[LabelText("目标图片信息文件(psd导出)"), FolderPath]
|
|
|
|
|
public string layoutInfoFilePath; // Sample.layout.txt
|
|
|
|
|
[LabelText("目标图片文件夹路径")]
|
2024-11-04 18:05:34 +00:00
|
|
|
|
public string TargetImgDirPath => m_targetImgDirPath;
|
2024-10-21 16:20:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 通过ElementInfo创建所有Actor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 鼠标选中img或者text,1s 后触发助手
|
|
|
|
|
|
|
|
|
|
// img
|
|
|
|
|
// 2. 通过img的图片路径,查找rotScaleInfos,获得匹配的旋转缩放信息. 如果没有查找到则return
|
|
|
|
|
// 3. 通过img的图片路径, 查找ActorManager, 获得所有和图片路径匹配的Actor,并显示所有匹配的Actor
|
|
|
|
|
// 4. 每帧刷新,如果鼠标进入Actor的rect中,则播放scale动效。鼠标松开,则apply 该actor的 trasform
|
|
|
|
|
|
|
|
|
|
// text
|
|
|
|
|
// 2. 每帧刷新,查找圆形范围内,所有text Actor,并显示所有匹配的Actor
|
|
|
|
|
// 3. 每帧刷新,如果鼠标进入Actor的rect中,则播放scale动效。鼠标松开,则apply 该actor的 trasform
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 通过cmd计算获得 图片路径(源图) -> 旋转缩放信息(效果图)
|
2024-10-23 17:30:59 +00:00
|
|
|
|
[SerializeField]
|
2024-10-21 16:20:39 +00:00
|
|
|
|
public Dictionary<string, List<RotScaleInfoItem>> rotScaleInfos = new();
|
2024-10-23 17:30:59 +00:00
|
|
|
|
[SerializeField]
|
2024-10-21 16:20:39 +00:00
|
|
|
|
public LayoutInfo layoutInfo;
|
2024-11-04 18:05:34 +00:00
|
|
|
|
[SerializeField, HideInInspector]
|
|
|
|
|
private string m_targetImgDirPath;
|
|
|
|
|
private PanelCache() { }
|
|
|
|
|
|
2024-11-28 17:06:27 +00:00
|
|
|
|
// public PanelCache(string srcImgDirPath, string layoutInfoFilePath)
|
|
|
|
|
public PanelCache(string layoutInfoFilePath)
|
2024-11-04 18:05:34 +00:00
|
|
|
|
{
|
2024-11-28 17:06:27 +00:00
|
|
|
|
// this.srcImgDirPath = srcImgDirPath;
|
2024-11-04 18:05:34 +00:00
|
|
|
|
this.layoutInfoFilePath = layoutInfoFilePath;
|
|
|
|
|
|
|
|
|
|
this.m_targetImgDirPath = GetTargetImgDirPath(layoutInfoFilePath);
|
|
|
|
|
}
|
2024-10-21 16:20:39 +00:00
|
|
|
|
|
2024-10-23 17:30:59 +00:00
|
|
|
|
public IEnumerable<T> GetLayoutElementInfos<T>() where T : LayoutInfo.ElementInfo
|
2024-10-08 15:19:05 +00:00
|
|
|
|
{
|
2024-10-21 16:20:39 +00:00
|
|
|
|
if (layoutInfo == null) yield break;
|
2024-10-08 15:19:05 +00:00
|
|
|
|
|
2024-10-21 16:20:39 +00:00
|
|
|
|
foreach (var e in layoutInfo)
|
|
|
|
|
{
|
|
|
|
|
if (e is T)
|
|
|
|
|
{
|
|
|
|
|
yield return e as T;
|
|
|
|
|
}
|
2024-10-08 15:19:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 16:20:39 +00:00
|
|
|
|
public static string GetTargetImgDirPath(in string layoutInfoFilePath)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(layoutInfoFilePath)) return "";
|
|
|
|
|
var dirName = System.IO.Path.GetDirectoryName(layoutInfoFilePath);
|
|
|
|
|
var fileName = System.IO.Path.GetFileName(layoutInfoFilePath);
|
|
|
|
|
var split = fileName.Split(".");
|
|
|
|
|
if (split.Length == 0) return "";
|
2024-10-08 16:32:08 +00:00
|
|
|
|
|
2024-10-21 16:20:39 +00:00
|
|
|
|
return System.IO.Path.Join(dirName, split[0]);
|
|
|
|
|
}
|
2024-10-08 16:32:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
2024-10-21 16:20:39 +00:00
|
|
|
|
public class LayoutInfo: IEnumerable<ElementInfo>
|
2024-10-08 16:32:08 +00:00
|
|
|
|
{
|
2024-10-23 17:30:59 +00:00
|
|
|
|
[SerializeField]
|
2024-10-09 16:12:59 +00:00
|
|
|
|
private List<ElementInfo> m_elementInfos;
|
2024-10-23 17:30:59 +00:00
|
|
|
|
[SerializeField]
|
2024-10-09 16:12:59 +00:00
|
|
|
|
private float m_w;
|
2024-10-23 17:30:59 +00:00
|
|
|
|
[SerializeField]
|
2024-10-09 16:12:59 +00:00
|
|
|
|
private float m_h;
|
|
|
|
|
|
|
|
|
|
public float W => m_w;
|
|
|
|
|
public float H => m_h;
|
2024-10-28 16:31:38 +00:00
|
|
|
|
public int Count => m_elementInfos == null ? 0 : m_elementInfos.Count;
|
2024-10-09 16:12:59 +00:00
|
|
|
|
|
|
|
|
|
public LayoutInfo(List<ElementInfo> elementInfos, float w, float h)
|
|
|
|
|
{
|
|
|
|
|
this.m_elementInfos = elementInfos;
|
|
|
|
|
this.m_w = w;
|
|
|
|
|
this.m_h = h;
|
|
|
|
|
}
|
2024-10-08 16:32:08 +00:00
|
|
|
|
|
2024-10-21 16:20:39 +00:00
|
|
|
|
public IEnumerator<ElementInfo> GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
return ((IEnumerable<ElementInfo>)m_elementInfos).GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
return ((IEnumerable)m_elementInfos).GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
2024-10-08 16:32:08 +00:00
|
|
|
|
public class ElementInfo
|
|
|
|
|
{
|
2024-10-21 16:20:39 +00:00
|
|
|
|
public string name;
|
|
|
|
|
|
2024-10-08 16:32:08 +00:00
|
|
|
|
public float x;
|
|
|
|
|
public float y;
|
|
|
|
|
public float w;
|
|
|
|
|
public float h;
|
|
|
|
|
|
2024-10-09 16:12:59 +00:00
|
|
|
|
public LayoutInfo layoutInfo;
|
2024-10-08 16:32:08 +00:00
|
|
|
|
|
2024-10-21 16:20:39 +00:00
|
|
|
|
public float2 Position
|
|
|
|
|
{
|
2024-10-09 16:12:59 +00:00
|
|
|
|
get {
|
|
|
|
|
float x = (layoutInfo.W / 2f) * -1 + this.x + this.w / 2f;
|
|
|
|
|
float y = layoutInfo.H / 2f - this.y - this.h / 2f;
|
|
|
|
|
|
|
|
|
|
return new float2(x, y);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-21 16:20:39 +00:00
|
|
|
|
|
|
|
|
|
public Rect Rect {
|
|
|
|
|
get {
|
|
|
|
|
var pos = Position - new float2(this.w / 2f, this.h / 2f);
|
|
|
|
|
return new(pos.x, pos.y, this.w, this.h);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-08 16:32:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 16:20:39 +00:00
|
|
|
|
[Serializable]
|
2024-10-08 16:32:08 +00:00
|
|
|
|
public class ImageInfo : ElementInfo
|
|
|
|
|
{
|
|
|
|
|
public string imgPath;
|
2024-12-01 16:33:27 +00:00
|
|
|
|
public string imgSlicePath; // 九宫格图片路径
|
|
|
|
|
public bool HaveSlice => imgSlicePath != null; // 是否有九宫格
|
2024-10-08 16:32:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 16:20:39 +00:00
|
|
|
|
[Serializable]
|
2024-10-08 16:32:08 +00:00
|
|
|
|
public class TextInfo : ElementInfo
|
|
|
|
|
{
|
|
|
|
|
public string text;
|
2024-10-21 16:20:39 +00:00
|
|
|
|
public float size;
|
|
|
|
|
public Color color;
|
|
|
|
|
public string align;
|
|
|
|
|
public string font;
|
2024-12-02 14:14:39 +00:00
|
|
|
|
public float strokeSize;
|
|
|
|
|
public Color strokeColor;
|
|
|
|
|
|
|
|
|
|
public bool IsStroke => strokeSize != 0;
|
2024-10-08 16:32:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
|
public struct TargetImageTransformInfo
|
|
|
|
|
{
|
|
|
|
|
public float3 position;
|
|
|
|
|
public RotScaleInfoItem rotScaleInfo;
|
|
|
|
|
|
|
|
|
|
public string ImgPath => rotScaleInfo.imgPath;
|
2024-10-08 15:19:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
2024-10-08 16:32:08 +00:00
|
|
|
|
public struct RotScaleInfoItem
|
2024-10-08 15:19:05 +00:00
|
|
|
|
{
|
2024-10-08 16:32:08 +00:00
|
|
|
|
public string imgPath; //(效果图)
|
2024-10-08 15:19:05 +00:00
|
|
|
|
public float rotiation;
|
|
|
|
|
public float2 scale;
|
2024-11-04 18:05:34 +00:00
|
|
|
|
public bool similarityCalc;
|
2024-10-08 15:19:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-23 17:30:59 +00:00
|
|
|
|
#endif
|
2024-10-08 15:19:05 +00:00
|
|
|
|
|