您可以使用自己的控件和脚本自定义渲染调试器 (Rendering Debugger) 窗口,以可视化项目的光照、渲染或材质属性。
渲染调试器 (Rendering Debugger) 窗口包含多个选项卡(“面板”)。当选择一个面板时,窗口会显示一个或多个控件(“小部件”)。
要创建小部件并将其添加到新面板,请执行以下操作:
using UnityEngine.Rendering; 来包含 UnityEngine.Rendering 命名空间。DebugUI.Button)来创建小部件。onValueChanged 回调,Unity 会在您更改小部件中的值时调用该回调。如果将 2 个或更多个小部件添加数组中,面板将按照与在数组中的相同顺序显示小部件。
以下代码示例创建并添加一个用于启用或禁用主方向光的小部件:
using UnityEngine;
using UnityEngine.Rendering;
using System.Collections.Generic;
using System.Linq;
[ExecuteInEditMode]
public class CustomDebugPanel : MonoBehaviour
{
static bool lightEnabled = true;
void OnEnable()
{
// Create a list of widgets
var widgetList = new List<DebugUI.Widget>();
// Add a checkbox widget to the list of widgets
widgetList.AddRange(new DebugUI.Widget[]
{
new DebugUI.BoolField
{
displayName = "Enable main directional light",
tooltip = "Enable or disable the main directional light",
getter = () => lightEnabled,
// When the widget value is changed, change the value of lightEnabled
setter = value => lightEnabled = value,
// Run a custom function to enable or disable the main directional light based on the widget value
onValueChanged = DisplaySunChanged
},
});
// Create a new panel (tab) in the Rendering Debugger
var panel = DebugManager.instance.GetPanel("My Custom Panel", createIfNull: true);
// Add the widgets to the panel
panel.children.Add(widgetList.ToArray());
}
// Remove the custom panel if the GameObject is disabled
void OnDisable()
{
DebugManager.instance.RemovePanel("My Custom Panel");
}
// Enable or disable the main directional light based on the widget value
void DisplaySunChanged(DebugUI.Field<bool> field, bool displaySun)
{
Light sun = FindObjectsOfType<Light>().Where(x => x.type == LightType.Directional).FirstOrDefault();
if (sun)
sun.enabled = displaySun;
}
}
将脚本添加到游戏对象。您应可在渲染调试器 (Rendering Debugger) 窗口中看到一个新的我的自定义面板 (My Custom Pane) 面板。
要获取现有面板,请使用带有面板名称的 DebugManager.instance.GetPanel。将 createIfNull 设置为 false,这样一来,如果名称与现有面板不匹配,您就不会意外创建新面板。
以下代码示例从上面的代码示例中获取面板:
var panel = DebugManager.instance.GetPanel("My Custom Panel", createIfNull: false);
您不应该将小部件添加到 URP 的内置渲染调试器面板。
可以使用容器来一起显示多组小部件。
DebugUI.Foldout)创建容器。Add 方法添加小部件数组。以下代码示例创建一个包含 2 个复选框的可折叠容器:
using UnityEngine;
using UnityEngine.Rendering;
using System.Collections.Generic;
[ExecuteInEditMode]
public class CustomDebugPanelWithContainer : MonoBehaviour
{
void OnEnable()
{
// Create a list of widgets
var widgetList = new List<DebugUI.Widget>();
// Add 2 checkbox widgets to the list of widgets
widgetList.AddRange(new DebugUI.Widget[]
{
new DebugUI.BoolField
{
displayName = "Visualisation 1",
},
new DebugUI.BoolField
{
displayName = "Visualisation 2",
},
});
// Create a container
var container = new DebugUI.Foldout
{
displayName = "My Container"
};
// Add the widgets to the container
container.children.Add(widgetList.ToArray());
// Create a new panel (tab) in the Rendering Debugger
var panel = DebugManager.instance.GetPanel("My Custom Panel With Container", createIfNull: true);
// Add the container to the panel
panel.children.Add(container);
}
}