Skip to content

Instantly share code, notes, and snippets.

@longmang
Created October 11, 2018 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save longmang/6b217711bf7ef16173368d6293629ea9 to your computer and use it in GitHub Desktop.
Save longmang/6b217711bf7ef16173368d6293629ea9 to your computer and use it in GitHub Desktop.
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