您可以使用 C# 脚本在运行时更改 URP 资源的某些属性。这可以帮助微调硬件与项目中任何质量级别不完全匹配的设备的性能。
注意:要使用 C# 脚本更改 URP 资源的属性,该属性必须具有
set方法。有关这些属性的更多信息,请参阅通用渲染管线资源 API。
以下示例使用更改质量级别 (Change Quality Level) 部分中的 QualityControls 脚本和 QualityController 对象,并扩展了查找活动 URP 资源方面的功能以及更改其某些属性以适应硬件的性能级别。
打开 QualityControls 脚本。
在脚本顶部添加 using UnityEngine.Rendering 和 using UnityEngine.Rendering.Universal。
将名称为 ChangeAssetProperties 且类型为 void 的方法添加到 QualityControls 类中,如下所示。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class QualityController : MonoBehaviour
{
void Start()
{
// Select the appropriate Quality Level first
SwitchQualityLevel();
}
private void SwitchQualityLevel()
{
// Code from previous example
}
private void ChangeAssetProperties()
{
// New code is added to this method
}
}
使用如下所示的 GraphicsSettings.currentRenderPipeline 检索激活的渲染管线资源。
注意:必须使用
as关键字将渲染管线资源转换为UniversalRenderPipelineAsset类型,脚本才能正常运行。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class QualityController : MonoBehaviour
{
void Start()
{
// Select the appropriate Quality Level first
SwitchQualityLevel();
}
private void SwitchQualityLevel()
{
// Code from previous example
}
private void ChangeAssetProperties()
{
// Locate the current URP Asset
UniversalRenderPipelineAsset data = GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset;
// Do nothing if Unity can't locate the URP Asset
if (!data) return;
}
}
在 ChangeAssetProperties 方法中添加 switch 语句以设置 URP 资源属性的值。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class QualityController : MonoBehaviour
{
void Start()
{
// Select the appropriate Quality Level first
SwitchQualityLevel();
}
private void SwitchQualityLevel()
{
// Code from previous example
}
private void ChangeAssetProperties()
{
// Locate the current URP Asset
UniversalRenderPipelineAsset data = GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset;
// Do nothing if Unity can't locate the URP Asset
if (!data) return;
// Change URP Asset settings based on the size of the device's graphics memory
switch (SystemInfo.graphicsMemorySize)
{
case <= 1024:
data.renderScale = 0.7f;
data.shadowDistance = 50.0f;
break;
case <= 3072:
data.renderScale = 0.9f;
data.shadowDistance = 150.0f;
break;
default:
data.renderScale = 0.7f;
data.shadowDistance = 25.0f;
break;
}
}
}
在 Start 方法中添加对 ChangeAssetProperties 方法的调用。这样可以确保 URP 资源仅在场景首次加载时更改。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class QualityController : MonoBehaviour
{
void Start()
{
// Select the appropriate Quality Level first
SwitchQualityLevel();
// Fine tune performance with specific URP Asset properties
ChangeAssetProperties();
}
private void SwitchQualityLevel()
{
// Code from previous example
}
private void ChangeAssetProperties()
{
// Locate the current URP Asset
UniversalRenderPipelineAsset data = GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset;
// Do nothing if Unity can't locate the URP Asset
if (!data) return;
// Change URP Asset settings based on the size of the device's graphics memory
switch (SystemInfo.graphicsMemorySize)
{
case <= 1024:
data.renderScale = 0.7f;
data.shadowDistance = 50.0f;
break;
case <= 3072:
data.renderScale = 0.9f;
data.shadowDistance = 150.0f;
break;
default:
data.renderScale = 0.7f;
data.shadowDistance = 25.0f;
break;
}
}
}
现在,当该场景加载时,Unity 会检测系统的总图形内存并相应地设置 URP 资源属性。
您可以使用这种更改特定 URP 资源属性与更改质量级别的方法来微调不同系统的项目性能,而无需为每个目标硬件配置创建质量级别。