/// <summary> /// /// * Texture(.png etc...)をディスクに書き出す /// * EditorApplication.delayCall で処理を進めて 書き出した画像が Asset として成立するのを待つ /// * 書き出した Asset から TextureImporter を取得して設定する /// /// </summary> /// <param name="importer"></param> /// <param name="dirName"></param> /// <param name="onCompleted"></param> public static void ExtractTextures(GltfData data, UnityPath textureDirectory, ITextureDescriptorGenerator textureDescriptorGenerator, IReadOnlyDictionary <SubAssetKey, Texture> subAssets, Action <SubAssetKey, Texture2D> addRemap, Action <IEnumerable <UnityPath> > onCompleted = null) { var extractor = new TextureExtractor(data, textureDirectory, subAssets); foreach (var param in textureDescriptorGenerator.Get().GetEnumerable()) { extractor.Extract(param.SubAssetKey, param); } EditorApplication.delayCall += () => { // Wait for the texture assets to be imported foreach (var(key, targetPath) in extractor.Textures) { // remap var externalObject = targetPath.LoadAsset <Texture2D>(); #if VRM_DEVELOP // Debug.Log($"remap: {targetPath} => {externalObject}"); #endif if (externalObject != null) { addRemap(key, externalObject); } } if (onCompleted != null) { onCompleted(extractor.Textures.Values); } }; }
void ExtractMaterialsAndTextures(ScriptedImporter self, GltfData data, ITextureDescriptorGenerator textureDescriptorGenerator, Func <string, string> textureDir, Func <string, string> materialDir) { if (string.IsNullOrEmpty(self.assetPath)) { return; } Action <SubAssetKey, Texture2D> addRemap = (key, externalObject) => { self.AddRemap(new AssetImporter.SourceAssetIdentifier(key.Type, key.Name), externalObject); }; Action <IEnumerable <UnityPath> > onCompleted = _ => { // texture extract 後に importer 発動 AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); ExtractMaterials(self, materialDir); }; // 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.FromUnityPath(self.assetPath); var dirName = textureDir(assetPath.Value); // $"{assetPath.FileNameWithoutExtension}.Textures"; TextureExtractor.ExtractTextures( data, assetPath.Parent.Child(dirName), textureDescriptorGenerator, subAssets, addRemap, onCompleted ); }
public void OnGUI(ScriptedImporter importer, GltfData data, ITextureDescriptorGenerator textureDescriptorGenerator, Func <string, string> textureDir, Func <string, string> materialDir) { if (CanExtract(importer)) { if (GUILayout.Button("Extract Materials And Textures ...")) { ExtractMaterialsAndTextures(importer, data, textureDescriptorGenerator, textureDir, materialDir); } EditorGUILayout.HelpBox("Extract subasset to external object and overwrite remap", MessageType.Info); } else { if (GUILayout.Button("Clear extraction")) { ClearExternalObjects(importer, typeof(Texture), typeof(Material)); } EditorGUILayout.HelpBox("Clear remap. All remap use subAsset", MessageType.Info); } // // Draw ExternalObjectMap // s_foldMaterials = EditorGUILayout.Foldout(s_foldMaterials, "Remapped Materials"); if (s_foldMaterials) { DrawRemapGUI <UnityEngine.Material>(importer.GetExternalObjectMap()); } s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures"); if (s_foldTextures) { DrawRemapGUI <UnityEngine.Texture>(importer.GetExternalObjectMap()); } }