//
// 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 AssetIcons.Editors.Preferences;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace AssetIcons.Editors
{
///
/// Cache renders of the models that we would like to render.
///
///
///
public sealed class AssetIconsRenderCache : UnityEditor.AssetModificationProcessor
{
private static Dictionary> Renders { get; set; }
static AssetIconsRenderCache()
{
Renders = new Dictionary>();
}
///
/// Retrieves a from the cache for a using a .
///
/// A camera setup to use when rendering the graphic.
/// A Prefab to render a graphic for.
///
/// A of the renedred with the .
///
public static Texture2D GetTexture(AssetIconsCameraSetup cameraSetup, GameObject target)
{
string path = AssetDatabase.GetAssetPath(target);
Dictionary styleCache;
bool result = Renders.TryGetValue(cameraSetup, out styleCache);
if (!result)
{
styleCache = new Dictionary();
Renders.Add(cameraSetup, styleCache);
}
Texture2D texture;
result = styleCache.TryGetValue(path, out texture);
if (!result || texture == null || texture.Equals(null))
{
int size = AssetIconsPreferences.PrefabResolution.Value;
texture = AssetIconsRenderer.RenderModel(target, cameraSetup, size, size);
styleCache[path] = texture;
}
return texture;
}
///
/// Clears all rendered textures from this cache.
///
public static void ClearCache()
{
Renders.Clear();
}
///
/// Listens for an asset being saved, in this case we will mostly be listening for a Prefab
/// to be saved, however this can also listen for anything that imports a Prefab to be monitored.
///
/// The paths of the assets that will be saved.
private static string[] OnWillSaveAssets(string[] paths)
{
foreach (var cache in Renders)
{
foreach (string path in paths)
{
cache.Value.Remove(path);
}
}
return paths;
}
}
}
#pragma warning restore
#endif