//
// Copyright (c) 2017 Anthony Marmont. All rights reserved.
// Licensed for use under the Unity Asset Store EULA. See https://unity3d.com/legal/as_terms for full license information.
//
#if UNITY_EDITOR
#pragma warning disable
using UnityEditor;
using UnityEngine;
namespace AssetIcons.Editors.Preferences
{
///
/// Stores AssetIcons preferences information in .
///
///
/// You can interact with AssetIcons preferences via an editor scripting API.
///
/// using AssetIcons.Editors.Preferences;
/// using UnityEditor;
///
/// public class DemoWindow : EditorWindow
/// {
/// [MenuItem("AssetIcons/Demo Window")]
/// private static void Init()
/// {
/// var window = GetWindow(typeof(DemoWindow), false, "Demo Window");
/// window.Show();
/// }
///
/// private void OnGUI()
/// {
/// AssetIconsPreferences.Enabled.Value = EditorGUILayout.Toggle("AssetIcons Enabled", AssetIconsPreferences.Enabled.Value);
/// }
/// }
///
///
///
/// These preferences are effective on a single machine and in a single Unity project. Changing to another project with AssetIcons
/// installed will not preserve these saved preferences.
///
///
public static class AssetIconsPreferences
{
///
/// The key used to store the current users preferences.
///
public const string EditorPrefsKey = "AssetIcons_Preferences";
private static AssetIconsPreferencesPreset currentPreferences;
///
/// The current set of preferences used by AssetIcons.
///
///
/// You can interact with AssetIcons preferences via an editor scripting API.
///
/// using AssetIcons.Editors.Preferences;
/// using UnityEditor;
///
/// public class DemoWindow : EditorWindow
/// {
/// [MenuItem("AssetIcons/Demo Window")]
/// private static void Init()
/// {
/// var window = GetWindow(typeof(DemoWindow), false, "Demo Window");
/// window.Show();
/// }
///
/// private void OnGUI()
/// {
/// var preferences = AssetIconsPreferences.CurrentPreferences;
///
/// preferences.Enabled.Value = EditorGUILayout.Toggle("AssetIcons Enabled", preferences.Enabled.Value);
/// }
/// }
///
///
public static AssetIconsPreferencesPreset CurrentPreferences
{
get
{
if (currentPreferences == null)
{
string jsonData = EditorPrefs.GetString(EditorPrefsKey, "");
if (string.IsNullOrEmpty(jsonData))
{
currentPreferences = ScriptableObject.CreateInstance();
}
else
{
currentPreferences = ScriptableObject.CreateInstance();
JsonUtility.FromJsonOverwrite(jsonData, currentPreferences);
}
currentPreferences.Enabled.OnChanged += SaveChangesCallback;
currentPreferences.DrawGUIStyles.OnChanged += SaveChangesCallback;
currentPreferences.PrefabResolution.OnChanged += ClearCacheCallback;
currentPreferences.SelectionTint.OnChanged += SaveChangesCallback;
currentPreferences.TypeIcons.OnChanged += SaveChangesCallback;
}
return currentPreferences;
}
}
///
/// A shorthand for CurrentPreferences.DrawGUIStyles.
/// Enables previewing of Unity assets.
///
public static BoolEventField DrawGUIStyles
{
get
{
return CurrentPreferences.DrawGUIStyles;
}
}
///
/// A shorthand for CurrentPreferences.Enabled.
/// Controls whether AssetIcons should be enabled or disabled.
///
public static BoolEventField Enabled
{
get
{
return CurrentPreferences.Enabled;
}
}
///
/// A shorthand for CurrentPreferences.PrefabResolution.
/// Allows for adjusting the resolution AssetIcons will render Prefabs with.
///
///
/// If you are struggling with performance with a large amount of rendered assets, you could try
/// adjusting this to boost performance.
///
public static IntEventField PrefabResolution
{
get
{
return CurrentPreferences.PrefabResolution;
}
}
///
/// A shorthand for CurrentPreferences.SelectionTint.
/// Controls how strong of a tint is applied to AssetIcons rendered graphics when selected.
///
public static ColorTintEventField SelectionTint
{
get
{
return CurrentPreferences.SelectionTint;
}
}
///
/// A shorthand for CurrentPreferences.TypeIcons.
/// A collection of Graphics associated with file extensions that AssetIcons uses to render custom
/// file icons.
///
public static IconMapping TypeIcons
{
get
{
return CurrentPreferences.TypeIcons;
}
}
///
/// A shorthand for CurrentPreferences.HideReviewDialog.
/// Controls whether the review dialog should be shown.
///
public static BoolEventField HideReviewDialog
{
get
{
return CurrentPreferences.HideReviewDialog;
}
}
private static void ClearCacheCallback()
{
AssetIconsRenderCache.ClearCache();
SaveChangesCallback();
}
private static void SaveChangesCallback()
{
EditorApplication.delayCall -= SaveAndRepaint;
EditorApplication.delayCall += SaveAndRepaint;
}
private static void SaveAndRepaint()
{
EditorApplication.RepaintProjectWindow();
EditorPrefs.SetString(EditorPrefsKey, JsonUtility.ToJson(CurrentPreferences));
}
}
}
#pragma warning restore
#endif