I created an editor window control in Unity to easily edit the PlayerInfo object in my game without changing code. You can edit the object in the window and save it out a serialized file that the game can read. I was originally doing this manually by adding each field to the control when I edited the PlayerInfo object. I would like to use Reflection to have this done automatically when the PlayerInfo object changes. However, my attempt to do this causes Unity to crash (not the script or my game, Unity itself crashes). This is what I have been trying to do…
Type type = typeof(PlayerInfo);
var fields = type.GetFields();
foreach(FieldInfo field in fields)
{
object temp = field.GetValue(playerInfo);
if(temp is string)
{
field.SetValue(EditorGUILayout.TextField(field.Name + ": ", (string)temp), (string)temp);
}
else if(temp is int)
{
field.SetValue(EditorGUILayout.IntSlider(field.Name + ": ", (int)temp, -1, 100),(int)temp);
}
else if(temp is float)
{
field.SetValue(EditorGUILayout.Slider(field.Name + ": ", (float)temp, 0f, 200f), (float)temp);
}
}