1
This commit is contained in:
96
Assets/BITKit/Unity/Scripts/UX/Library/JsonBasedTable.cs
Normal file
96
Assets/BITKit/Unity/Scripts/UX/Library/JsonBasedTable.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit.UX
|
||||
{
|
||||
/// <summary>
|
||||
/// 基于Json的Table生成元素
|
||||
/// </summary>
|
||||
public class JsonBasedTable : VisualElement
|
||||
{
|
||||
public new class UxmlTraits : VisualElement.UxmlTraits
|
||||
{
|
||||
private readonly UxmlStringAttributeDescription m_JsonAttribute = new ()
|
||||
{
|
||||
name = "Json"
|
||||
};
|
||||
private readonly UxmlBoolAttributeDescription m_allowWarningsAttribute = new ()
|
||||
{
|
||||
name = "allowWarnings",
|
||||
defaultValue = false
|
||||
};
|
||||
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
|
||||
{
|
||||
base.Init(ve, bag, cc);
|
||||
var table = (JsonBasedTable)ve;
|
||||
table.Json = m_JsonAttribute.GetValueFromBag(bag, cc);
|
||||
table.AllowWarning = m_allowWarningsAttribute.GetValueFromBag(bag, cc);
|
||||
}
|
||||
}
|
||||
public new class UxmlFactory : UxmlFactory<JsonBasedTable, UxmlTraits> { }
|
||||
private string _json;
|
||||
|
||||
public string Json
|
||||
{
|
||||
get => _json;
|
||||
set
|
||||
{
|
||||
_json = value;
|
||||
ApplyJson();
|
||||
}
|
||||
}
|
||||
public bool AllowWarning { get; set; }
|
||||
private readonly List<VisualElement> instanceColumns = new();
|
||||
private void ApplyJson()
|
||||
{
|
||||
try
|
||||
{
|
||||
Clear();
|
||||
style.flexDirection = FlexDirection.Row;
|
||||
style.justifyContent = Justify.SpaceAround;
|
||||
instanceColumns.Clear();
|
||||
|
||||
if (string.IsNullOrEmpty(Json)) return;
|
||||
|
||||
var jArray = JArray.Parse(Json);
|
||||
var colLength = jArray.Max(x => x.Count());
|
||||
var rowLength = jArray.Count;
|
||||
for (var i = 0; i < colLength; i++)
|
||||
{
|
||||
instanceColumns.Add(this.Create<VisualElement>());
|
||||
}
|
||||
for (var y = 0; y < rowLength; y++)
|
||||
{
|
||||
var array = jArray[y] as JArray;
|
||||
for (var x = 0; x < colLength; x++)
|
||||
{
|
||||
var instance = instanceColumns[x];
|
||||
if (x >= array!.Count)
|
||||
{
|
||||
instance.Create<VisualElement>();
|
||||
}
|
||||
else
|
||||
{
|
||||
var label = instance.Create<Label>();
|
||||
label.text = array[x].ToString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (AllowWarning)
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user