本页面包含了有关如何获取、设置和配置 Unity 当前使用的渲染管线的信息。Unity 当前使用的渲染管线称为激活的渲染管线。
要渲染内容,Unity 可以使用内置渲染管线或基于可编程渲染管线 (SRP) 的渲染管线,其中包括了通用渲染管线 (URP) 和高清渲染管线 (HDRP)。
要指定 Unity 使用的可编程渲染管线,请使用渲染管线资源。渲染管线资源会告知 Unity 要使用的 SRP 以及如何进行配置。如果未指定渲染管线资源,Unity 将使用内置渲染管线。
可以创建多个使用相同渲染管线但具有不同配置的渲染管线资源;例如,可以为不同的硬件质量级别使用不同的渲染管线资源。有关渲染管线资源的一般介绍,请参阅可编程渲染管线简介。有关 URP 渲染管线资源的信息,请参阅通用渲染管线资源,有关 HDRP 渲染管线资源的信息,请参阅高清渲染管线资源。
只要在 Unity 编辑器中或在运行时更改激活的渲染管线,Unity 就会使用新的激活的渲染管线来渲染内容。如果是在 Unity 编辑器中,则这包括游戏 (Game) 视图、场景 (Scene) 视图,以及项目 (Project) 面板和检视面板 (Inspector) 中的材质预览。
在更改激活的渲染管线时,必须确保项目中的资源和代码兼容新的渲染管线,否则,可能会遇到错误或意外的视觉效果。
图形设置和质量设置中的设置确定了激活的渲染管线。在图形设置中,可以配置 Unity 默认使用的渲染管线。在质量设置中,可以覆盖特定质量级别的默认渲染管线。
要获取或设置默认渲染管线,请使用图形设置 (Graphics Settings) > 可编程渲染管线设置 (Scriptable Render Pipeline Setting)(或其等效 API GraphicsSettings.defaultRenderPipeline)。要获取或设置覆盖特定质量级别默认项的渲染管线,请执行质量设置 (Quality settings) > 渲染管线 (Render Pipeline)(或其等效 API QualitySettings.renderPipeline)。
Unity 按如下方式确定激活的渲染管线:
要在编辑器__ UI__(即用户界面,User Interface)让用户能够与您的应用程序进行交互。Unity 目前支持三种 UI 系统。更多信息
See in Glossary 中获取激活的渲染管线,必须检查图形设置和质量设置窗口。有关如何使用这些值来确定激活的渲染管线的信息,请参阅确定激活的渲染管线。
要将激活的渲染管线设置为内置渲染管线,请从图形设置和质量设置中删除任何渲染管线资源。
为此需要执行以下操作:
要将激活的渲染管线设置为基于 SRP 的渲染管线,则请告知 Unity 要用作默认激活的渲染管线的渲染管线资源,以及要用于每个质量级别的渲染管线资源(可选)。
为此需要执行以下操作:
在 C# 脚本中,可以获取和设置激活的渲染管线,并在激活的渲染管线发生更改时接收回调。可以在 Unity 编辑器中以编辑模式 (Edit Mode) 或播放模式 (Play Mode) 执行此操作,也可以在应用程序运行时进行。
要执行此操作,请使用以下 API:
以下示例代码展示了如何使用这些 API:
using UnityEngine;
using UnityEngine.Rendering;
public class ActiveRenderPipelineExample : MonoBehaviour
{
// In the Inspector, assign a Render Pipeline Asset to each of these fields
public RenderPipelineAsset defaultRenderPipelineAsset;
public RenderPipelineAsset overrideRenderPipelineAsset;
void Start()
{
GraphicsSettings.defaultRenderPipeline = defaultRenderPipelineAsset;
QualitySettings.renderPipeline = overrideRenderPipelineAsset;
DisplayCurrentRenderPipeline();
}
void Update()
{
// When the user presses the left shift key, switch the default render pipeline
if (Input.GetKeyDown(KeyCode.LeftShift)) {
SwitchDefaultRenderPipeline();
DisplayCurrentRenderPipeline();
}
// When the user presses the right shift key, switch the override render pipeline
else if (Input.GetKeyDown(KeyCode.RightShift)) {
SwitchOverrideRenderPipeline();
DisplayCurrentRenderPipeline();
}
}
// Switch the default render pipeline between null,
// and the render pipeline defined in defaultRenderPipelineAsset
void SwitchDefaultRenderPipeline()
{
if (GraphicsSettings.defaultRenderPipeline == defaultRenderPipelineAsset)
{
GraphicsSettings.defaultRenderPipeline = null;
}
else
{
GraphicsSettings.defaultRenderPipeline = defaultRenderPipelineAsset;
}
}
// Switch the override render pipeline between null,
// and the render pipeline defined in overrideRenderPipelineAsset
void SwitchOverrideRenderPipeline()
{
if (QualitySettings.renderPipeline == overrideRenderPipelineAsset)
{
QualitySettings.renderPipeline = null;
}
else
{
QualitySettings.renderPipeline = overrideRenderPipelineAsset;
}
}
// Print the current render pipeline information to the console
void DisplayCurrentRenderPipeline()
{
// GraphicsSettings.defaultRenderPipeline determines the default render pipeline
// If it is null, the default is the Built-in Render Pipeline
if (GraphicsSettings.defaultRenderPipeline != null)
{
Debug.Log("The default render pipeline is defined by " + GraphicsSettings.defaultRenderPipeline.name);
}
else
{
Debug.Log("The default render pipeline is the Built-in Render Pipeline");
}
// QualitySettings.renderPipeline determines the override render pipeline for the current quality level
// If it is null, no override exists for the current quality level
if (QualitySettings.renderPipeline != null)
{
Debug.Log("The override render pipeline for the current quality level is defined by " + QualitySettings.renderPipeline.name);
}
else
{
Debug.Log("No override render pipeline exists for the current quality level");
}
// If an override render pipeline is defined, Unity uses that
// Otherwise, it falls back to the default value
if (QualitySettings.renderPipeline != null)
{
Debug.Log("The active render pipeline is the override render pipeline");
}
else
{
Debug.Log("The active render pipeline is the default render pipeline");
}
// To get a reference to the Render Pipeline Asset that defines the active render pipeline,
// without knowing if it is the default or an override, use GraphicsSettings.currentRenderPipeline
if (GraphicsSettings.currentRenderPipeline != null)
{
Debug.Log("The active render pipeline is defined by " +GraphicsSettings.currentRenderPipeline.name);
}
else
{
Debug.Log("The active render pipeline is the Built-in Render Pipeline");
}
}
}