Skip to content

Instantly share code, notes, and snippets.

@zawazawagh
Last active September 11, 2022 03:39
Show Gist options
  • Save zawazawagh/18f2d10d81e0f6fc750f7e8a10738bab to your computer and use it in GitHub Desktop.
Save zawazawagh/18f2d10d81e0f6fc750f7e8a10738bab to your computer and use it in GitHub Desktop.
【Unity AssetGraph】Add load ignoresettings with custom menu

【Unity】AssetGraphのLoadノードにおいてファイルやフォルダを無視する設定を追加できるカスタムメニュー

※簡単に追加できると思ったら非公開フィールドだったため、reflectionの練習がてら書いてみた

フォルダやアセットを選択して右クリックでメニューを実行するとLoadノードにファイル名やディレクトリ名が保存される(※AssetGraphのRefreshボタンを押す必要がある) image

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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment