127 lines
4.1 KiB
C#
127 lines
4.1 KiB
C#
#if UNITY_EDITOR
|
||
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()
|
||
{
|
||
name = layoutElementJsonData.name,
|
||
imgPath = layoutElementJsonData.imageName,
|
||
x = layoutElementJsonData.x,
|
||
y = layoutElementJsonData.y,
|
||
w = layoutElementJsonData.w,
|
||
h = layoutElementJsonData.h,
|
||
layoutInfo = layoutInfo,
|
||
imgSlicePath = layoutElementJsonData.slice ? layoutElementJsonData.imageName + "-slice" : null
|
||
});
|
||
}
|
||
else if (layoutElementJsonData.type == "Text")
|
||
{
|
||
ColorUtility.TryParseHtmlString("#" + layoutElementJsonData.color, out var fontColor);
|
||
Color strokeColor = Color.white;
|
||
if (!string.IsNullOrEmpty(layoutElementJsonData.strokeColor))
|
||
ColorUtility.TryParseHtmlString("#" + layoutElementJsonData.strokeColor, out strokeColor);
|
||
|
||
elementInfos.Add(new LayoutInfo.TextInfo()
|
||
{
|
||
name = layoutElementJsonData.name,
|
||
text = layoutElementJsonData.text,
|
||
font = layoutElementJsonData.font,
|
||
strokeSize = layoutElementJsonData.strokeSize,
|
||
strokeColor = strokeColor,
|
||
|
||
size = layoutElementJsonData.size,
|
||
align = layoutElementJsonData.align,
|
||
color = fontColor,
|
||
x = layoutElementJsonData.x,
|
||
y = layoutElementJsonData.y,
|
||
w = layoutElementJsonData.w,
|
||
h = layoutElementJsonData.h,
|
||
layoutInfo = layoutInfo
|
||
});
|
||
}
|
||
|
||
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;
|
||
|
||
// ͼƬ
|
||
public string imageName;
|
||
public bool slice; // <20>Ƿ<EFBFBD>Ϊ<EFBFBD>Ź<EFBFBD>
|
||
|
||
// <20>ı<EFBFBD>
|
||
public string text;
|
||
public float size;
|
||
public string color;
|
||
public string align;
|
||
public string font;
|
||
public float strokeSize;
|
||
public string strokeColor;
|
||
}
|
||
}
|
||
|
||
#endif |