您可以使用自己的控件和脚本一段代码,允许您创建自己的组件、触发游戏事件、随着时间的推移修改组件属性并以您喜欢的任何方式响应用户输入。 了解更多信息
在词汇表中查看自定义渲染调试器窗口,以可视化项目的照明、渲染或材质属性。
渲染调试器窗口包含多个选项卡(“面板”)。选择一个面板时,窗口将显示一个或多个控件(“窗口部件”)。
要创建窗口部件并将其添加到新面板,请执行以下操作
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;
}
}
将脚本添加到游戏对象Unity 场景中的基本对象,可以表示角色、道具、场景、相机、路径点等。游戏对象的功用由附加到它的组件定义。 了解更多信息
在词汇表中查看。您应该在渲染调试器窗口中注意到一个新的我的自定义面板面板。
要获取现有面板,请使用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);
}
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.