Skip to content

Instantly share code, notes, and snippets.

@AldeRoberge
Last active February 12, 2022 17:02
Show Gist options
  • Save AldeRoberge/67486e1774646617d386fc1365a96062 to your computer and use it in GitHub Desktop.
Save AldeRoberge/67486e1774646617d386fc1365a96062 to your computer and use it in GitHub Desktop.
Uses InGameDebugConsole's command system to dynamically change the quality of the game during runtime.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Humanizer;
using IngameDebugConsole;
using UnityEngine;
using UnityEngine.Rendering;
using VirtualRamen.Utils;
namespace VirtualRamen
{
/// <summary>
/// Adds a convenient way to change the quality level of the game during runtime.
/// </summary>
public class QualityCommand : MonoBehaviour
{
private Dictionary<int, string> QualityLevels;
public void Start()
{
// Load the quality levels dynamically from the QualitySettings
QualityLevels = new Dictionary<int, string>();
for (int i = 0; i < QualitySettings.names.Length; i++)
{
QualityLevels[i] = QualitySettings.names[i];
}
// Registers the InGameDebugConsole commands
DebugLogConsole.AddCommand<string>("quality", "Sets the quality to " + QualityLevels.Humanize("or"),
SetQuality);
DebugLogConsole.AddCommand("quality", "Returns the quality.", GetQuality);
}
/// <summary>
/// Prints the current quality level.
/// </summary>
public void GetQuality()
{
Debug.Log("Current quality: " + QualitySettings.GetQualityLevel());
// Prints the current render pipeline name
Debug.Log("Current render pipeline: " + QualitySettings.renderPipeline.name);
}
/// <summary>
/// Command to set the quality level to the given value.
/// </summary>
public void SetQuality(string quality)
{
foreach (KeyValuePair<int, string> qualityLevel in QualityLevels)
{
if (qualityLevel.Value.EqualsIgnoreCase(quality))
{
// Fun fact : Changing Quality level does not automatically update the render pipeline associated with it!
// So we need to change the render pipeline manually
QualitySettings.SetQualityLevel(qualityLevel.Key);
GetQuality();
return;
}
}
Debug.LogError("Could not find quality level " + quality);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment