在可编程渲染管线 (SRP) 中,项目必须具有激活的渲染管线资源。本页包含有关在 Unity Editor 中以及在运行时为项目设置渲染管线资源的信息。
因为 SRP 的可配置性很强,所以更改激活的渲染管线资源可能导致的变化很小(例如使用具有不同质量层的同一渲染管线实例)或者变化很大(例如从通用渲染管线 (URP) 切换到高清渲染管线 (HDRP))。
如果将激活的渲染管线资源更改为一种使用其他渲染管线实例的渲染管线资源,必须确保项目中的资源和代码兼容新的渲染管线实例。否则,可能会遇到错误或意外的视觉效果。
还要注意,切换到新的渲染管线资源会使 Unity 销毁当前的渲染管线实例,并调用新渲染管线资源的 CreatePipeline()
方法。根据 SRP 中的代码,此操作可能在计算上是资源密集型操作。
必须在 Unity Editor 中设置渲染管线资源,以便在编辑项目时可以使用 SRP。在 Editor 中设置的渲染管线资源是 Unity 默认在构建的播放器中使用的渲染管线资源。
在 Graphics Settings 窗口中,设置项目的渲染管线资源。
如果要在渲染管线资源之间切换,可在运行时设置渲染管线资源。您可以在构建的播放器中或在 Editor 的运行模式下执行此操作。
在运行时,应使用 GraphicsSettings.renderPipelineAsset API 来设置渲染管线资源。
以下示例代码显示了如何在两个渲染管线资源之间切换。
using UnityEngine;
using UnityEngine.Rendering;
public class SwitchRenderPipelineAsset : MonoBehaviour
{
public RenderPipelineAsset exampleAssetA;
public RenderPipelineAsset exampleAssetB;
void Update()
{
if (Input.GetKeyDown(KeyCode.A)) {
GraphicsSettings.renderPipelineAsset = exampleAssetA;
Debug.Log("Active render pipeline asset is: " + GraphicsSettings.renderPipelineAsset.name);
}
else if (Input.GetKeyDown(KeyCode.B)) {
GraphicsSettings.renderPipelineAsset = exampleAssetB;
Debug.Log("Active render pipeline asset is: " + GraphicsSettings.renderPipelineAsset.name);
}
}
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.