samedi 6 juin 2020

Get Reference to Field from Reflection

I'm working on an OpenGL game engine as a passion project and im using the UI library "Dear ImGUI" to display and debug values similar to Unity's inspector. I'm having trouble thinking of a way to get a reference to the field I'm trying to debug.

Here is the code i have got at the moment but the problem is that its not a reference to the actual field, its just a reference to a local variable (value) and as such, it doesnt actually set the variable that I debug in the GUI. From what i've been able to see, there is no clean cut way to get the reference.

var fields = c.GetType().GetFields();

foreach (var field in fields)
{
    if (field.FieldType == typeof(System.Numerics.Vector3)) 
    {
        var value = (System.Numerics.Vector3)field.GetValue(c);
        ImGui.DragFloat3(field.Name, ref value);
    }
    if (field.FieldType == typeof(bool)) 
    { 
        var value = (bool)field.GetValue(c); 
        ImGui.Checkbox(field.Name, ref value); 
    }
}

Is there any way I can get a reference to the actual field that is being looped over in the foreach? In my head I would imagine it looking something like this:

ImGui.DragFloat3(field.Name, field.Reference);

Thanks!





Aucun commentaire:

Enregistrer un commentaire