Version: 6000.3
语言: 中文
设置渲染管线
Unity 中的渲染路径

更改或检测活动渲染管线

此页面包含有关如何获取、设置和配置渲染管线 获取场景内容并将其显示在屏幕上的一系列作。Unity 允许您从预构建的渲染管道中进行选择,或编写自己的渲染管道。更多信息
请参阅术语表
Unity 当前正在使用的。Unity 当前使用的渲染管线称为活动渲染管线。

概述

要渲染内容,Unity 可以使用内置渲染管线或基于可编写脚本渲染管线 (SRP) 的渲染管线,其中包括通用渲染管线 (URP) 和高清渲染管线 (HDRP)。

要指定Unity使用的可编写脚本的渲染管线,请使用渲染管线资产。渲染管线资产告诉 Unity 使用哪个 SRP 以及如何配置它。如果未指定渲染管线资产,Unity 将使用内置渲染管线。

你可以创建多个渲染管线资产,这些资产使用相同的渲染管线,但配置不同;例如,你可以为不同的硬件质量级别使用不同的渲染管线资产。有关渲染管线资产的一般介绍,请参阅可编写脚本的渲染管线简介。有关 URP 的渲染管线资产的信息,请参阅通用渲染管线资产,有关 HDRP 的渲染管线资产,请参阅高清渲染管线资产

一旦您在 Unity 编辑器中或运行时更改活动渲染管线资产,Unity 就会使用新的活动渲染管线来渲染内容。如果您在 Unity 编辑器中,这包括 Game 视图、场景场景包含游戏的环境和菜单。将每个唯一的场景文件视为一个独特的关卡。在每个场景中,你放置你的环境、障碍物和装饰品,基本上是将你的游戏设计和构建成碎片。更多信息
请参阅术语表
在“项目”面板和检查器一个 Unity 窗口,显示有关当前选定游戏对象、资产或项目设置的信息,允许您检查和编辑值。更多信息
请参阅术语表
.

更改活动渲染管线时,必须确保项目中的资产和代码与新的渲染管线兼容;否则,您可能会遇到错误或意外的视觉效果。

确定活动渲染管线

图形设置质量设置中的设置确定活动渲染管线。

对于 Quality settings 窗口中的每个质量级别,Unity 使用分配给 Render Pipeline Asset 的 Render Pipeline Asset。如果未分配该属性,Unity 会改用分配给 Graphics settings 窗口中 Default Render Pipeline Asset 的 Render Pipeline Asset。

如果未同时设置 Render Pipeline AssetDefault Render Pipeline,则 Unity 会使用内置渲染管线。

如何在编辑器UI中设置活动渲染管线

激活内置渲染管线

要将活动渲染管线设置为 内置渲染管线(Built-in Render Pipeline),请从图形设置和质量设置中删除所有渲染管线资产。

为此,请执行以下作:

  1. 选择 编辑>项目设置(Project Settings) > 质量(Quality) 。
  2. 对于每个质量级别,如果渲染管线资产已分配给渲染管线资产,请取消分配。
  3. 选择 编辑(Edit) > 项目设置(Project Settings) > 图形(Graphics)。
  4. 如果渲染管线资产已分配给 默认渲染管线(Default Render Pipeline),请取消分配。

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

要将活动渲染管线设置为基于 SRP 的渲染管线,请告诉 Unity 要将哪个渲染管线资产用作默认活动渲染管线,以及可选地为每个质量级别使用哪个渲染管线资产。

为此,请执行以下作:

  1. 在 项目(Project) 文件夹中,找到要使用的渲染管线资产。
  2. 设置默认渲染管线,当给定质量级别没有覆盖时,Unity 会使用该管线。如果不设置此设置,Unity 会在未应用覆盖时使用内置渲染管线。
    1. 选择 编辑(Edit) > 项目设置(Project Settings) > 图形(Graphics)。
    2. 默认渲染管线(Default Render Pipeline) 设置为要使用的渲染管线资产。
  3. 可选:为不同的质量级别设置覆盖渲染管线资产。
    1. 选择 编辑>项目设置(Project Settings) > 质量(Quality) 。
    2. 渲染管线资产(Render Pipeline Asset) 设置为要使用的渲染管线资产。

如何在 C# 脚本中获取和设置活动渲染管道

在 C# 中脚本一段代码,允许您创建自己的组件、触发游戏事件、随时间修改组件属性以及以您喜欢的任何方式响应用户输入。更多信息
请参阅术语表
,您可以获取和设置活动渲染管线,并在活动渲染管线发生变化时接收回调。您可以在 Unity 编辑器的编辑模式或播放模式下执行此作,也可以在应用程序的运行时执行此作。

为此,请使用以下 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 中的渲染路径