概要
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;
public class Example : AssetPostprocessor
{
[MenuItem( "Tools/Hoge" )]
private static void Hoge()
{
var count = 100;
var shader = Shader.Find( "Sprites/Diffuse" );
for ( int i = 0; i < count; i++ )
{
AssetDatabase.CreateAsset( new Material( shader ), $"Assets/Materials/{i}.mat" );
}
}
private static void OnPostprocessAllAssets( string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths )
{
Debug.Log( "OnPostprocessAllAssets: " + importedAssets.Length );
}
}
- 上記のように普通に AssetDatabase.CreateAsset を呼び出すと
AssetDatabase.CreateAsset が呼び出されるたびに OnPostprocessAllAssets が実行される
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;
public class Example : AssetPostprocessor
{
[MenuItem( "Tools/Hoge" )]
private static void Hoge()
{
var count = 100;
var shader = Shader.Find( "Sprites/Diffuse" );
try
{
AssetDatabase.StartAssetEditing();
for ( int i = 0; i < count; i++ )
{
AssetDatabase.CreateAsset( new Material( shader ), $"Assets/Materials/{i}.mat" );
}
}
finally
{
AssetDatabase.StopAssetEditing();
}
}
private static void OnPostprocessAllAssets( string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths )
{
Debug.Log( "OnPostprocessAllAssets: " + importedAssets.Length );
}
}
- 上記のように AssetDatabase.CreateAsset を呼び出している箇所を
AssetDatabase.StartAssetEditing と AssetDatabase.StopAssetEditing で囲むと
OnPostprocessAllAssets は AssetDatabase.StopAssetEditing の時に
1度だけ呼び出されるようになる
- OnPostprocessAllAssets で重たい処理が実装されている場合は
高速化できるかもしれない
参考サイト様