Created
May 31, 2018 06:57
-
-
Save SachinGanesh/07d98382a02db63da32eec43438eb2be to your computer and use it in GitHub Desktop.
Unity3D Dynamic Quality Settings based on FPS. Runtime reduction of quality based on device performance
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; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class QualityManager : Singleton<QualityManager> { | |
private int QUALITY_INDEX = 0; | |
public int MIN_FPS = 28; | |
public Text qualityUIText; | |
private int waitTime = 3; | |
void Start(){ | |
//Application.targetFrameRate = 60; | |
QUALITY_INDEX = PlayerPrefs.GetInt("QualityIndex",-1); | |
if(QUALITY_INDEX == -1){ | |
QUALITY_INDEX = QualitySettings.GetQualityLevel(); | |
Debug.Log("Current Quality Index:" + QUALITY_INDEX); | |
Invoke("CheckFrameRate",waitTime); | |
qualityUIText.text = QUALITY_INDEX.ToString(); | |
} | |
else{ | |
UpdateSettings(); | |
qualityUIText.text = QUALITY_INDEX.ToString(); | |
} | |
} | |
void CheckFrameRate(){ | |
if(FPSCounter.Instance.FPS < MIN_FPS){ | |
//Drop Qiality Indez | |
QUALITY_INDEX--; | |
if(QUALITY_INDEX >=0){ | |
UpdateSettings(); | |
Invoke("CheckFrameRate",waitTime); | |
Debug.Log("Quality Index Decreased to :" + QUALITY_INDEX); | |
qualityUIText.text = QUALITY_INDEX.ToString(); | |
} | |
else{ | |
PlayerPrefs.SetInt("QualityIndex",QUALITY_INDEX); | |
AnalyticsManager.Instance.DeviceQuality(QUALITY_INDEX); | |
} | |
} | |
else { | |
//Optimal Settings | |
PlayerPrefs.SetInt("QualityIndex",QUALITY_INDEX); | |
Debug.Log("Saved Quality Index:" + QUALITY_INDEX); | |
AnalyticsManager.Instance.DeviceQuality(QUALITY_INDEX); | |
} | |
} | |
void UpdateSettings(){ | |
QualitySettings.SetQualityLevel(QUALITY_INDEX); | |
switch(QUALITY_INDEX){ | |
case 5: | |
break; | |
case 4: | |
QualitySettings.antiAliasing = 6; | |
break; | |
case 3: | |
QualitySettings.shadowResolution = ShadowResolution.High; | |
QualitySettings.antiAliasing = 4; | |
break; | |
case 2: | |
QualitySettings.shadowResolution = ShadowResolution.Medium; | |
QualitySettings.antiAliasing = 4; | |
HideShadows(); | |
break; | |
case 1: | |
QualitySettings.shadowResolution = ShadowResolution.Low; | |
QualitySettings.antiAliasing = 2; | |
HideShadows(); | |
break; | |
case 0: | |
QualitySettings.shadowResolution = ShadowResolution.Low; | |
QualitySettings.antiAliasing = 0; | |
HideShadows(); | |
break; | |
} | |
} | |
public void ReduceQuality(){ | |
QUALITY_INDEX--; | |
qualityUIText.text = QUALITY_INDEX.ToString(); | |
UpdateSettings(); | |
} | |
private void HideShadows(){ | |
var lights = FindObjectsOfType<Light>() as Light[]; | |
foreach(Light l in lights){ | |
l.shadows = LightShadows.None; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uses:
Singleton
FPSCounter