DragonECS-Unity/src/Internal/MonoScriptsAssetProcessor.cs

81 lines
2.6 KiB
C#
Raw Normal View History

2024-09-27 22:04:00 +08:00
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEditor;
namespace DCFApixels.DragonECS.Unity.Editors
{
internal class MonoScriptsAssetProcessor : AssetPostprocessor
{
private static long _version;
public static long Version { get { return _version; } }
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload)
{
_removedScriptGuids.Clear();
_newScriptIDs.Clear();
foreach (string str in importedAssets)
{
2024-10-01 18:26:20 +08:00
//Debug.Log("Reimported Asset: " + str);
2024-09-27 22:04:00 +08:00
ProcessAssetPath(str);
}
foreach (string str in deletedAssets)
{
2024-10-01 18:26:20 +08:00
//Debug.Log("Deleted Asset: " + str);
2024-09-27 22:04:00 +08:00
RemoveAssetPath(str);
}
for (int i = 0; i < movedAssets.Length; i++)
{
2024-10-01 18:26:20 +08:00
//Debug.Log("Moved Asset: " + movedAssets[i] + " from: " + movedFromAssetPaths[i]);
2024-09-27 22:04:00 +08:00
RemoveAssetPath(movedFromAssetPaths[i]);
ProcessAssetPath(movedAssets[i]);
}
2024-10-01 18:26:20 +08:00
//if (didDomainReload)
//{
// Debug.Log("Domain has been reloaded");
//}
2024-09-27 22:04:00 +08:00
_version = DateTime.Now.Ticks;
}
private static List<string> _removedScriptGuids = new List<string>();
private static List<string> _newScriptIDs = new List<string>();
public static IReadOnlyCollection<string> RemovedScriptPaths
{
get { return _removedScriptGuids; }
}
public static IReadOnlyCollection<string> NewScriptPaths
{
get { return _newScriptIDs; }
}
private static void RemoveAssetPath(string filePath)
{
if (IsScript(filePath) == false) { return; }
2024-10-01 18:26:20 +08:00
//Debug.Log("RemoveAssetPath: " + filePath);
2024-09-27 22:04:00 +08:00
_removedScriptGuids.Add(filePath);
}
private static void ProcessAssetPath(string filePath)
{
if (IsScript(filePath) == false) { return; }
2024-10-01 18:26:20 +08:00
//Debug.Log("ProcessAssetPath: " + filePath);
2024-09-27 22:04:00 +08:00
var script = AssetDatabase.LoadAssetAtPath<MonoScript>(filePath).text;
_newScriptIDs.Add(filePath);
}
private static bool IsScript(string filePath)
{
int i = filePath.Length - 3;
return filePath[i++] == '.'
&& filePath[i++] == 'c'
&& filePath[i++] == 's';
}
}
}
#endif