// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using UnityEditor;
using UnityEngine;
namespace XRTool.Util
{
///
/// Convenience class for setting Editor Preferences with Application.productName as key prefix.
///
public static class EditorPreferences
{
///
/// Set the saved from to EditorPrefs.
///
public static void Set(string key, string value)
{
Debug.Assert(!string.IsNullOrWhiteSpace(key));
EditorPrefs.SetString($"{Application.productName}_{key}", value);
}
///
/// Set the saved from to EditorPrefs.
///
public static void Set(string key, bool value)
{
Debug.Assert(!string.IsNullOrWhiteSpace(key));
EditorPrefs.SetBool($"{Application.productName}_{key}", value);
}
///
/// Set the saved from the EditorPrefs.
///
public static void Set(string key, float value)
{
Debug.Assert(!string.IsNullOrWhiteSpace(key));
EditorPrefs.SetFloat($"{Application.productName}_{key}", value);
}
///
/// Set the saved from theEditorPrefs.
///
public static void Set(string key, int value)
{
Debug.Assert(!string.IsNullOrWhiteSpace(key));
EditorPrefs.SetInt($"{Application.productName}_{key}", value);
}
///
/// Get the saved from theEditorPrefs.
///
public static string Get(string key, string defaultValue)
{
Debug.Assert(!string.IsNullOrWhiteSpace(key));
if (EditorPrefs.HasKey($"{Application.productName}_{key}"))
{
return EditorPrefs.GetString($"{Application.productName}_{key}");
}
EditorPrefs.SetString($"{Application.productName}_{key}", defaultValue);
return defaultValue;
}
///
/// Get the saved from the EditorPrefs.
///
public static bool Get(string key, bool defaultValue)
{
Debug.Assert(!string.IsNullOrWhiteSpace(key));
if (EditorPrefs.HasKey($"{Application.productName}_{key}"))
{
return EditorPrefs.GetBool($"{Application.productName}_{key}");
}
EditorPrefs.SetBool($"{Application.productName}_{key}", defaultValue);
return defaultValue;
}
///
/// Get the saved from the EditorPrefs.
///
public static float Get(string key, float defaultValue)
{
Debug.Assert(!string.IsNullOrWhiteSpace(key));
if (EditorPrefs.HasKey($"{Application.productName}_{key}"))
{
return EditorPrefs.GetFloat($"{Application.productName}_{key}");
}
EditorPrefs.SetFloat($"{Application.productName}_{key}", defaultValue);
return defaultValue;
}
///
/// Get the saved from the EditorPrefs.
///
public static int Get(string key, int defaultValue)
{
Debug.Assert(!string.IsNullOrWhiteSpace(key));
if (EditorPrefs.HasKey($"{Application.productName}_{key}"))
{
return EditorPrefs.GetInt($"{Application.productName}_{key}");
}
EditorPrefs.SetInt($"{Application.productName}_{key}", defaultValue);
return defaultValue;
}
}
}