※簡単に追加できると思ったら非公開フィールドだったため、reflectionの練習がてら書いてみた
フォルダやアセットを選択して右クリックでメニューを実行するとLoadノードにファイル名やディレクトリ名が保存される(※AssetGraphのRefreshボタンを押す必要がある)
using System; | |
using System.Collections; | |
using System.IO; | |
using System.Reflection; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.AssetGraph; | |
using UnityEngine.AssetGraph.DataModel.Version2; | |
public class AssetGraphIgnorer | |
{ | |
private static string s_ignorePatternTypeName = "UnityEngine.AssetGraph.Loader+IgnorePattern"; | |
private static string s_fileTypeMaskTypeName = "UnityEngine.AssetGraph.Loader+IgnorePattern+FileTypeMask"; | |
private static string s_ignorePatternsFieldName = "m_ignorePatterns"; | |
private static string s_loadNodeCategoryName = "Load"; | |
[MenuItem("Assets/Ignore AssetPostprocessor")] | |
private static void SetAsIgnoreAsset() | |
{ | |
var guids = AssetDatabase.FindAssets("t:ConfigGraph", new[] { "Assets" }); | |
if (guids.Length != 1) | |
{ | |
return; | |
} | |
if (Selection.activeObject == null) | |
{ | |
Debug.LogWarning("Please select file or folder."); | |
return; | |
} | |
var graphAsset = AssetDatabase.LoadAssetAtPath<ConfigGraph>(AssetDatabase.GUIDToAssetPath(guids[0])); | |
foreach (var node in graphAsset.Nodes) | |
{ | |
if (node.Operation.Object.Category.Equals(s_loadNodeCategoryName)) | |
{ | |
Loader loader = node.Operation.Object as Loader; | |
FieldInfo[] fields = typeof(Loader).GetFields(BindingFlags.NonPublic | BindingFlags.Instance); | |
foreach (var field in fields) | |
{ | |
if (field.Name.Equals(s_ignorePatternsFieldName)) | |
{ | |
var listObj = (IList)field.GetValue(loader); | |
var path = AssetDatabase.GetAssetPath(Selection.activeObject); | |
var fileName = Path.GetFileName(path); | |
var ignorePattern = CreateIgnorePattern(fileName, IsFolderSelected()); | |
listObj.Add(ignorePattern); | |
field.SetValue(loader, listObj); | |
} | |
} | |
} | |
} | |
graphAsset.Save(); | |
} | |
private static object CreateIgnorePattern(string pattern, bool isFolder) | |
{ | |
var assembly = Assembly.GetAssembly(typeof(Loader)); | |
var ignorePatternType = assembly.GetType(s_ignorePatternTypeName); | |
var enumType = assembly.GetType(s_fileTypeMaskTypeName); | |
object enumInstance = Enum.ToObject(enumType, isFolder ? 2 : 1); | |
var ignorePattern = ignorePatternType.GetConstructor(new [] {enumType, typeof(string)})?.Invoke(new [] {enumInstance, pattern}); | |
return ignorePattern; | |
} | |
private static bool IsFolderSelected() | |
{ | |
var obj = Selection.activeObject; | |
string path = obj == null ? "Assets" : AssetDatabase.GetAssetPath(obj.GetInstanceID()); | |
if (path.Length > 0) | |
{ | |
if (Directory.Exists(path)) | |
{ | |
return true; | |
} | |
return false; | |
} | |
return false; | |
} | |
} |