Пример #1
0
        private static async Task Save(GLTFImporter context, UnityPath path)
        {
            await context.Load(context.storage, p => Debug.Log(p));

            context.ShowMeshes();
            await SaveAsAsset(context, path);

            context.EditorDestroyRoot();
        }
Пример #2
0
        public static void ImportMenu()
        {
            var path = EditorUtility.OpenFilePanel("open gltf", "", "gltf,glb,zip");

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

            if (Application.isPlaying)
            {
                //
                // load into scene
                //
                var context = new GLTFImporter();
                //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;
                }

                var assetPath = EditorUtility.SaveFilePanel("save prefab", "Assets", Path.GetFileNameWithoutExtension(path), "prefab");
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                // import as asset
                GLTFAssetPostprocessor.ImportAsset(path, Path.GetExtension(path).ToLower(), UnityPath.FromFullpath(assetPath));
            }
        }
Пример #3
0
        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);
        }