remove ScriptedImporterExtension

This commit is contained in:
ousttrue
2021-06-24 18:22:41 +09:00
parent d72e1f1551
commit 765c685837
6 changed files with 94 additions and 138 deletions

View File

@@ -1,7 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Experimental.AssetImporters;
@@ -35,16 +32,6 @@ namespace UniGLTF
DrawRemapGUI<AnimationClip>(importer.GetExternalObjectMap());
}
static string GetAndCreateFolder(string assetPath, string suffix)
{
var path = $"{Path.GetDirectoryName(assetPath)}/{Path.GetFileNameWithoutExtension(assetPath)}{suffix}";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
public static void Extract(ScriptedImporter importer, GltfParser parser)
{
if (string.IsNullOrEmpty(importer.assetPath))
@@ -52,12 +39,12 @@ namespace UniGLTF
return;
}
var path = GetAndCreateFolder(importer.assetPath, ".Animations");
foreach (var asset in AssetDatabase.LoadAllAssetsAtPath(importer.assetPath))
{
var path = GetAndCreateFolder(importer.assetPath, ".Animations");
foreach (var (key, asset) in importer.GetSubAssets<AnimationClip>(importer.assetPath))
if (asset is AnimationClip)
{
asset.ExtractSubAsset($"{path}/{asset.name}.asset", false);
ExtractSubAsset(asset, $"{path}/{asset.name}.asset", false);
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Experimental.AssetImporters;
@@ -99,5 +100,64 @@ namespace UniGLTF
}
EditorGUI.indentLevel--;
}
protected static string GetAndCreateFolder(string assetPath, string suffix)
{
var path = $"{Path.GetDirectoryName(assetPath)}/{Path.GetFileNameWithoutExtension(assetPath)}{suffix}";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
/// <summary>
/// subAsset を 指定された path に extract する
/// </summary>
/// <param name="subAsset"></param>
/// <param name="destinationPath"></param>
/// <param name="isForceUpdate"></param>
public static UnityEngine.Object ExtractSubAsset(UnityEngine.Object subAsset, string destinationPath, bool isForceUpdate)
{
string assetPath = AssetDatabase.GetAssetPath(subAsset);
// clone を path に出力(subAsset を出力するため)
var clone = UnityEngine.Object.Instantiate(subAsset);
AssetDatabase.CreateAsset(clone, destinationPath);
// subAsset を clone に対して remap する
var assetImporter = AssetImporter.GetAtPath(assetPath);
assetImporter.AddRemap(new AssetImporter.SourceAssetIdentifier(clone), clone);
if (isForceUpdate)
{
AssetDatabase.WriteImportSettingsIfDirty(assetPath);
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
}
return clone;
}
// public static void ClearExternalObjects(this ScriptedImporter importer, params Type[] targetTypes)
// {
// foreach (var targetType in targetTypes)
// {
// if (!typeof(UnityEngine.Object).IsAssignableFrom(targetType))
// {
// throw new NotImplementedException();
// }
// foreach (var (key, obj) in importer.GetExternalObjectMap())
// {
// if (targetType.IsAssignableFrom(key.type))
// {
// importer.RemoveRemap(key);
// }
// }
// }
// AssetDatabase.WriteImportSettingsIfDirty(importer.assetPath);
// AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);
// }
}
}

View File

@@ -72,17 +72,23 @@ namespace UniGLTF
AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate);
ExtractMaterials(self, materialDir);
// material extract 後に importer 発動
AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate);
};
// subAsset を ExternalObject として投入する
var subAssets = AssetDatabase.LoadAllAssetsAtPath(self.assetPath)
.Select(x => x as Texture)
.Where(x => x != null)
.Select(x => (new SubAssetKey(x), x))
.ToDictionary(kv => kv.Item1, kv => kv.Item2)
;
var assetPath = UnityPath.FromFullpath(parser.TargetPath);
var dirName = textureDir(assetPath.Value); // $"{assetPath.FileNameWithoutExtension}.Textures";
TextureExtractor.ExtractTextures(
parser,
assetPath.Parent.Child(dirName),
textureDescriptorGenerator,
self.GetSubAssets<Texture>(self.assetPath).ToDictionary(kv => kv.Item1, kv => kv.Item2),
subAssets,
addRemap,
onCompleted
);
@@ -94,16 +100,22 @@ namespace UniGLTF
{
return;
}
var path = $"{Path.GetDirectoryName(importer.assetPath)}/{materialDir(importer.assetPath)}"; // Path.GetFileNameWithoutExtension(importer.assetPath)}.Materials
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
foreach (var (key, asset) in importer.GetSubAssets<Material>(importer.assetPath))
foreach (var asset in AssetDatabase.LoadAllAssetsAtPath(importer.assetPath))
{
asset.ExtractSubAsset($"{path}/{key.Name}.mat", false);
if (asset is Material)
{
ExtractSubAsset(asset, $"{path}/{asset.name}.mat", false);
}
}
AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);
}
}
}

