解析 layout.txt
This commit is contained in:
parent
743745b839
commit
2d61e2d96f
@ -16,42 +16,4 @@ namespace UguiToolkit.Editor
|
||||
public float rot;
|
||||
public float[] scale;
|
||||
}
|
||||
|
||||
[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> element;
|
||||
}
|
||||
}
|
93
Assets/Editor/Helper/LayoutParser.cs
Normal file
93
Assets/Editor/Helper/LayoutParser.cs
Normal file
@ -0,0 +1,93 @@
|
||||
|
||||
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()
|
||||
{
|
||||
imgPath = layoutElementJsonData.imageName,
|
||||
x = layoutElementJsonData.x,
|
||||
y = layoutElementJsonData.y,
|
||||
w = layoutElementJsonData.w,
|
||||
h = layoutElementJsonData.h,
|
||||
layoutInfo = layoutInfo
|
||||
});
|
||||
}
|
||||
else if (layoutElementJsonData.type == "Text")
|
||||
{
|
||||
// TODO
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
11
Assets/Editor/Helper/LayoutParser.cs.meta
Normal file
11
Assets/Editor/Helper/LayoutParser.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e115306ce3d6eb548b614ed589b175b3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -45,8 +45,19 @@ namespace UguiToolkit.Editor
|
||||
[Serializable]
|
||||
public class LayoutInfo
|
||||
{
|
||||
private List<ElementInfo> m_elementInfos;
|
||||
private float m_w;
|
||||
private float m_h;
|
||||
|
||||
public float W => m_w;
|
||||
public float H => m_h;
|
||||
|
||||
public LayoutInfo(List<ElementInfo> elementInfos, float w, float h)
|
||||
{
|
||||
this.m_elementInfos = elementInfos;
|
||||
this.m_w = w;
|
||||
this.m_h = h;
|
||||
}
|
||||
|
||||
public class ElementInfo
|
||||
{
|
||||
@ -55,8 +66,16 @@ namespace UguiToolkit.Editor
|
||||
public float w;
|
||||
public float h;
|
||||
|
||||
public LayoutInfo layoutInfo;
|
||||
|
||||
public float3 position;
|
||||
public float2 Position {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ImageInfo : ElementInfo
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Sirenix.OdinInspector;
|
||||
using Sirenix.OdinInspector.Editor;
|
||||
using Unity.Mathematics;
|
||||
@ -33,6 +34,7 @@ namespace UguiToolkit.Editor.Windows
|
||||
{
|
||||
panelCache.rotScaleInfos = new();
|
||||
CalcRotScaleInfos();
|
||||
PaserLayout();
|
||||
}
|
||||
|
||||
var cache = GlobalManager.Instance.cache;
|
||||
@ -42,6 +44,22 @@ namespace UguiToolkit.Editor.Windows
|
||||
CloseWindow();
|
||||
}
|
||||
|
||||
[Button("解析Layout.txt", ButtonSizes.Medium), ShowIf(nameof(m_cacheExist))]
|
||||
private void PaserLayout()
|
||||
{
|
||||
if (string.IsNullOrEmpty(m_layoutInfoFilePath))
|
||||
{
|
||||
Debug.LogError("layoutInfoFilePath 为空");
|
||||
return;
|
||||
}
|
||||
ILayoutParser layoutParser = new DefaultLayoutParser();
|
||||
using (StreamReader reader = File.OpenText(m_layoutInfoFilePath))
|
||||
{
|
||||
var jsonData = reader.ReadToEnd();
|
||||
layoutParser.Parser(jsonData);
|
||||
}
|
||||
}
|
||||
|
||||
[Button("重新计算旋转和缩放信息", ButtonSizes.Medium), ShowIf(nameof(m_cacheExist))]
|
||||
private void CalcRotScaleInfos()
|
||||
{
|
||||
@ -103,5 +121,6 @@ namespace UguiToolkit.Editor.Windows
|
||||
_window = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user