public static void ImportVrmAndCreatePrefab(string vrmPath, UnityPath prefabPath) { if (!prefabPath.IsUnderAssetsFolder) { Debug.LogWarningFormat("out of asset path: {0}", prefabPath); return; } /// <summary> /// これは EditorApplication.delayCall により呼び出される。 /// /// * delayCall には UnityEngine.Object 持ち越すことができない /// * vrmPath のみを持ち越す /// /// </summary> /// <value></value> Action <IEnumerable <UnityPath> > onCompleted = texturePaths => { var map = texturePaths .Select(x => x.LoadAsset <Texture>()) .ToDictionary(x => new SubAssetKey(x), x => x as UnityEngine.Object); // 確実に Dispose するために敢えて再パースしている using (var data = new GlbFileParser(vrmPath).Parse()) using (var context = new VRMImporterContext(new VRMData(data), externalObjectMap: map)) { var editor = new VRMEditorImporterContext(context, prefabPath); foreach (var textureInfo in context.TextureDescriptorGenerator.Get().GetEnumerable()) { VRMShaders.TextureImporterConfigurator.Configure(textureInfo, context.TextureFactory.ExternalTextures); } var loaded = context.Load(); editor.SaveAsAsset(loaded); } }; using (var data = new GlbFileParser(vrmPath).Parse()) using (var context = new VRMImporterContext(new VRMData(data))) { var editor = new VRMEditorImporterContext(context, prefabPath); // extract texture images editor.ConvertAndExtractImages(onCompleted); } }
static void ImportAsset(string path, UnityPath prefabPath) { if (!prefabPath.IsUnderAssetsFolder) { Debug.LogWarningFormat("out of asset path: {0}", prefabPath); return; } // import as asset var data = new GlbFileParser(path).Parse(); var vrm = new VRMData(data); Action <IEnumerable <UnityPath> > onCompleted = texturePaths => { // // after textures imported // var map = texturePaths .Select(x => x.LoadAsset <Texture2D>()) .Where(x => x != null) .ToDictionary(x => new SubAssetKey(x), x => x as Object); using (var context = new VRMImporterContext(vrm, externalObjectMap: map)) { var editor = new VRMEditorImporterContext(context, prefabPath); foreach (var textureInfo in editor.TextureDescriptorGenerator.Get().GetEnumerable()) { VRMShaders.TextureImporterConfigurator.Configure(textureInfo, context.TextureFactory.ExternalTextures); } var loaded = context.Load(); editor.SaveAsAsset(loaded); } }; using (var context = new VRMImporterContext(vrm)) { var editor = new VRMEditorImporterContext(context, prefabPath); editor.ConvertAndExtractImages(onCompleted); } }
static void ImportVrm(UnityPath vrmPath) { if (!vrmPath.IsUnderAssetsFolder) { throw new Exception(); } var data = new GlbFileParser(vrmPath.FullPath).Parse(); var vrm = new VRMData(data); var prefabPath = vrmPath.Parent.Child(vrmPath.FileNameWithoutExtension + ".prefab"); Action <IEnumerable <UnityPath> > onCompleted = texturePaths => { var map = texturePaths .Select(x => x.LoadAsset <Texture>()) .ToDictionary(x => new SubAssetKey(x), x => x as UnityEngine.Object); using (var context = new VRMImporterContext(vrm, externalObjectMap: map)) { var editor = new VRMEditorImporterContext(context, prefabPath); foreach (var textureInfo in context.TextureDescriptorGenerator.Get().GetEnumerable()) { VRMShaders.TextureImporterConfigurator.Configure(textureInfo, context.TextureFactory.ExternalTextures); } var loaded = context.Load(); editor.SaveAsAsset(loaded); } }; // extract texture images using (var context = new VRMImporterContext(vrm)) { var editor = new VRMEditorImporterContext(context, prefabPath); editor.ConvertAndExtractImages(onCompleted); } }
static void ImportMenu() { var path = EditorUtility.OpenFilePanel("open vrm", "", "vrm"); if (string.IsNullOrEmpty(path)) { return; } if (Application.isPlaying) { // load into scene var parser = new GltfParser(); parser.ParsePath(path); using (var context = new VRMImporterContext(parser)) { context.Load(); context.EnableUpdateWhenOffscreen(); context.ShowMeshes(); context.DisposeOnGameObjectDestroyed(); Selection.activeGameObject = context.Root; } } else { if (path.StartsWithUnityAssetPath()) { Debug.LogWarningFormat("disallow import from folder under the Assets"); return; } var assetPath = EditorUtility.SaveFilePanel("save prefab", "Assets", Path.GetFileNameWithoutExtension(path), "prefab"); if (string.IsNullOrEmpty(path)) { return; } if (!assetPath.StartsWithUnityAssetPath()) { Debug.LogWarningFormat("out of asset path: {0}", assetPath); return; } // import as asset var prefabPath = UnityPath.FromUnityPath(assetPath); var parser = new GltfParser(); parser.ParseGlb(File.ReadAllBytes(path)); Action <IEnumerable <string> > onCompleted = _ => { // // after textures imported // using (var context = new VRMImporterContext(parser)) { var editor = new VRMEditorImporterContext(context, prefabPath); context.Load(); editor.SaveAsAsset(); } }; using (var context = new VRMImporterContext(parser)) { var editor = new VRMEditorImporterContext(context, prefabPath); editor.ConvertAndExtractImages(UnityPath.FromFullpath(path), onCompleted); } } }