GameObject Load(byte[] bytes) { Debug.LogFormat("[OnClick] {0}", path); var context = new ImporterContext(); context.ParseGlb(bytes); gltfImporter.Load(context); context.Root.name = Path.GetFileNameWithoutExtension(path); context.ShowMeshes(); return(context.Root); }
/// <summary> /// Import GLB. /// </summary> /// <returns></returns> public static void ImportGlb() { string path = EditorUtility.OpenFilePanel(title: "Open File Dialog", directory: "", extension: "glb"); if (string.IsNullOrEmpty(path)) { return; } if (Application.isPlaying) { // // load into scene // var context = new ImporterContext(); context.Load(path); context.ShowMeshes(); Selection.activeGameObject = context.Root; } else { // // save as asset // if (path.StartsWithUnityAssetPath()) { Debug.LogWarningFormat("disallow import from folder under the Assets"); return; } string assetPath = EditorUtility.SaveFilePanel(title: "Save prefab", directory: "Assets", defaultName: Path.GetFileNameWithoutExtension(path), extension: "prefab"); if (string.IsNullOrEmpty(path)) { return; } // import as asset GlbAssetPostprocessor.ImportAsset(src: path, prefabPath: UnityPath.FromFullpath(assetPath)); } }
GameObject Load(string path) { var bytes = File.ReadAllBytes(path); Debug.LogFormat("[OnClick] {0}", path); var context = new ImporterContext(); var ext = Path.GetExtension(path).ToLower(); switch (ext) { case ".gltf": context.ParseJson(Encoding.UTF8.GetString(bytes), new FileSystemStorage(Path.GetDirectoryName(path))); break; case ".zip": { var zipArchive = UniGLTF.Zip.ZipArchiveStorage.Parse(bytes); var gltf = zipArchive.Entries.FirstOrDefault(x => x.FileName.ToLower().EndsWith(".gltf")); if (gltf == null) { throw new Exception("no gltf in archive"); } var jsonBytes = zipArchive.Extract(gltf); var json = Encoding.UTF8.GetString(jsonBytes); context.ParseJson(json, zipArchive); } break; case ".glb": context.ParseGlb(bytes); break; default: throw new NotImplementedException(); } gltfImporter.Load(context); context.Root.name = Path.GetFileNameWithoutExtension(path); context.ShowMeshes(); return(context.Root); }
void GLB(string path) { var bytes = File.ReadAllBytes(path); var context = new ImporterContext(); context.ParseGlb(bytes); context.LoadAsync(() => { context.ShowMeshes(); var markerM = Instantiate(MarkerM).transform; markerM.name = "GLB_Root"; markerM.position = new Vector3(0, -1, 0); markerM.gameObject.AddComponent <SaveSceneTarget>().Path = path; context.Root.transform.parent = markerM; LoadCount++; }); DandD.SetActive(false); }
IEnumerator GetObjects() { yield return(new WaitForSeconds(3)); Debug.Log("Objects request"); //If not empty, update the ARObject list and remove the old objects RemoveOldObjects(); //For each objects foreach (ObjectInfo inf in objectsInfos_Near) { //We check first if the object already is spawned if (!objectsInfos_Near_Old.Contains(inf)) { Debug.Log("NOT SPAWNDED"); string filepath = filepath_objects + inf.name; //We check first if the objects already exists on the phone if (File.Exists(filepath)) { Mapbox.Unity.Utilities.Console.Instance.Log("File exists", "yellow"); } else { //Get the data from the server UnityWebRequest request = UnityWebRequest.Get(URL_getObjects + "/" + inf.name); //Set a twelve seconds timeout in case an error isn't returned request.timeout = 12; Mapbox.Unity.Utilities.Console.Instance.Log("URL : " + URL_getObjects + "/" + inf.name, "cyan"); yield return(request.SendWebRequest()); if (request.isNetworkError || request.isHttpError) { Mapbox.Unity.Utilities.Console.Instance.Log(request.error, "yellow"); } else { Mapbox.Unity.Utilities.Console.Instance.Log("data size :" + request.downloadHandler.data.Length, "yellow"); //Write object as local file try { File.WriteAllBytes(filepath, request.downloadHandler.data); Mapbox.Unity.Utilities.Console.Instance.Log("File writed", "yellow"); } catch (Exception e) { Mapbox.Unity.Utilities.Console.Instance.Log(e.Message, "red"); } } } //Load the glb file as a gameobject ImporterContext context = gltfImporter.Load(filepath); context.ShowMeshes(); //ADD ROTATE cnt++; if (cnt == 4 || cnt == 8 || cnt == 3 || cnt == 7) { context.Root.AddComponent <RotateObject>(); } //Add it to the AR objects array ARObjects.Add(context.Root); } } //Requests are finshed and objects loaded areRequestsFinished = true; Mapbox.Unity.Utilities.Console.Instance.Log("Finished reqs", "cyan"); }