using UniGLTF; using UnityEngine; using System.Linq; using System.IO; using UnityEditor; using VRMShaders; using System.Collections.Generic; #if UNITY_2020_2_OR_NEWER using UnityEditor.AssetImporters; #else using UnityEditor.Experimental.AssetImporters; #endif namespace UniVRM10 { public static class EditorVrm { public static void OnGUI(ScriptedImporter importer, GltfParser parser, UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm) { var hasExternal = importer.GetExternalObjectMap().Any(x => x.Value is VRM10Object || x.Value is VRM10Expression); using (new EditorGUI.DisabledScope(hasExternal)) { if (GUILayout.Button("Extract Meta And Expressions ...")) { Extract(importer, parser); } } // meta importer.DrawRemapGUI(new SubAssetKey[] { VRM10Object.SubAssetKey }); // expressions importer.DrawRemapGUI(vrm.Expressions.Select(x => ExpressionKey.CreateFromVrm10(x).SubAssetKey)); if (GUILayout.Button("Clear")) { importer.ClearExternalObjects( typeof(VRM10Object), typeof(VRM10Expression)); } } /// /// $"{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 /// * VRM10Expression[] /// /// が Extract 対象となる /// /// public static void Extract(ScriptedImporter importer, GltfParser parser) { if (string.IsNullOrEmpty(importer.assetPath)) { return; } var path = GetAndCreateFolder(importer.assetPath, ".vrm1.Assets"); { var map = new Dictionary(); foreach (var (key, asset) in importer.GetSubAssets(importer.assetPath)) { 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); } } AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate); } } }