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,
|
|
|
|
|
layoutInfo = layoutInfo
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if (layoutElementJsonData.type == "Text")
|
|
|
|
|
{
|
2024-10-28 16:31:38 +00:00
|
|
|
|
ColorUtility.TryParseHtmlString("#" + layoutElementJsonData.color, out var color);
|
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,
|
|
|
|
|
size = layoutElementJsonData.size,
|
|
|
|
|
align = layoutElementJsonData.align,
|
|
|
|
|
color = color,
|
|
|
|
|
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-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-10-09 16:12:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-23 17:30:59 +00:00
|
|
|
|
|
|
|
|
|
#endif
|