static void ImportDelayed(string src, UnityPath prefabPath, ImporterContext context) { EditorApplication.delayCall += () => { // // After textures imported(To ensure TextureImporter be accessible). // try { context.Load(); context.SaveAsAsset(prefabPath); context.Destroy(false); } catch (UniGLTFNotSupportedException ex) { Debug.LogWarningFormat("{0}: {1}", src, ex.Message ); context.Destroy(true); } catch (Exception ex) { Debug.LogErrorFormat("import error: {0}", src); Debug.LogErrorFormat("{0}", ex); context.Destroy(true); } }; }
public static void Import(UnityPath gltfPath) { if (!gltfPath.IsUnderAssetsFolder) { throw new Exception(); } ImporterContext context = new ImporterContext(gltfPath); var ext = gltfPath.Extension.ToLower(); try { var prefabPath = gltfPath.Parent.Child(gltfPath.FileNameWithoutExtension + ".prefab"); if (ext == ".gltf") { context.ParseJson(File.ReadAllText(gltfPath.FullPath, System.Text.Encoding.UTF8), new FileSystemStorage(gltfPath.Parent.FullPath)); gltfImporter.Load(context); context.SaveAsAsset(prefabPath); context.Destroy(false); } else if (ext == ".glb") { context.ParseGlb(File.ReadAllBytes(gltfPath.FullPath)); context.SaveTexturesAsPng(prefabPath); EditorApplication.delayCall += () => { // delay and can import png texture gltfImporter.Load(context); context.SaveAsAsset(prefabPath); context.Destroy(false); }; } else { return; } } catch (UniGLTFNotSupportedException ex) { Debug.LogWarningFormat("{0}: {1}", gltfPath, ex.Message ); } catch (Exception ex) { Debug.LogErrorFormat("import error: {0}", gltfPath); Debug.LogErrorFormat("{0}", ex); if (context != null) { context.Destroy(true); } } }
public void UniGLTFSimpleSceneTest() { var go = CreateSimpelScene(); var context = new ImporterContext(); try { // export var gltf = new glTF(); using (var exporter = new gltfExporter(gltf)) { exporter.Prepare(go); exporter.Export(); // import context.ParseJson(gltf.ToJson(), new SimpleStorage(new ArraySegment <byte>())); //Debug.LogFormat("{0}", context.Json); gltfImporter.Load(context); AssertAreEqual(go.transform, context.Root.transform); } } finally { //Debug.LogFormat("Destory, {0}", go.name); GameObject.DestroyImmediate(go); context.Destroy(true); } }
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { foreach (string path in importedAssets) { ImporterContext context = null; var ext = Path.GetExtension(path).ToLower(); try { if (ext == ".gltf") { context = ImportGltf(path, false); } else if (ext == ".glb") { context = ImportGltf(path, true); } if (context != null) { context.SaveAsAsset(); } if (context != null) { context.Destroy(false); } } catch (Exception ex) { Debug.LogErrorFormat("import error: {0}", path); Debug.LogErrorFormat("{0}", ex); if (context != null) { context.Destroy(true); } } } }
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { foreach (string path in importedAssets) { ImporterContext context = new ImporterContext { Path = path, }; var ext = Path.GetExtension(path).ToLower(); try { if (ext == ".gltf") { context.ParseJson <glTF>(File.ReadAllText(context.Path, System.Text.Encoding.UTF8), new FileSystemStorage(Path.GetDirectoryName(path))); gltfImporter.Import <glTF>(context); context.SaveAsAsset(); context.Destroy(false); } else if (ext == ".glb") { context.ParseGlb <glTF>(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); } // name & bytes var textureName = !string.IsNullOrEmpty(image.name) ? image.name : string.Format("{0:00}#GLB", i); var byteSegment = context.GLTF.GetViewBytes(image.bufferView); // path var png = Path.Combine(folder, textureName + ".png"); File.WriteAllBytes(png, byteSegment.ToArray()); var assetPath = png.ToUnityRelativePath(); //Debug.LogFormat("import asset {0}", assetPath); UnityEditor.AssetDatabase.ImportAsset(assetPath); image.uri = assetPath.Substring(context.GLTF.baseDir.Length + 1); } } UnityEditor.AssetDatabase.Refresh(); EditorApplication.delayCall += () => { // delay and can import png texture gltfImporter.Import <glTF>(context); context.SaveAsAsset(); context.Destroy(false); }; } else { continue; } } catch (UniGLTFNotSupportedException ex) { Debug.LogWarningFormat("{0}: {1}", path, ex.Message ); } catch (Exception ex) { Debug.LogErrorFormat("import error: {0}", path); Debug.LogErrorFormat("{0}", ex); if (context != null) { context.Destroy(true); } } } }