Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This class in conjunction with the Adaptive Quality class implements a basic adaptive quality mechanism in Unity.
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// This class in conjunction with the Adaptive Quality class implements a basic adaptive quality mechanism in Unity.
/// Must be attached to the active camera in your scene.
/// </summary>
///
public class AdaptiveViewport : MonoBehaviour {
[SerializeField]
private int fullSizeQualityLevel = 5;
[SerializeField]
private int minSizeQualityLevel = 0;
[SerializeField]
private float minViewportSize = 0.5f;
[SerializeField]
private AdaptiveQuality qualityController;
public float currentScale { get; private set; }
private void OnEnable()
{
currentScale = 1.0f;
if (qualityController)
{
qualityController.QualityChanged += QualityChangedEvent;
SetScaleFromQuality(qualityController.qualityLevel);
}
}
private void onDisable()
{
qualityController.QualityChanged -= QualityChangedEvent;
UnityEngine.XR.XRSettings.renderViewportScale = 1.0f;
}
private void OnPreCull()
{
UnityEngine.XR.XRSettings.renderViewportScale = currentScale;
QualitySettings.SetQualityLevel(qualityController.qualityLevel, false);
}
private void QualityChangedEvent(int newQuality, int previousQuality)
{
SetScaleFromQuality(newQuality);
}
private void SetScaleFromQuality(int quality)
{
int clampedQuality = Mathf.Clamp(quality, minSizeQualityLevel, fullSizeQualityLevel);
float lerpVal = Mathf.InverseLerp(minSizeQualityLevel, fullSizeQualityLevel, clampedQuality);
currentScale = Mathf.Lerp(minViewportSize, 1.0f, lerpVal);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.