Version: 6000.3
语言: 中文
在URP中启用渲染调试器
URP 的渲染调试器窗口参考

在 URP 中向渲染调试器添加控件

您可以使用自己的控件自定义渲染调试器窗口,并脚本一段代码,允许您创建自己的组件、触发游戏事件、随时间修改组件属性以及以您喜欢的任何方式响应用户输入。更多信息
请参阅术语表
,以可视化项目的光照、渲染或材质属性。

渲染调试器窗口包含多个选项卡(“面板”)。选择面板时,窗口会显示一个或多个控件(“小部件”)。

要创建微件并将其添加到新面板,请执行以下作:

  1. 创建使用using UnityEngine.Rendering;以包含UnityEngine.RenderingNamespace。
  2. 例如,通过创建 DebugUI.Widget 子类的实例来创建控件DebugUI.Button.
  3. 在小组件中,实现onValueChangedcallback,当您更改控件中的值时,Unity 会调用该回调。
  4. 使用 DebugUI.instance.GetPanel 创建面板。
  5. 将小部件添加到数组。
  6. 将小组件数组添加到面板中的子级列表中。

如果将 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;
    }
}

将脚本添加到游戏对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
请参阅术语表
.您应该会注意到 渲染调试器(Rendering Debugger) 窗口中出现了一个新的 我的自定义面板(My Custom Panel) 面板。

将控件添加到现有面板

要获取现有面板,请使用DebugManager.instance.GetPanel替换为面板名称。设置createIfNullfalse,这样如果名称与现有面板不匹配,您就不会意外创建新面板。

以下代码示例从上面的代码示例中提取面板:

var panel = DebugManager.instance.GetPanel("My Custom Panel", createIfNull: false);

您不应将控件添加到 URP 的内置渲染调试器面板中。

添加容器

您可以使用容器一起显示微件组。

  1. 使用 DebugUI.Container 的子类之一创建容器,例如DebugUI.Foldout.
  2. 使用容器的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);
    }
}
在URP中启用渲染调试器
URP 的渲染调试器窗口参考