Example #1
0
        private IEnumerator DoHandleFileLoading()
        {
            var hasFiles = _items != null && (_items.Count > 0 || _items.Count == 1 && _items[0].HasData);

            if (_onBeginLoad != null)
            {
                _onBeginLoad(hasFiles);
            }
            yield return(new WaitForEndOfFrame());

            yield return(new WaitForEndOfFrame());

            if (!hasFiles)
            {
                DestroyMe();
                yield break;
            }
            var modelFileWithStream = FindModelFile();
            var modelFilename       = modelFileWithStream.Name;
            var modelStream         = modelFileWithStream.OpenStream();

            if (_assetLoaderOptions == null)
            {
                _assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
            }
            _assetLoaderOptions.TextureMapper      = ScriptableObject.CreateInstance <FilePickerTextureMapper>();
            _assetLoaderOptions.ExternalDataMapper = ScriptableObject.CreateInstance <FilePickerExternalDataMapper>();
            _assetLoaderOptions.FixedAllocations.Add(_assetLoaderOptions.ExternalDataMapper);
            _assetLoaderOptions.FixedAllocations.Add(_assetLoaderOptions.TextureMapper);
            _modelExtension = modelFilename != null?FileUtils.GetFileExtension(modelFilename, false) : null;

            if (_modelExtension == "zip")
            {
                if (modelStream != null)
                {
                    AssetLoaderZip.LoadModelFromZipStream(modelStream, _onLoad, _onMaterialsLoad, _onProgress, _onError, _wrapperGameObject, _assetLoaderOptions, _items, null);
                }
                else
                {
                    AssetLoaderZip.LoadModelFromZipFile(modelFilename, _onLoad, _onMaterialsLoad, _onProgress, _onError, _wrapperGameObject, _assetLoaderOptions, _items, null);
                }
            }
            else
            {
                if (modelStream != null)
                {
                    AssetLoader.LoadModelFromStream(modelStream, modelFilename, _modelExtension, _onLoad, _onMaterialsLoad, _onProgress, _onError, _wrapperGameObject, _assetLoaderOptions, _items);
                }
                else
                {
                    AssetLoader.LoadModelFromFile(modelFilename, _onLoad, _onMaterialsLoad, _onProgress, _onError, _wrapperGameObject, _assetLoaderOptions, _items);
                }
            }
            DestroyMe();
        }
        /// <summary>Downloads the Model using the given Request and options.</summary>
        /// <param name="unityWebRequest">The Unity Web Request used to load the Model. You can use the CreateWebRequest method to create a new Unity Web Request or pass your instance.</param>
        /// <param name="onLoad">The Method to call on the Main Thread when the Model is loaded but resources may still pending.</param>
        /// <param name="onMaterialsLoad">The Method to call on the Main Thread when the Model and resources are loaded.</param>
        /// <param name="onProgress">The Method to call when the Model loading progress changes.</param>
        /// <param name="wrapperGameObject">The Game Object that will be the parent of the loaded Game Object. Can be null.</param>
        /// <param name="onError">The Method to call on the Main Thread when any error occurs.</param>
        /// <param name="assetLoaderOptions">The options to use when loading the Model.</param>
        /// <param name="customContextData">The Custom Data that will be passed along the Context.</param>
        /// <param name="fileExtension">The extension of the URI Model.</param>
        /// <param name="isZipFile">Pass <c>true</c> if your file is a Zip file.</param>
        /// <returns>The download coroutine enumerator.</returns>
        public IEnumerator DownloadAsset(UnityWebRequest unityWebRequest, Action <AssetLoaderContext> onLoad, Action <AssetLoaderContext> onMaterialsLoad, Action <AssetLoaderContext, float> onProgress, GameObject wrapperGameObject, Action <IContextualizedError> onError, AssetLoaderOptions assetLoaderOptions, object customContextData, string fileExtension, bool?isZipFile = null)
        {
            _unityWebRequest = unityWebRequest;
            _onProgress      = onProgress;
            yield return(unityWebRequest.SendWebRequest());

            try
            {
                if (unityWebRequest.responseCode < 400)
                {
                    var memoryStream             = new MemoryStream(_unityWebRequest.downloadHandler.data);
                    var uriLoadCustomContextData = new UriLoadCustomContextData
                    {
                        UnityWebRequest = _unityWebRequest,
                        CustomData      = customContextData
                    };
                    var contentType = unityWebRequest.GetResponseHeader("Content-Type");
                    if (contentType != null && isZipFile == null)
                    {
                        isZipFile = contentType.Contains("application/zip") || contentType.Contains("application/x-zip-compressed") || contentType.Contains("multipart/x-zip");
                    }
                    if (!isZipFile.GetValueOrDefault() && string.IsNullOrWhiteSpace(fileExtension))
                    {
                        fileExtension = FileUtils.GetFileExtension(unityWebRequest.url);
                    }
                    if (isZipFile.GetValueOrDefault())
                    {
                        _assetLoaderContext = AssetLoaderZip.LoadModelFromZipStream(memoryStream, onLoad, onMaterialsLoad, delegate(AssetLoaderContext assetLoaderContext, float progress) { onProgress?.Invoke(assetLoaderContext, 0.5f + progress * 0.5f); }, onError, wrapperGameObject, assetLoaderOptions, uriLoadCustomContextData, fileExtension);
                    }
                    else
                    {
                        _assetLoaderContext = AssetLoader.LoadModelFromStream(memoryStream, null, fileExtension, onLoad, onMaterialsLoad, delegate(AssetLoaderContext assetLoaderContext, float progress) { onProgress?.Invoke(assetLoaderContext, 0.5f + progress * 0.5f); }, onError, wrapperGameObject, assetLoaderOptions, uriLoadCustomContextData);
                    }
                }
                else
                {
                    var exception = new Exception($"UnityWebRequest error:{unityWebRequest.error}, code:{unityWebRequest.responseCode}");
                    throw exception;
                }
            }
            catch (Exception exception)
            {
                if (onError != null)
                {
                    var contextualizedError = exception as IContextualizedError;
                    onError(contextualizedError ?? new ContextualizedError <AssetLoaderContext>(exception, null));
                }
                else
                {
                    throw;
                }
            }
            Destroy(gameObject);
        }