Change the active URP asset at runtime
To accommodate different hardware specifications, swap between quality levels and their associated URP assets via a C# script.
Read time 4 minutesLast updated a month ago
Unity has several preset levels of Quality settings and you might add more to your project. To accommodate different hardware specifications, you can switch between these levels and the associated URP asset from C# scripts. The following examples show how to use API to change Quality setting levels and the active URP asset, and how to change specific settings in the URP asset at runtime.
Change the active URP asset
Each quality level uses a URP asset to control many of the specific graphics settings. You can assigning different URP assets to each quality level and switch between them at runtime.
Configure Project Quality settings
To use Quality settings to switch between URP assets, ensure that the quality levels of your project are configured to use different URP assets. The URP 3D Sample scene has this configuration by default.
-
Create a URP asset for each quality level. To do this, right-click in the Project window and select Create > Rendering > URP Asset (with Universal Renderer).
-
Configure and name the new URP assets as necessary.
-
Open the Quality section in the Project Settings (Edit >> Quality).Project Settings
?
-
Assign each URP asset to a quality level. To do this, select a quality level from the Levels list, then go to Rendering > Render Pipeline Asset and choose the URP asset you created for this quality level. Do this for each quality level.
The quality levels of your project are now ready to be used to change between URP assets at runtime.
Change Quality Level
You can change the quality level Unity uses at runtime through the QualitySettings API. With the quality levels setup as shown previously, this enables you to switch between URP assets as well as Quality settings presets.
In the following simple example, the C# script uses the system's total Graphics Memory to determine the appropriate quality level without any input from the user when they open the built project.
-
Create a new C# script with the name QualityControls.
-
Open the QualityControls script and add themethod to the
SwitchQualityLevelclass.QualityControlsusing System.Collections;using System.Collections.Generic;using UnityEngine;public class QualityControls : MonoBehaviour{ void Start() { } private void SwitchQualityLevel() { }} -
Add astatement in the
switchmethod to select the quality level with theSwitchQualityLevelmethod as shown below.QualitySettings.SetQualityLevel()using System.Collections;using System.Collections.Generic;using UnityEngine;public class QualityControls : MonoBehaviour{ void Start() { } private void SwitchQualityLevel() { // Select Quality settings level (URP asset) based on the size of the device's graphics memory switch (SystemInfo.graphicsMemorySize) { case <= 2048: QualitySettings.SetQualityLevel(1); break; case <= 4096: QualitySettings.SetQualityLevel(2); break; default: QualitySettings.SetQualityLevel(0); break; } }} -
Add a call to themethod in the
SwitchQualityLevelmethod. This ensures that the quality level only changes when the scene first loads.Startusing System.Collections;using System.Collections.Generic;using UnityEngine;public class QualityControls : MonoBehaviour{ void Start() { SwitchQualityLevel(); } private void SwitchQualityLevel() { // Select Quality settings level (URP asset) based on the size of the device's graphics memory switch (SystemInfo.graphicsMemorySize) { case <= 2048: QualitySettings.SetQualityLevel(1); break; case <= 4096: QualitySettings.SetQualityLevel(2); break; default: QualitySettings.SetQualityLevel(0); break; } }} -
Open the first scene that your built project loads on startup.
-
Create an empty GameObject and call it QualityController. To do this, right-click in the Hierarchy Window and select Create Empty.
-
Open the QualityController object in the Inspector.
-
Add the QualityControls script to the QualityController as a component.
Now when this scene loads, Unity runs the method in the QualityControls script which detects the system's total graphics memory and sets the quality level. The quality level sets the URP asset as the active Render Pipeline asset.
SwitchQualityLevelYou can create more complex systems and sequences of checks to determine which quality level to use, but the fundamental process remains the same. When the project starts, run a script which uses to select a quality level and through that select the URP asset for the project to use at runtime.
QualitySettings.SetQualityLevel