2024-10-23 17:30:59 +00:00
|
|
|
|
#if UNITY_EDITOR
|
2024-10-09 16:12:59 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace UguiToolkit.Editor
|
|
|
|
|
{
|
|
|
|
|
public interface ILayoutParser
|
|
|
|
|
{
|
|
|
|
|
LayoutInfo Parser(string txt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DefaultLayoutParser : ILayoutParser
|
|
|
|
|
{
|
|
|
|
|
public LayoutInfo Parser(string txt)
|
|
|
|
|
{
|
|
|
|
|
var layoutJsonData = JsonConvert.DeserializeObject<LayoutJsonData>(txt);
|
|
|
|
|
List<LayoutInfo.ElementInfo> elementInfos = new();
|
|
|
|
|
LayoutInfo layoutInfo = new(elementInfos, layoutJsonData.info.canvas.size.w, layoutJsonData.info.canvas.size.h);
|
|
|
|
|
|
|
|
|
|
void AddElementInfo(LayoutElementJsonData layoutElementJsonData)
|
|
|
|
|
{
|
|
|
|
|
if (layoutElementJsonData.type == "Image")
|
|
|
|
|
{
|
|
|
|
|
elementInfos.Add(new LayoutInfo.ImageInfo()
|
|
|
|
|
{
|
2024-10-21 16:20:39 +00:00
|
|
|
|
name = layoutElementJsonData.name,
|
2024-10-09 16:12:59 +00:00
|
|
|
|
imgPath = layoutElementJsonData.imageName,
|
|
|
|
|
x = layoutElementJsonData.x,
|
|
|
|
|
y = layoutElementJsonData.y,
|
|
|
|
|
w = layoutElementJsonData.w,
|
|
|
|
|
h = layoutElementJsonData.h,
|
2024-12-01 16:33:27 +00:00
|
|
|
|
layoutInfo = layoutInfo,
|
|
|
|
|
imgSlicePath = layoutElementJsonData.slice ? layoutElementJsonData.imageName + "-slice" : null
|
2024-10-09 16:12:59 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if (layoutElementJsonData.type == "Text")
|
|
|
|
|
{
|
2024-12-02 14:14:39 +00:00
|
|
|
|
ColorUtility.TryParseHtmlString("#" + layoutElementJsonData.color, out var fontColor);
|
|
|
|
|
Color strokeColor = Color.white;
|
|
|
|
|
if (!string.IsNullOrEmpty(layoutElementJsonData.strokeColor))
|
|
|
|
|
ColorUtility.TryParseHtmlString("#" + layoutElementJsonData.strokeColor, out strokeColor);
|
2024-10-09 16:12:59 +00:00
|
|
|
|
|
2024-10-21 16:20:39 +00:00
|
|
|
|
elementInfos.Add(new LayoutInfo.TextInfo()
|
|
|
|
|
{
|
|
|
|
|
name = layoutElementJsonData.name,
|
|
|
|
|
text = layoutElementJsonData.text,
|
|
|
|
|
font = layoutElementJsonData.font,
|
2024-12-02 14:14:39 +00:00
|
|
|
|
strokeSize = layoutElementJsonData.strokeSize,
|
|
|
|
|
strokeColor = strokeColor,
|
|
|
|
|
|
2024-10-21 16:20:39 +00:00
|
|
|
|
size = layoutElementJsonData.size,
|
|
|
|
|
align = layoutElementJsonData.align,
|
2024-12-02 14:14:39 +00:00
|
|
|
|
color = fontColor,
|
2024-10-21 16:20:39 +00:00
|
|
|
|
x = layoutElementJsonData.x,
|
|
|
|
|
y = layoutElementJsonData.y,
|
|
|
|
|
w = layoutElementJsonData.w,
|
|
|
|
|
h = layoutElementJsonData.h,
|
|
|
|
|
layoutInfo = layoutInfo
|
|
|
|
|
});
|
2024-10-09 16:12:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (layoutElementJsonData.elements != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var e in layoutElementJsonData.elements) AddElementInfo(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AddElementInfo(layoutJsonData.root);
|
|
|
|
|
|
|
|
|
|
return layoutInfo;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
|
public class LayoutJsonData
|
|
|
|
|
{
|
|
|
|
|
public LayoutInfoJsonData info;
|
|
|
|
|
public LayoutElementJsonData root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
|
public class LayoutInfoJsonData
|
|
|
|
|
{
|
|
|
|
|
public string version;
|
|
|
|
|
public CanvasInfoJsonData canvas;
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
|
public class CanvasInfoJsonData
|
|
|
|
|
{
|
|
|
|
|
public CanvasSizeJsonData size;
|
|
|
|
|
[Serializable]
|
|
|
|
|
public struct CanvasSizeJsonData
|
|
|
|
|
{
|
|
|
|
|
public float w;
|
|
|
|
|
public float h;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
|
public class LayoutElementJsonData
|
|
|
|
|
{
|
|
|
|
|
public string type;
|
|
|
|
|
public string name;
|
|
|
|
|
public float x;
|
|
|
|
|
public float y;
|
|
|
|
|
public float w;
|
|
|
|
|
public float h;
|
|
|
|
|
public List<LayoutElementJsonData> elements;
|
|
|
|
|
|
2024-10-21 16:20:39 +00:00
|
|
|
|
// ͼƬ
|
2024-10-09 16:12:59 +00:00
|
|
|
|
public string imageName;
|
2024-12-01 16:33:27 +00:00
|
|
|
|
public bool slice; // <20>Ƿ<EFBFBD>Ϊ<EFBFBD>Ź<EFBFBD>
|
2024-10-21 16:20:39 +00:00
|
|
|
|
|
|
|
|
|
// <20>ı<EFBFBD>
|
|
|
|
|
public string text;
|
|
|
|
|
public float size;
|
|
|
|
|
public string color;
|
|
|
|
|
public string align;
|
|
|
|
|
public string font;
|
2024-12-02 14:14:39 +00:00
|
|
|
|
public float strokeSize;
|
|
|
|
|
public string strokeColor;
|
2024-10-09 16:12:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-23 17:30:59 +00:00
|
|
|
|
|
|
|
|
|
#endif
|