Last active
September 26, 2025 19:42
-
-
Save liamcary/fd25f2a4b35553e394ccb6357f054ba7 to your computer and use it in GitHub Desktop.
Dynamic Resolution for Oculus Quest with Unity 2021.3 LTS and URP 12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using UnityEngine; | |
| using UnityEngine.Rendering; | |
| using UnityEngine.Rendering.Universal; | |
| using UnityEngine.XR; | |
| public class DynamicResolutionScaler : MonoBehaviour | |
| { | |
| [SerializeField, Range(0f, 1f)] float _minResolutionScale; | |
| float _lastStepTime; | |
| float _minCooldown; | |
| float _normalizedMinScale; | |
| float _maxResolutionScale; | |
| Vector2Int _recommendedResolution; | |
| const int _pixelStepDown = 256; | |
| const int _pixelStepUp = 128; | |
| const float _stepDownCooldown = 0.25f; | |
| const float _stepUpCooldown = 1f; | |
| void Awake() | |
| { | |
| _maxResolutionScale = (GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset).renderScale; | |
| _normalizedMinScale = _minResolutionScale / _maxResolutionScale; | |
| _minCooldown = Mathf.Min(_stepDownCooldown, _stepUpCooldown); | |
| } | |
| void LateUpdate() | |
| { | |
| float timeSinceLastStep = Time.time - _lastStepTime; | |
| if (timeSinceLastStep < _minCooldown || !TryGetRecommendedResolution(out _recommendedResolution)) { | |
| return; | |
| } | |
| float currentScale = XRSettings.renderViewportScale; | |
| int recommendedWidth = Mathf.Clamp( | |
| _recommendedResolution.x, | |
| (int) (XRSettings.eyeTextureWidth * _normalizedMinScale) - _pixelStepDown, | |
| (int) (XRSettings.eyeTextureWidth * currentScale) + _pixelStepUp | |
| ); | |
| float newScale = Mathf.Clamp(recommendedWidth / (float) XRSettings.eyeTextureWidth, _normalizedMinScale, 1f); | |
| if (Mathf.Approximately(currentScale, newScale)) { | |
| return; | |
| } | |
| if ((newScale > currentScale && timeSinceLastStep > _stepUpCooldown) || (newScale < currentScale && timeSinceLastStep > _stepDownCooldown)) { | |
| XRSettings.renderViewportScale = newScale; | |
| _lastStepTime = Time.time; | |
| } | |
| } | |
| bool TryGetRecommendedResolution(out Vector2Int resolution) | |
| { | |
| if (OVRPlugin.GetEyeLayerRecommendedResolution(out var scale)) { | |
| resolution = new Vector2Int(scale.w, scale.h); | |
| return true; | |
| } | |
| resolution = new Vector2Int(XRSettings.eyeTextureWidth, XRSettings.eyeTextureHeight); | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! Works for me too (vulkan, urp 12, unity 2022.3)