87 lines
2.9 KiB
C#
87 lines
2.9 KiB
C#
using UnityEngine;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Octopus.Simulator
|
|
{
|
|
public class Panel_LogicData : MonoBehaviour
|
|
{
|
|
List<LogicDataInfoItem> infoItems = new List<LogicDataInfoItem>();
|
|
[SerializeField]
|
|
LogicDataInfoItem logicItem;
|
|
RectTransform rect;
|
|
[SerializeField]
|
|
RectTransform contentRect;
|
|
|
|
float panelPadding = 25f;
|
|
public event Action onPanelUpdated;
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
private void Awake()
|
|
{
|
|
rect = GetComponent<RectTransform>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
var logicUIManager = FindAnyObjectByType<LogicUIManager>();
|
|
logicUIManager.onLogicItemSelected += SetLogicDataItem;
|
|
logicUIManager.onLogicItemDeSelected += UnsetLogicDataItem;
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
void ClearItem()
|
|
{
|
|
foreach (var infoItem in infoItems)
|
|
{
|
|
Destroy(infoItem.gameObject);
|
|
}
|
|
infoItems.Clear();
|
|
}
|
|
|
|
public void SetLogicDataItem(ILogicItem item)
|
|
{
|
|
gameObject.SetActive(true);
|
|
ClearItem();
|
|
|
|
Type type = item.GetType();
|
|
switch (item.ItemType)
|
|
{
|
|
case LogicItemType.Queue:
|
|
type = (item as LogicQueue).GetType();
|
|
break;
|
|
case LogicItemType.Resource:
|
|
type = (item as LogicResource).GetType();
|
|
break;
|
|
case LogicItemType.Component:
|
|
type = (item as ILogicComponent).GetType();
|
|
break;
|
|
}
|
|
FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
|
|
float itemHeight = logicItem.GetComponent<RectTransform>().rect.height;
|
|
SetRectHeight(fields.Length, itemHeight);
|
|
for (int i = 0; i < fields.Length; i++)
|
|
{
|
|
var rawValue = fields[i].GetValue(item);
|
|
var value = rawValue?.ToString() ?? "";
|
|
var iLogicItem = Instantiate(logicItem, rect);
|
|
iLogicItem.GetComponent<RectTransform>().anchoredPosition = new Vector2(iLogicItem.GetComponent<RectTransform>().anchoredPosition.x, -itemHeight * i);
|
|
iLogicItem.Set(fields[i].Name, value);
|
|
infoItems.Add(iLogicItem);
|
|
}
|
|
onPanelUpdated?.Invoke();
|
|
}
|
|
|
|
void SetRectHeight(int count,float itemHeight)
|
|
{
|
|
float panelHeight = count * itemHeight + panelPadding;
|
|
rect.sizeDelta = new Vector2(rect.sizeDelta.x, panelHeight);
|
|
}
|
|
|
|
public void UnsetLogicDataItem()
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
} |