Files
crtr/Assets/Cryville/Crtr/SettingsPanel.cs
2022-09-30 17:32:21 +08:00

52 lines
1.6 KiB
C#

using Cryville.Crtr.Browsing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using UnityEngine;
namespace Cryville.Crtr {
public class SettingsPanel : MonoBehaviour {
[SerializeField]
private GameObject m_categoryPrefab;
private Transform _container;
#pragma warning disable IDE0051
void Awake() {
_container = transform.Find("Content/__content__");
}
public void Start() {
LoadProperties();
foreach (Transform c in _container) GameObject.Destroy(c.gameObject);
foreach (var c in _categories) {
var obj = GameObject.Instantiate<GameObject>(m_categoryPrefab);
obj.transform.SetParent(_container, false);
obj.GetComponent<PropertyCategoryPanel>().Load(c.Key, c.Value, Settings.Default);
}
}
#pragma warning restore IDE0051
Dictionary<string, List<PropertyInfo>> _categories = null;
public void LoadProperties() {
if (_categories != null) return;
_categories = new Dictionary<string, List<PropertyInfo>>();
foreach (var p in typeof(Settings).GetProperties()) {
bool browsable = true;
string category = "miscellaneous";
foreach (var attr in p.GetCustomAttributes(true)) {
if (attr is BrowsableAttribute) {
browsable = ((BrowsableAttribute)attr).Browsable;
if (!browsable) break;
}
else if (attr is CategoryAttribute) {
category = ((CategoryAttribute)attr).Category;
}
}
if (!browsable) continue;
if (!_categories.ContainsKey(category))
_categories.Add(category, new List<PropertyInfo>());
_categories[category].Add(p);
}
}
}
}