Created
October 11, 2018 11:03
-
-
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.
This file contains 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.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