Beispiel #1
0
        private static void ExportFromMenu()
        {
            var go   = Selection.activeObject as GameObject;
            var path = EditorUtility.SaveFilePanel("Save glb", "", go.name + ".glb", "glb");

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

            var gltf = new GLTFRoot();

            using (var exporter = new GLTFExporter(gltf))
            {
                exporter.Prepare(go);
                exporter.Export();
            }
            var bytes = gltf.ToGlbBytes();

            File.WriteAllBytes(path, bytes);

            if (path.StartsWithUnityAssetPath())
            {
                AssetDatabase.ImportAsset(path.ToUnityRelativePath());
                AssetDatabase.Refresh();
            }
        }
Beispiel #2
0
        private static async Task SaveFiles(string path, string name, GLTFExporter exporter)
        {
            var buffers = exporter.gltf.buffers;

            if (buffers.Any())
            {
                for (int i = 0; i < buffers.Count; i++)
                {
                    buffers[i].uri = i == 0 ? $"{name}.bin" : $"{name}_{i}bin";
                }
            }
            var p = $"{path}{Path.AltDirectorySeparatorChar}{name}.gltf";

            Debug.Log("json write ..." + p);
            var json = exporter.gltf.ToJson();
            await Task.Yield();

            Debug.Log(json);
            File.WriteAllText(p, json);
            await Task.Yield();

            p = $"{path}{Path.AltDirectorySeparatorChar}{name}.bin";
            Debug.Log("bin write ..." + p);
            File.WriteAllBytes(p, exporter.gltf.ToBinary());
            await Task.Yield();

            Debug.Log("===>" + exporter.textureManager.unityTextures.Count);
            for (int i = 0; i < exporter.textureManager.unityTextures.Count; i++)
            {
                var ut  = exporter.textureManager.unityTextures[i];
                var tex = exporter.textureManager.GetExportTexture(i);
                var bwm = tex.GetBytesWithMime(ut.TextureType, 0);
                p = $"{path}{Path.AltDirectorySeparatorChar}{tex.name.ToLower()}.png";
                Debug.Log("save png " + i + " -- " + p);
                File.WriteAllBytes(p, bwm.bytes);
                await Task.Yield();
            }
            exporter.Dispose();
            Debug.Log("export complete " + path);
        }
Beispiel #3
0
        private static void ExportGLTFFromMenu()
        {
            var go   = Selection.activeObject as GameObject;
            var path = EditorUtility.SaveFolderPanel("Save GLTF", "", go.name);

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

            Debug.Log("export ... " + path);
            if (!path.EndsWith(go.name))
            {
                path += Path.AltDirectorySeparatorChar + go.name;
            }
            Debug.Log(path + "exist = " + Directory.Exists(path));
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var gltf     = new GLTFRoot();
            var exporter = new GLTFExporter(gltf);

            exporter.Prepare(go);
            exporter.Export();
            Debug.Log("exported");
            var res = SaveFiles(path, go.name, exporter);
            // return;
            // var bytes = gltf.ToGlbBytes();
            // File.WriteAllBytes(path, bytes);

            // if (path.StartsWithUnityAssetPath())
            // {
            //     AssetDatabase.ImportAsset(path.ToUnityRelativePath());
            //     AssetDatabase.Refresh();
            // }
        }
Beispiel #4
0
        public void UniGLTFSimpleSceneTest()
        {
            var go      = CreateSimpelScene();
            var context = new GLTFImporter();

            try
            {
                // export
                var gltf = new GLTFRoot();

                string json = null;
                using (var exporter = new GLTFExporter(gltf))
                {
                    exporter.Prepare(go);
                    exporter.Export();

                    // remove empty buffer
                    gltf.buffers.Clear();

                    json = gltf.ToJson();
                }

                // import
                context.ParseJson(json, new SimpleStorage(new ArraySegment <byte>()));
                //Debug.LogFormat("{0}", context.Json);
                //context.Load();

                AssertAreEqual(go.transform, context.root.transform);
            }
            finally
            {
                //Debug.LogFormat("Destory, {0}", go.name);
                GameObject.DestroyImmediate(go);
                context.EditorDestroyRootAndAssets();
            }
        }