Version: Unity 6.0 (6000.0)
语言 : 中文
设置渲染管线
Unity 中的渲染路径

更改或检测激活的渲染管线

本页面包含了有关如何获取、设置和配置 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 按如下方式确定激活的渲染管线:

  • 如果当前质量设置级别 > 渲染管线引用了渲染管线资源,则 Unity 将使用该值。
  • 否则:
    • 如果图形设置 > 可编程渲染管线设置引用了渲染管线资源,则 Unity 会使用该值。
    • 否则,Unity 将使用内置渲染管线。

如何在编辑器 UI 中获取和设置激活的渲染管线

获取激活的渲染管线

要在编辑器__ UI__(即用户界面,User Interface)让用户能够与您的应用程序进行交互。Unity 目前支持三种 UI 系统。更多信息
See in Glossary
中获取激活的渲染管线,必须检查图形设置和质量设置窗口。有关如何使用这些值来确定激活的渲染管线的信息,请参阅确定激活的渲染管线

激活内置渲染管线

要将激活的渲染管线设置为内置渲染管线,请从图形设置和质量设置中删除任何渲染管线资源。

为此需要执行以下操作:

  1. 选择编辑 (Edit) > 项目设置 (Project Settings) > 质量 (Quality)
  2. 对于每个质量级别,如果将渲染管线资源分配给了渲染管线 (Render Pipeline) 字段,则请取消分配。
  3. 选择编辑 (Edit) > 项目设置 (Project Settings) > 图形 (Graphics)
  4. 如果已将渲染管线资源分配给了可编程渲染管线设置 (Scriptable Render Pipeline Setting) 字段,则请取消分配。

激活基于 SRP 的 URP、HDRP 或自定义渲染管线

要将激活的渲染管线设置为基于 SRP 的渲染管线,则请告知 Unity 要用作默认激活的渲染管线的渲染管线资源,以及要用于每个质量级别的渲染管线资源(可选)。

为此需要执行以下操作:

  1. 在 Project 文件夹中,找到要使用的渲染管线资源。
  2. 设置默认渲染管线,Unity 在特定质量级别未被覆盖时将使用该管线。如果未设置此项,则 Unity 会在未进行覆盖时使用内置渲染管线。
    1. 选择编辑 (Edit) > 项目设置 (Project Settings) > 图形 (Graphics)
    2. 将渲染管线资源拖到可编程渲染管线设置 (Scriptable Render Pipeline Setting) 字段上。
  3. 可选:为不同的质量级别设置覆盖渲染管线资源。
    1. 选择编辑 (Edit) > 项目设置 (Project Settings) > 质量 (Quality)
    2. 将渲染管线资源拖到渲染管线 (Render Pipeline) 字段上。

如何在 C# 脚本中获取和设置激活的渲染管线

在 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");
        }
    }
}
设置渲染管线
Unity 中的渲染路径