View File

@@ -1,76 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using VRMShaders;
#if UNITY_2020_2_OR_NEWER
using UnityEditor.AssetImporters;
#else
using UnityEditor.Experimental.AssetImporters;
#endif
namespace UniGLTF
{
public static class ScriptedImporterExtension
{
// public static void ClearExternalObjects(this ScriptedImporter importer, params Type[] targetTypes)
// {
// foreach (var targetType in targetTypes)
// {
// if (!typeof(UnityEngine.Object).IsAssignableFrom(targetType))
// {
// throw new NotImplementedException();
// }
// foreach (var (key, obj) in importer.GetExternalObjectMap())
// {
// if (targetType.IsAssignableFrom(key.type))
// {
// importer.RemoveRemap(key);
// }
// }
// }
// AssetDatabase.WriteImportSettingsIfDirty(importer.assetPath);
// AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);
// }
public static IEnumerable<(SubAssetKey, T)> GetSubAssets<T>(this ScriptedImporter importer, string assetPath) where T : UnityEngine.Object
{
return AssetDatabase
.LoadAllAssetsAtPath(assetPath)
.Where(x => AssetDatabase.IsSubAsset(x))
.Where(x => x is T)
.Select(x => (new SubAssetKey(typeof(T), x.name), x as T));
}
/// <summary>
/// subAsset を 指定された path に extract する
/// </summary>
/// <param name="subAsset"></param>
/// <param name="destinationPath"></param>
/// <param name="isForceUpdate"></param>
public static UnityEngine.Object ExtractSubAsset(this UnityEngine.Object subAsset, string destinationPath, bool isForceUpdate)
{
string assetPath = AssetDatabase.GetAssetPath(subAsset);
// clone を path に出力(subAsset を出力するため)
var clone = UnityEngine.Object.Instantiate(subAsset);
AssetDatabase.CreateAsset(clone, destinationPath);
// subAsset を clone に対して remap する
var assetImporter = AssetImporter.GetAtPath(assetPath);
assetImporter.AddRemap(new AssetImporter.SourceAssetIdentifier(clone), clone);
if (isForceUpdate)
{
AssetDatabase.WriteImportSettingsIfDirty(assetPath);
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
}
return clone;
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 1c57d58453713684bb3888c33177ed78
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,7 +1,6 @@
using UniGLTF;
using UnityEngine;
using System.Linq;
using System.IO;
using UnityEditor;
using VRMShaders;
using System.Collections.Generic;
@@ -37,22 +36,6 @@ namespace UniVRM10
DrawRemapGUI<VRM10Expression>(importer.GetExternalObjectMap());
}
/// <summary>
/// $"{assetPath without extension}.{folderName}"
/// </summary>
/// <param name="assetPath"></param>
/// <param name="folderName"></param>
/// <returns></returns>
static string GetAndCreateFolder(string assetPath, string suffix)
{
var path = $"{Path.GetDirectoryName(assetPath)}/{Path.GetFileNameWithoutExtension(assetPath)}{suffix}";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
/// <summary>
///
/// * VRM10Object
@@ -69,23 +52,24 @@ namespace UniVRM10
}
var path = GetAndCreateFolder(importer.assetPath, ".vrm1.Assets");
// expression を extract し置き換え map を作る
var map = new Dictionary<VRM10Expression, VRM10Expression>();
foreach (var asset in AssetDatabase.LoadAllAssetsAtPath(importer.assetPath))
{
var map = new Dictionary<VRM10Expression, VRM10Expression>();
foreach (var (key, asset) in importer.GetSubAssets<VRM10Expression>(importer.assetPath))
if (asset is VRM10Expression expression)
{
var clone = asset.ExtractSubAsset($"{path}/{asset.name}.asset", false);
map.Add(asset, clone as VRM10Expression);
}
var (_, vrmObject) = importer.GetSubAssets<VRM10Object>(importer.assetPath).First();
vrmObject.Expression.Replace(map);
{
vrmObject.ExtractSubAsset($"{path}/{vrmObject.name}.asset", false);
var clone = ExtractSubAsset(asset, $"{path}/{asset.name}.asset", false);
map.Add(expression, clone as VRM10Expression);
}
}
// vrmObject の expression を置き換える
var vrmObject = AssetDatabase.LoadAllAssetsAtPath(importer.assetPath).First(x => x is VRM10Object) as VRM10Object;
vrmObject.Expression.Replace(map);
// extract
ExtractSubAsset(vrmObject, $"{path}/{vrmObject.name}.asset", false);
AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);
}
}