Skip to content

Commit

Permalink
feat: keep session settings (#130)
Browse files Browse the repository at this point in the history
Signed-off-by: Alptuğ Cırıt <alptug@leodrive.ai>
  • Loading branch information
mozhoku authored Aug 19, 2024
1 parent 6448886 commit aea30f0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 40 deletions.
92 changes: 53 additions & 39 deletions Assets/AWSIM/Scripts/UI/GraphicsSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ public class GraphicsSettings : MonoBehaviour
private List<GameObject> _cameraObjectsList;
private Light _sunSource;

private int _gpuMemorySize;
private int _initialQualityLevel;
private int _currentQualityLevel;
private bool _isInitialised;

/// <summary>
/// PlayerPref key for graphics quality settings:
/// Not set = -1 | Low = 0 | Medium = 1 | High = 2 | Ultra = 3
/// </summary>
private const string UserGraphicsQualityKey = "UserGraphicsQuality";

private readonly List<string> _dropdownOptions = new()
{
Expand All @@ -27,12 +31,16 @@ public class GraphicsSettings : MonoBehaviour

private void Awake()
{
_gpuMemorySize = SystemInfo.graphicsMemorySize;
// Initialize PlayerPref Keys
if (!PlayerPrefs.HasKey(UserGraphicsQualityKey))
{
PlayerPrefs.SetInt(UserGraphicsQualityKey, -1);
}
}

private void Start()
{
// add main camera to list (BEV cam is not initialized yet will be added later)
// add main camera to list (BEV cam is not initialized yet, will be when enabled)
_cameraObjectsList = new List<GameObject>
{
Camera.main?.gameObject,
Expand All @@ -43,7 +51,17 @@ private void Start()
BirdEyeView.OnCameraInitialized += UpdateGraphicSettingsForAddedCamera;

// Set initial quality level
InitialQualityLevel();
if (PlayerPrefs.HasKey(UserGraphicsQualityKey) &&
PlayerPrefs.GetInt(UserGraphicsQualityKey) != -1)
{
_initialQualityLevel = PlayerPrefs.GetInt(UserGraphicsQualityKey);
UISetQuality(_initialQualityLevel);
}
else
{
// Get GPU memory size
InitialQualityLevel(SystemInfo.graphicsMemorySize);
}

// Populate dropdown with quality settings
_dropdown = GetComponent<Dropdown>();
Expand All @@ -64,36 +82,31 @@ public void Activate()
Start();
}

private void InitialQualityLevel()
private void InitialQualityLevel(int gpuMemorySize)
{
if (!_isInitialised)
// Select Quality settings level (URP Asset) based on the size of the device's graphics memory
switch (gpuMemorySize)
{
// Select Quality settings level (URP Asset) based on the size of the device's graphics memory
switch (_gpuMemorySize)
{
case <= 2048:
_initialQualityLevel = 0;
GraphicsLowQuality();
break;
case <= 4096:
_initialQualityLevel = 1;
GraphicsMediumQuality();
break;
case <= 6144:
_initialQualityLevel = 2;
GraphicsHighQuality();
break;
case <= 8192:
_initialQualityLevel = 3;
GraphicsUltraQuality();
break;
default:
_initialQualityLevel = 3;
GraphicsUltraQuality();
break;
}

_isInitialised = true;
case <= 2048:
_initialQualityLevel = 0;
GraphicsLowQuality();
break;
case <= 4096:
_initialQualityLevel = 1;
GraphicsMediumQuality();
break;
case <= 6144:
_initialQualityLevel = 2;
GraphicsHighQuality();
break;
case <= 8192:
_initialQualityLevel = 3;
GraphicsUltraQuality();
break;
default:
_initialQualityLevel = 3;
GraphicsUltraQuality();
break;
}
}

Expand All @@ -118,8 +131,10 @@ public void UISetQuality(int value)

private void GraphicsLowQuality()
{
// Update quality level
_currentQualityLevel = 0;
QualitySettings.SetQualityLevel(0);
PlayerPrefs.SetInt(UserGraphicsQualityKey, 0);

// update camera parameters
foreach (var cam in _cameraObjectsList)
Expand Down Expand Up @@ -151,6 +166,7 @@ private void GraphicsMediumQuality()
{
_currentQualityLevel = 1;
QualitySettings.SetQualityLevel(1);
PlayerPrefs.SetInt(UserGraphicsQualityKey, 1);

// update camera and volume parameters
foreach (var cam in _cameraObjectsList)
Expand Down Expand Up @@ -184,6 +200,7 @@ private void GraphicsHighQuality()
{
_currentQualityLevel = 2;
QualitySettings.SetQualityLevel(2);
PlayerPrefs.SetInt(UserGraphicsQualityKey, 2);

// update camera parameters
foreach (var cam in _cameraObjectsList)
Expand Down Expand Up @@ -220,6 +237,7 @@ private void GraphicsUltraQuality()
{
_currentQualityLevel = 3;
QualitySettings.SetQualityLevel(3);
PlayerPrefs.SetInt(UserGraphicsQualityKey, 3);

// update camera parameters
foreach (var cam in _cameraObjectsList)
Expand Down Expand Up @@ -261,18 +279,14 @@ private void GraphicsUltraQuality()
// Used for adding new cameras and updating their settings
private void UpdateGraphicSettingsForAddedCamera(GameObject cam)
{
if (_isInitialised)
{
_cameraObjectsList.Add(cam);
UISetQuality(_currentQualityLevel);
}
_cameraObjectsList.Add(cam);
UISetQuality(_currentQualityLevel);
}

private void OnDestroy()
{
BirdEyeView.OnCameraInitialized -= UpdateGraphicSettingsForAddedCamera;
_cameraObjectsList.Clear();
_isInitialised = false;
}
}
}
33 changes: 32 additions & 1 deletion Assets/AWSIM/Scripts/UI/Toggle/UIMainCameraToggle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,41 @@ public class UIMainCameraToggle : MonoBehaviour
private UnityEngine.UI.Toggle _toggle;
private Image _mainCanvasImage;

/// <summary>
/// Key for storing user's main camera render preference:
/// Not set = -1 | Disabled = 0 | Enabled = 1
/// </summary>
private const string UserMainCameraRenderKey = "UserMainCameraRender";

private void Awake()
{
if (!PlayerPrefs.HasKey(UserMainCameraRenderKey))
{
PlayerPrefs.SetInt(UserMainCameraRenderKey, -1);
}
}

private void Start()
{
// Get components
_mainCam = Camera.main;
_mainCanvasImage = GameObject.FindGameObjectWithTag("MainCanvas").GetComponent<Image>();
_toggle = GetComponent<UnityEngine.UI.Toggle>();
if (_mainCam != null) _toggle.isOn = _mainCam.enabled;

// Set main camera render preference
if (_mainCam && PlayerPrefs.GetInt(UserMainCameraRenderKey) != -1)
{
if (PlayerPrefs.GetInt(UserMainCameraRenderKey) == 1)
{
_mainCam.enabled = true;
}
else if (PlayerPrefs.GetInt(UserMainCameraRenderKey) == 0)
{
_mainCam.enabled = false;
}
}

if (_mainCam) _toggle.isOn = _mainCam.enabled;
_toggle.onValueChanged.AddListener(ToggleMainCamera);
}

Expand All @@ -30,6 +58,9 @@ private void ToggleMainCamera(bool value)
{
_mainCam.enabled = value;
_mainCanvasImage.enabled = !value;

// Save user's preference
PlayerPrefs.SetInt(UserMainCameraRenderKey, value ? 1 : 0);
}
}
}

0 comments on commit aea30f0

Please sign in to comment.