From 765c685837ca211618a889e177bfea63131a0f77 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 24 Jun 2021 18:22:41 +0900 Subject: [PATCH] remove ScriptedImporterExtension --- .../ScriptedImporter/RemapEditorAnimation.cs | 21 +---- .../ScriptedImporter/RemapEditorBase.cs | 60 +++++++++++++++ .../ScriptedImporter/RemapEditorMaterial.cs | 22 ++++-- .../ScriptedImporterExtension.cs | 76 ------------------- .../ScriptedImporterExtension.cs.meta | 11 --- .../Editor/ScriptedImporter/RemapEditorVrm.cs | 42 ++++------ 6 files changed, 94 insertions(+), 138 deletions(-) delete mode 100644 Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs delete mode 100644 Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs.meta diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorAnimation.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorAnimation.cs index da35e5afd..d6cc99015 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorAnimation.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorAnimation.cs @@ -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(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(importer.assetPath)) + if (asset is AnimationClip) { - asset.ExtractSubAsset($"{path}/{asset.name}.asset", false); + ExtractSubAsset(asset, $"{path}/{asset.name}.asset", false); } } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorBase.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorBase.cs index 48af4ba88..c248159ea 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorBase.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorBase.cs @@ -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; + } + + /// + /// subAsset を 指定された path に extract する + /// + /// + /// + /// + 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); + // } } } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs index 0e9e1c8c0..e14734554 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs @@ -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(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(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); } } } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs deleted file mode 100644 index d0a92a0b2..000000000 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs +++ /dev/null @@ -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(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)); - } - - /// - /// subAsset を 指定された path に extract する - /// - /// - /// - /// - 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; - } - } -} diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs.meta b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs.meta deleted file mode 100644 index fac355f38..000000000 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 1c57d58453713684bb3888c33177ed78 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/VRM10/Editor/ScriptedImporter/RemapEditorVrm.cs b/Assets/VRM10/Editor/ScriptedImporter/RemapEditorVrm.cs index 89c753b50..5d078f9cb 100644 --- a/Assets/VRM10/Editor/ScriptedImporter/RemapEditorVrm.cs +++ b/Assets/VRM10/Editor/ScriptedImporter/RemapEditorVrm.cs @@ -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(importer.GetExternalObjectMap()); } - /// - /// $"{assetPath without extension}.{folderName}" - /// - /// - /// - /// - 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; - } - /// /// /// * VRM10Object @@ -69,23 +52,24 @@ namespace UniVRM10 } var path = GetAndCreateFolder(importer.assetPath, ".vrm1.Assets"); + + // expression を extract し置き換え map を作る + var map = new Dictionary(); + foreach (var asset in AssetDatabase.LoadAllAssetsAtPath(importer.assetPath)) { - var map = new Dictionary(); - foreach (var (key, asset) in importer.GetSubAssets(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(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); } }