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); var context = new VRMImporterContext(parser); context.Load(); context.ShowMeshes(); context.EnableUpdateWhenOffscreen(); 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)); var context = new VRMImporterContext(parser); var editor = new VRMEditorImporterContext(context); editor.ExtractImages(prefabPath); EditorApplication.delayCall += () => { // // after textures imported // context.Load(); editor.SaveAsAsset(prefabPath); editor.Dispose(); }; } }
static void ImportVrm(UnityPath path) { if (!path.IsUnderAssetsFolder) { throw new Exception(); } var parser = new GltfParser(); try { parser.ParseGlb(File.ReadAllBytes(path.FullPath)); } catch (KeyNotFoundException) { // invalid VRM-0.X. // maybe VRM-1.0.do nothing return; } var prefabPath = path.Parent.Child(path.FileNameWithoutExtension + ".prefab"); // save texture assets ! LoadTextureAsyncFunc textureLoader = async(caller, textureIndex, used) => { var gltfTexture = parser.GLTF.textures[textureIndex]; var gltfImage = parser.GLTF.images[gltfTexture.source]; var assetPath = prefabPath.Parent.Child(gltfImage.uri); var texture = await UniGLTF.AssetTextureLoader.LoadTaskAsync(assetPath, parser.GLTF, textureIndex); return(new TextureLoadInfo(texture, used, false)); }; var context = new VRMImporterContext(parser, textureLoader); var editor = new VRMEditorImporterContext(context); editor.ExtractImages(prefabPath); EditorApplication.delayCall += () => { // // after textures imported // context.Load(); editor.SaveAsAsset(prefabPath); editor.Dispose(); }; }
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); } }