Added GenerateMask to AssetsHelpers;
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Misaki.ArtToolEditor
|
||||
{
|
||||
internal class ExtractMaterialsProcessor : IAssetsProcessor
|
||||
{
|
||||
private const string Material_Extension = ".mat";
|
||||
|
||||
public bool useMaterialRemap = false;
|
||||
public ModelImporterMaterialLocation materialLocation = ModelImporterMaterialLocation.InPrefab;
|
||||
public ModelImporterMaterialName materialRemapNamingOption = ModelImporterMaterialName.BasedOnMaterialName;
|
||||
public ModelImporterMaterialSearch materialRemapSearchOption = ModelImporterMaterialSearch.RecursiveUp;
|
||||
|
||||
public void OnPreProcess(AssetsProcessContext context)
|
||||
{
|
||||
context.userData = new HashSet<string>();
|
||||
}
|
||||
|
||||
public void OnProcess(Object source, string outputDirectory, AssetsProcessContext context)
|
||||
{
|
||||
var assetsToReload = (HashSet<string>)context.userData;
|
||||
|
||||
var assetImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(source));
|
||||
if (assetImporter is not ModelImporter modelImporter)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (useMaterialRemap)
|
||||
{
|
||||
modelImporter.materialLocation = materialLocation;
|
||||
modelImporter.SearchAndRemapMaterials(materialRemapNamingOption, materialRemapSearchOption);
|
||||
modelImporter.SaveAndReimport();
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
var materials = AssetDatabase.LoadAllAssetsAtPath(modelImporter.assetPath).Where(x => x.GetType() == typeof(Material));
|
||||
|
||||
foreach (var material in materials)
|
||||
{
|
||||
var newAssetPath = Path.Combine(outputDirectory, material.name) + Material_Extension;
|
||||
newAssetPath = AssetDatabase.GenerateUniqueAssetPath(newAssetPath);
|
||||
|
||||
var error = AssetDatabase.ExtractAsset(material, newAssetPath);
|
||||
if (string.IsNullOrEmpty(error))
|
||||
{
|
||||
assetsToReload.Add(modelImporter.assetPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPostProcess(AssetsProcessContext context)
|
||||
{
|
||||
var assetsToReload = (HashSet<string>)context.userData;
|
||||
|
||||
foreach (var assetPath in assetsToReload)
|
||||
{
|
||||
if (string.IsNullOrEmpty(assetPath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AssetDatabase.WriteImportSettingsIfDirty(assetPath);
|
||||
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user