56 lines
1.4 KiB
C#
56 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
public class OptionBase
|
|
{
|
|
public string section;
|
|
public Dictionary<string, string> options= new Dictionary<string, string>();
|
|
public MonoBehaviour target;
|
|
public OptionBase(MonoBehaviour mb)
|
|
{
|
|
target = mb;
|
|
var targetFields = mb.GetType().GetAllFields();
|
|
foreach (var f in targetFields)
|
|
{
|
|
var attributes = f.GetCustomAttributes(typeof(OptionAttribute), false);
|
|
if (attributes.Count() != 0)
|
|
{
|
|
switch (attributes[0])
|
|
{
|
|
case OptionKeyAttribute ok:
|
|
options.Add(f.Name, f.GetValue(mb).ToString());
|
|
break;
|
|
case OptionSectionAttribute os:
|
|
section = f.Name;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public void SetValue(MonoBehaviour mb)
|
|
{
|
|
var t = mb.GetType();
|
|
foreach(var option in options)
|
|
{
|
|
var field = t.GetAllField(option.Key);
|
|
field.SetValue(mb, option.Value);
|
|
}
|
|
}
|
|
}
|
|
|
|
[AttributeUsage(AttributeTargets.Field )]
|
|
public class OptionKeyAttribute : OptionAttribute
|
|
{
|
|
}
|
|
|
|
|
|
[AttributeUsage(AttributeTargets.Field)]
|
|
public class OptionSectionAttribute: OptionAttribute
|
|
{
|
|
|
|
}
|
|
|
|
[AttributeUsage(AttributeTargets.Field)]
|
|
public class OptionAttribute : Attribute { } |