143 lines
3.7 KiB
C#
143 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace Module.NavigationTool.Editor.History
|
|
{
|
|
[Serializable]
|
|
internal sealed class HistoryList
|
|
{
|
|
public static readonly string PREF_ID = "PREF_HISTORY_LIST";
|
|
public const int MAX_LENGTH = 32;
|
|
|
|
public List<Entry> entries;
|
|
|
|
public HistoryList()
|
|
{
|
|
entries = new List<Entry>();
|
|
string json = EditorPrefs.GetString(PREF_ID);
|
|
|
|
if (!string.IsNullOrEmpty(json))
|
|
EditorJsonUtility.FromJsonOverwrite(json, this);
|
|
}
|
|
|
|
public void Add(string guid)
|
|
{
|
|
if (InternalAddEntry(new Entry(guid)))
|
|
Save();
|
|
}
|
|
|
|
private bool InternalAddEntry(Entry e)
|
|
{
|
|
e.Refresh();
|
|
|
|
if (!e.valid)
|
|
return false;
|
|
|
|
entries.Add(e);
|
|
|
|
if (entries.Count > MAX_LENGTH)
|
|
entries.RemoveRange(0, entries.Count - MAX_LENGTH);
|
|
|
|
return true;
|
|
}
|
|
|
|
public void AddRange(string[] guids)
|
|
{
|
|
var isDirty = false;
|
|
|
|
for (var i = 0; i < guids.Length; i++)
|
|
{
|
|
if (InternalAddEntry(new Entry(guids[i])))
|
|
isDirty = true;
|
|
}
|
|
|
|
if (isDirty)
|
|
Save();
|
|
}
|
|
|
|
public void AddRangeByPath(string[] paths)
|
|
{
|
|
var isDirty = false;
|
|
|
|
for (var i = 0; i < paths.Length; i++)
|
|
{
|
|
string guid = AssetDatabase.AssetPathToGUID(paths[i]);
|
|
|
|
if (InternalAddEntry(new Entry(guid)))
|
|
isDirty = true;
|
|
}
|
|
|
|
if (isDirty)
|
|
Save();
|
|
}
|
|
|
|
public void Remove(Entry e)
|
|
{
|
|
if (entries.Remove(e))
|
|
Save();
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
entries.Clear();
|
|
Save();
|
|
}
|
|
|
|
public Object GetObject(Entry entry)
|
|
{
|
|
string path = AssetDatabase.GUIDToAssetPath(entry.guid);
|
|
return AssetDatabase.LoadMainAssetAtPath(path);
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
string json = EditorJsonUtility.ToJson(this, false);
|
|
EditorPrefs.SetString(PREF_ID, json);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Class: Entry
|
|
/// </summary>
|
|
[Serializable]
|
|
public sealed class Entry : ISerializationCallbackReceiver
|
|
{
|
|
public string guid;
|
|
|
|
[NonSerialized]
|
|
public string name;
|
|
[NonSerialized]
|
|
public bool valid;
|
|
[NonSerialized]
|
|
public GUIContent content;
|
|
[NonSerialized]
|
|
public string lowerName;
|
|
|
|
public Entry(string guid)
|
|
{
|
|
this.guid = guid;
|
|
}
|
|
|
|
void ISerializationCallbackReceiver.OnBeforeSerialize()
|
|
{
|
|
}
|
|
|
|
void ISerializationCallbackReceiver.OnAfterDeserialize()
|
|
{
|
|
Refresh();
|
|
}
|
|
|
|
public void Refresh()
|
|
{
|
|
string path = AssetDatabase.GUIDToAssetPath(guid);
|
|
name = Path.GetFileNameWithoutExtension(path);
|
|
lowerName = name.ToLower();
|
|
valid = AssetDatabase.LoadMainAssetAtPath(path) != null;
|
|
content = new GUIContent(name, HistoryGUIUtility.GetIcon(path), path);
|
|
}
|
|
}
|
|
}
|
|
} |