-
Notifications
You must be signed in to change notification settings - Fork 27
/
AssetProcessor.cs
83 lines (75 loc) · 2.84 KB
/
AssetProcessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using UnityEditor;
using System.Collections.Generic;
using System;
namespace Ogxd.ProjectCurator
{
/// <summary>
/// The purpose of this class is to try detecting asset changes to automatically update the ProjectCurator database.
/// </summary>
public class AssetProcessor : UnityEditor.AssetModificationProcessor
{
private static readonly Queue<Action> _actions = new Queue<Action>();
[InitializeOnLoadMethod]
public static void Init()
{
EditorApplication.update += OnUpdate;
}
/// <summary>
/// Some callbacks must be delayed on next frame
/// </summary>
private static void OnUpdate()
{
if (_actions.Count > 0) {
while (_actions.Count > 0) {
_actions.Dequeue()?.Invoke();
}
ProjectCurator.SaveDatabase();
}
}
static string[] OnWillSaveAssets(string[] paths)
{
if (ProjectCuratorData.IsUpToDate) {
_actions.Enqueue(() => {
foreach (string path in paths) {
var guid = AssetDatabase.AssetPathToGUID(path);
var removedAsset = ProjectCurator.RemoveAssetFromDatabase(guid);
ProjectCurator.AddAssetToDatabase(guid, removedAsset?.referencers);
}
});
}
return paths;
}
static void OnWillCreateAsset(string assetPath)
{
// Due to an apparent bug in Unity, newly created folders give the
// path of the meta file, instead of the folder itself. Trim the
// .meta off the end in the case that this happens.
//
// .meta files are not assets, so there are no other cases where
// this will have any effect.
assetPath = assetPath.TrimEnd(".meta");
if (ProjectCuratorData.IsUpToDate) {
_actions.Enqueue(() => {
var guid = AssetDatabase.AssetPathToGUID(assetPath);
if (guid != string.Empty) {
ProjectCurator.AddAssetToDatabase(guid);
}
});
}
}
static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions removeAssetOptions)
{
if (ProjectCuratorData.IsUpToDate) {
var guid = AssetDatabase.AssetPathToGUID(assetPath);
if (guid != string.Empty) {
ProjectCurator.RemoveAssetFromDatabase(guid);
}
}
return AssetDeleteResult.DidNotDelete;
}
static AssetMoveResult OnWillMoveAsset(string sourcePath, string destinationPath)
{
return AssetMoveResult.DidNotMove;
}
}
}