static void ImportVrm(UnityPath path)
        {
            if (!path.IsUnderAssetsFolder)
            {
                throw new Exception();
            }
            var context = new VRMImporterContext();

            context.ParseGlb(File.ReadAllBytes(path.FullPath));

            var prefabPath = path.Parent.Child(path.FileNameWithoutExtension + ".prefab");

            // save texture assets !
            context.ExtranctImages(prefabPath);

            EditorApplication.delayCall += () =>
            {
                //
                // after textures imported
                //
                context.Load();
                context.SaveAsAsset(prefabPath);
                context.Destroy(false);
            };
        }
        static void ImportMenu()
        {
            var path = EditorUtility.OpenFilePanel("open vrm", "", "vrm");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (Application.isPlaying)
            {
                // load into scene
                var context = new VRMImporterContext();
                context.Load(path);
                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 context    = new VRMImporterContext();
                context.ParseGlb(File.ReadAllBytes(path));
                context.ExtranctImages(prefabPath);

                EditorApplication.delayCall += () =>
                {
                    //
                    // after textures imported
                    //
                    context.Load();
                    context.SaveAsAsset(prefabPath);
                    context.Destroy(false);
                };
            }
        }
Example #3
0
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            foreach (string path in importedAssets)
            {
                var ext = Path.GetExtension(path).ToLower();
                if (ext == ".vrm")
                {
                    var context = new VRMImporterContext(path);

                    context.ParseVrm(File.ReadAllBytes(context.Path));

                    //
                    // https://answers.unity.com/questions/647615/how-to-update-import-settings-for-newly-created-as.html
                    //
                    for (int i = 0; i < context.GLTF.textures.Count; ++i)
                    {
                        var x     = context.GLTF.textures[i];
                        var image = context.GLTF.images[x.source];
                        if (string.IsNullOrEmpty(image.uri))
                        {
                            // glb buffer
                            var folder = context.GetAssetFolder(".Textures").AssetPathToFullPath();
                            if (!Directory.Exists(folder))
                            {
                                UnityEditor.AssetDatabase.CreateFolder(context.GLTF.baseDir, Path.GetFileNameWithoutExtension(context.Path) + ".Textures");
                                //Directory.CreateDirectory(folder);
                            }

                            var textureName = !string.IsNullOrEmpty(image.name) ? image.name: string.Format("buffer#{0:00}", i);
                            var png         = Path.Combine(folder, textureName + ".png");
                            var byteSegment = context.GLTF.GetViewBytes(image.bufferView);
                            File.WriteAllBytes(png, byteSegment.ToArray());
                            var assetPath = png.ToUnityRelativePath();
                            //Debug.LogFormat("import asset {0}", assetPath);
                            UnityEditor.AssetDatabase.ImportAsset(assetPath, UnityEditor.ImportAssetOptions.ForceUpdate);
                            UnityEditor.AssetDatabase.Refresh();
                            image.uri = assetPath.Substring(context.GLTF.baseDir.Length + 1);
                        }
                    }

                    EditorApplication.delayCall += () =>
                    {
                        // delay and can import png texture
                        VRMImporter.LoadFromBytes(context);
                        context.SaveAsAsset();
                        context.Destroy(false);
                    };
                }
            }
        }
Example #4
0
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            foreach (string path in importedAssets)
            {
                var ext = Path.GetExtension(path).ToLower();
                if (ext == ".vrm")
                {
                    var context = new VRMImporterContext(path);
                    try
                    {
                        VRMImporter.LoadFromPath(context);

                        /*
                         * var prefabPath = String.Format("{0}/{1}.prefab",
                         *  Path.GetDirectoryName(path),
                         *  Path.GetFileNameWithoutExtension(path));
                         *
                         * VRMAssetWriter.SaveAsPrefab(context.Root, prefabPath);
                         *
                         * var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath.ToUnityRelativePath());
                         * Selection.activeObject = prefab;
                         */

                        context.SaveAsAsset();
                        context.Destroy(false);
                    }
                    catch (Exception ex)
                    {
                        Debug.LogError(ex);
                        if (context != null)
                        {
                            context.Destroy(true);
                        }
                    }
                }
            }
        }
Example #5
0
        static void Import(string readPath, UnityPath prefabPath)
        {
            var bytes   = File.ReadAllBytes(readPath);
            var context = new VRMImporterContext(UnityPath.FromFullpath(readPath));

            context.ParseGlb(File.ReadAllBytes(readPath));
            context.SaveTexturesAsPng(prefabPath);

            EditorApplication.delayCall += () =>
            {
                // delay and can import png texture
                VRMImporter.LoadFromBytes(context);
                context.SaveAsAsset(prefabPath);
                context.Destroy(false);
            };
        }
        static void ImportVrm(UnityPath path)
        {
            if (!path.IsUnderAssetsFolder)
            {
                throw new Exception();
            }
            var context = new VRMImporterContext(path);

            context.ParseGlb(File.ReadAllBytes(path.FullPath));

            var prefabPath = path.Parent.Child(path.FileNameWithoutExtension + ".prefab");

            context.SaveTexturesAsPng(prefabPath);

            EditorApplication.delayCall += () =>
            {
                // delay and can import png texture
                VRMImporter.LoadFromBytes(context);
                context.SaveAsAsset(prefabPath);
                context.Destroy(false);
            };
        }