首次提交
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ilumisoft.GraphicsControl
|
||||
{
|
||||
[CreateAssetMenu(menuName = "Gaphics Control/Configuration", fileName = "Configuration")]
|
||||
public class Configuration : ScriptableObject
|
||||
{
|
||||
public const string ConfigurationPath = "Ilumisoft/Graphics Control/Configuration";
|
||||
|
||||
[Header("Prefabs")]
|
||||
public GameObject GraphicSettingsManager;
|
||||
|
||||
[Tooltip("Automatically creates a persistent instance of the Graphic Settings Manager at startup when enabled")]
|
||||
public bool AutoCreate = true;
|
||||
|
||||
public GameObject GraphicSettingsPanel;
|
||||
|
||||
public static Configuration Find()
|
||||
{
|
||||
var result = Resources.Load<Configuration>(ConfigurationPath);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
||||
static void Initialize()
|
||||
{
|
||||
var configuration = Find();
|
||||
|
||||
// Automatically create the Graphic Settings Manager at startup
|
||||
if(configuration.AutoCreate && configuration.GraphicSettingsManager !=null)
|
||||
{
|
||||
var instance = Instantiate(configuration.GraphicSettingsManager);
|
||||
instance.name = configuration.GraphicSettingsManager.name;
|
||||
DontDestroyOnLoad(instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 295992f08e41b134b92787aaf7435315
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,20 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ilumisoft.GraphicsControl
|
||||
{
|
||||
[RequireComponent(typeof(GraphicSettingsManager))]
|
||||
public abstract class GraphicSetting : MonoBehaviour, IGraphicSetting
|
||||
{
|
||||
protected GraphicSettingsStorage GraphicSettingsStorage { get; set; }
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
GraphicSettingsStorage = GetComponent<GraphicSettingsStorage>();
|
||||
}
|
||||
|
||||
public abstract void Initialize();
|
||||
public abstract string GetSettingName();
|
||||
public abstract void LoadSetting();
|
||||
public abstract void SaveSetting();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7d941ca13646b6047959b3ecbb732da2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,28 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ilumisoft.GraphicsControl
|
||||
{
|
||||
/// <summary>
|
||||
/// Abstract base class for all <see cref="GraphicSettingsApplier"/>'s.
|
||||
/// </summary>
|
||||
public abstract class GraphicSettingsApplier : MonoBehaviour
|
||||
{
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
if (GraphicSettingsManager.Instance != null)
|
||||
{
|
||||
GraphicSettingsManager.Instance.Register(this);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnDisable()
|
||||
{
|
||||
if (GraphicSettingsManager.Instance != null)
|
||||
{
|
||||
GraphicSettingsManager.Instance.Unregister(this);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void ApplySettings();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 664d217953f234541af976eb1ea845c8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ilumisoft.GraphicsControl
|
||||
{
|
||||
public abstract class GraphicSettingsManager : SingletonBehaviour<GraphicSettingsManager>
|
||||
{
|
||||
protected List<GraphicSettingsApplier> GraphicSettingsAppliers { get; set; } = new();
|
||||
|
||||
public abstract List<GraphicSetting> GetGraphicSettings();
|
||||
|
||||
public abstract T Get<T>() where T : GraphicSetting;
|
||||
|
||||
public abstract bool TryGet<T>(out T graphicSetting) where T : GraphicSetting;
|
||||
|
||||
public abstract void LoadSettings();
|
||||
|
||||
public abstract void SaveSettings();
|
||||
|
||||
public abstract void ApplySettings();
|
||||
|
||||
public void Register(GraphicSettingsApplier graphicSettingsApplier)
|
||||
{
|
||||
if(graphicSettingsApplier != null && !GraphicSettingsAppliers.Contains(graphicSettingsApplier))
|
||||
{
|
||||
GraphicSettingsAppliers.Add(graphicSettingsApplier);
|
||||
}
|
||||
}
|
||||
|
||||
public void Unregister(GraphicSettingsApplier graphicSettingsApplier)
|
||||
{
|
||||
GraphicSettingsAppliers.Remove(graphicSettingsApplier);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 09959d570680f234e97182aa1882ecaa
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ilumisoft.GraphicsControl
|
||||
{
|
||||
public abstract class GraphicSettingsStorage : MonoBehaviour
|
||||
{
|
||||
public abstract bool GetBool(string key, bool defaultValue);
|
||||
public abstract float GetFloat(string key, float defaultValue);
|
||||
public abstract int GetInt(string key, int defaultValue);
|
||||
public abstract string GetString(string key, string defaultValue);
|
||||
public abstract void SetBool(string key, bool value);
|
||||
public abstract void SetFloat(string key, float value);
|
||||
public abstract void SetInt(string key, int value);
|
||||
public abstract void SetString(string key, string value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 786eafc0a79685247be430eff085729a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Ilumisoft.GraphicsControl
|
||||
{
|
||||
public interface IGraphicSetting
|
||||
{
|
||||
void Initialize();
|
||||
string GetSettingName();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 57b76debcf750ed4fa5e4ceb1258f03d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ilumisoft.GraphicsControl
|
||||
{
|
||||
public interface IMultiOptionGraphicSetting
|
||||
{
|
||||
List<string> GetOptionNames();
|
||||
int GetIndex();
|
||||
void SetIndex(int index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3b02f234e94a07438f1760e688082fb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,130 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ilumisoft.GraphicsControl
|
||||
{
|
||||
public abstract class MultiOptionGraphicSetting<T> : GraphicSetting, IMultiOptionGraphicSetting
|
||||
{
|
||||
/// <summary>
|
||||
/// Option table holding all available options for the graphic setting (e.g. all available resolutions for the resolution setting)
|
||||
/// </summary>
|
||||
protected OptionTable<T> OptionTable = new();
|
||||
|
||||
/// <summary>
|
||||
/// The index of the selected option (e.g. the selected resolution)
|
||||
/// </summary>
|
||||
int selectedIndex;
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new option to the option table
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="value"></param>
|
||||
protected void AddOption(string name, T value)
|
||||
{
|
||||
OptionTable.Add(name, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the names of all available options
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<string> GetOptionNames()
|
||||
{
|
||||
return OptionTable.GetNames();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the selected index
|
||||
/// </summary>
|
||||
/// <param name="index"></param>
|
||||
public void SetIndex(int index)
|
||||
{
|
||||
this.selectedIndex = index;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the index of the selected option
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int GetIndex()
|
||||
{
|
||||
return selectedIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Selects the given option. If the option is not available, the defaultIndex will be used to pick an option from the option table
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="defaultIndex"></param>
|
||||
protected void SelectOption(T value, int defaultIndex = 0)
|
||||
{
|
||||
if (TryGetIndex(value, out var index))
|
||||
{
|
||||
SetIndex(index);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetIndex(defaultIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Selects the option that fulfills the given predicate. If no option applies, the defaultIndex will be used to pick an option from the option table
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="defaultIndex"></param>
|
||||
protected void SelectOption(Predicate<T> predicate, int defaultIndex = 0)
|
||||
{
|
||||
if(TryGetIndex(predicate, out var index))
|
||||
{
|
||||
SetIndex(index);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetIndex(defaultIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the index of the given option
|
||||
/// </summary>
|
||||
/// <param name="option"></param>
|
||||
/// <param name="index"></param>
|
||||
/// <returns></returns>
|
||||
protected bool TryGetIndex(T option, out int index)
|
||||
{
|
||||
return TryGetIndex(entry => entry.Equals(option), out index);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the first index that fulfills the given predicate
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="index"></param>
|
||||
/// <returns></returns>
|
||||
protected bool TryGetIndex(Predicate<T> predicate, out int index)
|
||||
{
|
||||
index = -1;
|
||||
|
||||
var entries = OptionTable.GetValues();
|
||||
|
||||
for (int i = 0; i < entries.Count; i++)
|
||||
{
|
||||
if (predicate(entries[i]))
|
||||
{
|
||||
index = i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the currently selected option
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public T GetSelectedOption() => OptionTable.GetValue(selectedIndex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58469bbd4c3c3cb4fa41792392bf2ca4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Ilumisoft.GraphicsControl
|
||||
{
|
||||
public class OptionTable<T>
|
||||
{
|
||||
public struct OptionEntry
|
||||
{
|
||||
public string Name;
|
||||
public T Value;
|
||||
|
||||
public OptionEntry(string name, T value)
|
||||
{
|
||||
Name = name;
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
|
||||
List<OptionEntry> entries = new();
|
||||
|
||||
public void Add(string name, T value)
|
||||
{
|
||||
entries.Add(new OptionEntry(name, value));
|
||||
}
|
||||
|
||||
public List<string> GetNames() => entries.Select(x=>x.Name).ToList();
|
||||
public List<T> GetValues() => entries.Select(x => x.Value).ToList();
|
||||
|
||||
public T GetValue(int index) => entries[index].Value;
|
||||
|
||||
public string GetName(int index) => entries[index].Name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bad7acddd156c9a4f88746f9c6bd4eb9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,44 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ilumisoft.GraphicsControl
|
||||
{
|
||||
public abstract class SingletonBehaviour<T> : MonoBehaviour where T : SingletonBehaviour<T>
|
||||
{
|
||||
protected static T instance = null;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the active instance of the singleton
|
||||
/// </summary>
|
||||
public static T Instance => instance;
|
||||
|
||||
/// <summary>
|
||||
/// Creates the instance of the singleton
|
||||
/// </summary>
|
||||
protected virtual void Awake()
|
||||
{
|
||||
//Dont allow multiple instances of a singleton
|
||||
if (instance != null)
|
||||
{
|
||||
Debug.LogError(string.Format("Multiple instances of {0} are not allowed", typeof(T)));
|
||||
|
||||
Destroy(this);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
instance = (T)this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the static instance when the object gets destroyed.
|
||||
/// This is necessary because static fields are not resolved by the garbage collector.
|
||||
/// </summary>
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
if (instance == this)
|
||||
{
|
||||
instance = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a83b4774cf3525943838f3b397160952
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,47 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ilumisoft.GraphicsControl
|
||||
{
|
||||
/// <summary>
|
||||
/// Toggle Graphic Settings can either be on or off.
|
||||
/// This for example used for Bloom, Motion Blur or Grain Post Processing effects.
|
||||
/// </summary>
|
||||
public abstract class ToggleGraphicSetting : MultiOptionGraphicSetting<bool>
|
||||
{
|
||||
[Tooltip("Whether the option should be enabled by default or not")]
|
||||
public bool IsEnabledByDefault = true;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
// Toggle graphic settings can be eitehr on or off
|
||||
AddOption("Off", false);
|
||||
AddOption("On", true);
|
||||
|
||||
SelectOption(IsEnabledByDefault);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the setting is on, false otherwise
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool IsEnabled() => GetSelectedOption();
|
||||
|
||||
/// <summary>
|
||||
/// Loads the stored settings
|
||||
/// </summary>
|
||||
public override void LoadSetting()
|
||||
{
|
||||
bool value = GraphicSettingsStorage.GetBool(GetSettingName(), IsEnabled());
|
||||
|
||||
SelectOption(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the selected option
|
||||
/// </summary>
|
||||
public override void SaveSetting()
|
||||
{
|
||||
GraphicSettingsStorage.SetBool(GetSettingName(), IsEnabled());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a77a8124e4600dc46a0fa337984bfb25
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user