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(); } }
private async Task <GameObject> Load(Action <float> progress) { await Task.Yield(); _unloaded = false; var name = url.Substring(url.LastIndexOf("/") + 1); //加载.gltf文件 await _storage.LoadString(name, p => progress?.Invoke(p * 0.1f)); if (_unloaded) { return(null); } _loader = new GLTFImporter(this, null, null, null, null); //用JsonUtility解析到gltf数据 _loader.ParseJson(_storage.GetString(name)); //加载buffers里面的.bin数据 int total = _loader.gltf.buffers.Count; int current = 0; var stepPrecent = showWithTexture ? 0.4f : 0.8f; foreach (var buffer in _loader.gltf.buffers) { Debug.Log(buffer.uri); await _storage.LoadBinary(buffer.uri, p => progress?.Invoke(0.1f + stepPrecent * (current + p) / total)); if (_unloaded) { return(null); } //Debug.Log(buffer.uri + " loaded"); buffer.OpenStorage(_storage); current++; } //跳过图片的加载 if (showWithTexture) { current = 0; total = _loader.gltf.images.Count; foreach (var image in _loader.gltf.images) { await _storage.LoadTexture(image.uri, p => progress?.Invoke(0.5f + 0.4f * (current + p) / total)); current++; if (_unloaded) { return(null); } } } //解析mesh、material、animation等数据 await _loader.Load(_storage, p => progress?.Invoke(0.9f + p * 0.1f)); if (_unloaded) { return(null); } //loader.Parse(url,www.downloadHandler.data); _loader.ShowMeshes(); _loader.root.SetActive(false); _loader.root.transform.SetParent(transform); _loader.root.SetActive(true); return(_loader.root); }