我正在开发一个 OpenGL 游戏引擎作为一个激情项目,我使用 UI 库“Dear ImGUI”来显示和调试类似于 Unity 检查器的值。我很难想出一种方法来获取对我正在尝试调试的字段的引用。
这是我目前得到的代码,但问题是它不是对实际字段的引用,它只是对局部变量(值)的引用,因此,它实际上并没有设置我在图形用户界面。从我所看到的来看,没有明确的方法来获取参考。
protected override void OnImGUIRender(FrameEventArgs e)
{
ImGui.PushFont(font);
ImGui.ShowDemoWindow();
//Scene Window
{
ImGui.Begin("Scene");
ImGui.BeginTabBar("1");
ImGui.BeginTabItem("Heirachy");
if (ImGui.TreeNode("Scene"))
{
foreach (var obj in (LayerStack.Layers.FirstOrDefault(x => x.GetType() == typeof(GameLayer)) as GameLayer).scene.GameObjects)
{
if (ImGui.Selectable(obj.Name))
selectedGameObject = obj;
}
ImGui.TreePop();
}
ImGui.EndTabItem();
ImGui.EndTabBar();
ImGui.Dummy(new System.Numerics.Vector2(0, 40));
ImGui.BeginTabBar("Properties");
ImGui.BeginTabItem("Properties");
if (selectedGameObject != null)
{
ImGui.Text(selectedGameObject.Name);
foreach (var c in selectedGameObject.components)
{
if (ImGui.TreeNode(c.GetType().Name))
{
var fields = c.GetType().GetFields();
foreach (var field in fields)
{
ImGui.DragFloat3(field.Name, field.refValue); <-- Focused line
}
ImGui.TreePop();
}
}
}
else
ImGui.Text("No Currently Selected Gameobject");
ImGui.EndTabItem();
ImGui.EndTabBar();
ImGui.End();
ImGui.Begin("Debug");
ImGui.Text("Gameobjects: " + LayerStack.GameObjectCount);
ImGui.End();
}
base.OnImGUIRender(e);
}
有什么方法可以引用在 foreach 中循环的实际字段?在我的脑海中,我会想象它看起来像这样:
ImGui.DragFloat3(field.Name, field.Reference);
谢谢!
编辑:
我发现我的个人解决方案在下面的代码中,但非常感谢@pinkfloydx33 帮助我更好地理解问题并提供高质量的答案。
var fields = c.GetType().GetFields();
foreach (var field in fields)
{
var value = (field.FieldType)field.GetValue(c);
ImGui.DragFloat3(field.Name, field.refValue);
field.SetValue(c, value);
}