包含此页的版本:
不含此页的版本:
您可以使用自己的控件自定义渲染调试器窗口,并脚本一段代码,允许您创建自己的组件、触发游戏事件、随时间修改组件属性以及以您喜欢的任何方式响应用户输入。更多信息
请参阅术语表,以可视化项目的光照、渲染或材质属性。
渲染调试器窗口包含多个选项卡(“面板”)。选择面板时,窗口会显示一个或多个控件(“小部件”)。
要创建微件并将其添加到新面板,请执行以下作:
using UnityEngine.Rendering;以包含UnityEngine.RenderingNamespace。DebugUI.Button.onValueChangedcallback,当您更改控件中的值时,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;
}
}
将脚本添加到游戏对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
请参阅术语表.您应该会注意到 渲染调试器(Rendering Debugger) 窗口中出现了一个新的 我的自定义面板(My Custom Panel) 面板。
要获取现有面板,请使用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);
}
}