public Thread LoadFromFile(string filename, AssetLoaderOptions options = null, GameObject wrapperGameObject = null,
                                   ObjectLoadedHandle onAssetLoaded            = null, string basePath = null, AssimpInterop.ProgressCallback progressCallback = null)
#endif
        {
            if (basePath == null)
            {
                basePath = FileUtils.GetFileDirectory(filename);
            }
            var usesWrapperGameObject = wrapperGameObject != null;

            return(ThreadUtils.RunThread(delegate
            {
                InternalLoadFromFile(filename, basePath, options, usesWrapperGameObject, progressCallback);
            },
                                         delegate
            {
                var loadedGameObject = BuildGameObject(options, basePath, wrapperGameObject);
                if (onAssetLoaded != null)
                {
                    onAssetLoaded(loadedGameObject);
                }
                ReleaseImport();
            }
                                         ));
        }
Beispiel #2
0
 /// <summary>
 /// Downloads an asset.
 /// </summary>
 /// <returns><c>true</c>, if asset was downloaded, <c>false</c> otherwise.</returns>
 /// <param name="assetURI">Asset URI.</param>
 /// <param name="assetExtension">Asset extension.</param>
 /// <param name="onAssetLoaded">On asset loaded event.</param>
 /// <param name="onTexturePreLoad">On texture pre load event.</param>
 /// <param name="options">Asset loading options.</param>
 /// <param name="wrapperGameObject">Wrapper <see cref="UnityEngine.GameObject"/> to load the asset into.</param>
 /// <param name="progressCallback">Callback used to retrieve file loading percentage.</param>
 public bool DownloadAsset(string assetURI, string assetExtension, ObjectLoadedHandle onAssetLoaded = null, TexturePreLoadHandle onTexturePreLoad = null, AssetLoaderOptions options = null, GameObject wrapperGameObject = null, AssimpInterop.ProgressCallback progressCallback = null)
 {
     if (HasStarted && !IsDone)
     {
         return(false);
     }
     AssetURI          = assetURI;
     AssetExtension    = assetExtension;
     WrapperGameObject = wrapperGameObject;
     StartCoroutine(DoDownloadAsset(assetURI, assetExtension, onAssetLoaded, options, wrapperGameObject, progressCallback));
     return(true);
 }
Beispiel #3
0
        /// <summary>
        /// Shows the progress on screen, if ShowProgress is set to <c>true</c>.
        /// </summary>
        //protected void OnGUI()
        //{
        //    if (!ShowProgress || !HasStarted || IsDone)
        //    {
        //        return;
        //    }
        //    if (_centeredStyle == null)
        //    {
        //        _centeredStyle = GUI.skin.GetStyle("Label");
        //        _centeredStyle.alignment = TextAnchor.UpperCenter;
        //    }
        //    var centeredRect = new Rect(Screen.width / 2f - 100f, Screen.height / 2f - 25f, 200f, 50f);
        //    GUI.Label(centeredRect, string.Format("Downloaded {0:P2}", Progress), _centeredStyle);
        //}

        /// <summary>
        /// Downloads an asset.
        /// </summary>
        /// <returns><c>true</c>, if asset was downloaded, <c>false</c> otherwise.</returns>
        /// <param name="assetURI">Asset URI.</param>
        /// <param name="assetExtension">Asset extension.</param>
        /// <param name="onAssetLoaded">On asset loaded event.</param>
        /// <param name="onTexturePreLoad">On texture pre load event.</param>
        /// <param name="options">Asset loading options.</param>
        /// <param name="wrapperGameObject">Wrapper <see cref="UnityEngine.GameObject"/> to load the asset into.</param>
        public bool DownloadAsset(string assetURI, string assetExtension, ObjectLoadedHandle onAssetLoaded = null, TexturePreLoadHandle onTexturePreLoad = null, AssetLoaderOptions options = null, GameObject wrapperGameObject = null)
        {
            if (HasStarted && !IsDone)
            {
                return(false);
            }

            AssetURI          = assetURI;
            AssetExtension    = assetExtension;
            WrapperGameObject = wrapperGameObject;

            DownloadEvents.DownloadStart();

            StartCoroutine(DoDownloadAsset(assetURI, assetExtension, onAssetLoaded, options, wrapperGameObject));
            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// Internal asset download coroutine.
        /// </summary>
        /// <returns>The coroutine IEnumerator.</returns>
        /// <param name="assetURI">Asset URI.</param>
        /// <param name="assetExtension">Asset extension.</param>
        /// <param name="onAssetLoaded">On asset loaded event.</param>
        /// <param name="onTexturePreLoad">On texture pre load event.</param>
        /// <param name="options">Asset loading options.</param>
        /// <param name="wrapperGameObject">Wrapper <see cref="UnityEngine.GameObject"/> to load the asset into.</param>
        private IEnumerator DoDownloadAsset(string assetURI, string assetExtension, ObjectLoadedHandle onAssetLoaded, TexturePreLoadHandle onTexturePreLoad = null, AssetLoaderOptions options = null, GameObject wrapperGameObject = null)
        {
            _unityWebRequest = UnityWebRequest.Get(assetURI);
            yield return(_unityWebRequest.Send());

            if (string.IsNullOrEmpty(_unityWebRequest.error))
            {
                var data = _unityWebRequest.downloadHandler.data;
                using (var assetLoader = new AssetLoader())
                {
                    assetLoader.LoadFromMemoryWithTextures(data, assetExtension, onAssetLoaded, out _error, onTexturePreLoad, options, wrapperGameObject);
                }
            }
            _unityWebRequest.Dispose();
            _unityWebRequest = null;
        }
Beispiel #5
0
        /// <summary>
        /// Internal asset download coroutine.
        /// </summary>
        /// <returns>The coroutine IEnumerator.</returns>
        /// <param name="assetUri">Asset URI.</param>
        /// <param name="assetExtension">Asset extension.</param>
        /// <param name="onAssetLoaded">On asset loaded event.</param>
        /// <param name="options">Asset loading options.</param>
        /// <param name="wrapperGameObject">Wrapper <see cref="UnityEngine.GameObject"/> to load the asset into.</param>
        private IEnumerator DoDownloadAsset(string assetUri, string assetExtension, ObjectLoadedHandle onAssetLoaded, AssetLoaderOptions options = null, GameObject wrapperGameObject = null)
        {
            _unityWebRequest         = UnityWebRequest.Get(assetUri);
            _unityWebRequest.timeout = Timeout;
#if UNITY_2017_3_OR_NEWER
            yield return(_unityWebRequest.SendWebRequest());
#else
            yield return(_unityWebRequest.Send());
#endif
            if (string.IsNullOrEmpty(_unityWebRequest.error))
            {
                var data = _unityWebRequest.downloadHandler.data;
                try
                {
                    if (Async)
                    {
                        using (var assetLoaderAsync = new AssetLoaderAsync())
                        {
                            assetLoaderAsync.LoadFromMemoryWithTextures(data, assetExtension, options, wrapperGameObject,
                                                                        onAssetLoaded);
                        }
                    }
                    else
                    {
                        using (var assetLoader = new AssetLoader())
                        {
                            assetLoader.OnObjectLoaded += onAssetLoaded;
                            assetLoader.LoadFromMemoryWithTextures(data, assetExtension, options, wrapperGameObject);
                        }
                    }
                }
                catch (Exception exception)
                {
                    _error = exception.ToString();
                    if (onAssetLoaded != null)
                    {
                        onAssetLoaded(null);
                    }
                }
            }
            else
            {
                onAssetLoaded(null);
            }
            _unityWebRequest.Dispose();
            _unityWebRequest = null;
        }
Beispiel #6
0
        public Thread LoadFromMemory(byte[] fileBytes, string filename, AssetLoaderOptions options,
                                     GameObject wrapperGameObject, ObjectLoadedHandle onAssetLoaded)
#endif
        {
            return(ThreadUtils.RunThread(delegate
            {
                InternalLoadFromMemory(fileBytes, filename, options);
            },
                                         delegate
            {
                var loadedGameObject = BuildGameObject(options, filename, wrapperGameObject);
                if (onAssetLoaded != null)
                {
                    onAssetLoaded(loadedGameObject);
                }
            }
                                         ));
        }
        public Thread LoadFromFile(string filename, AssetLoaderOptions options, GameObject wrapperGameObject,
                                   ObjectLoadedHandle onAssetLoaded)
#endif
        {
            var basePath = FileUtils.GetFileDirectory(filename);
            var usesWrapperGameObject = wrapperGameObject != null;

            return(ThreadUtils.RunThread(delegate
            {
                InternalLoadFromFile(filename, basePath, options, usesWrapperGameObject);
            },
                                         delegate
            {
                var loadedGameObject = BuildGameObject(options, basePath, wrapperGameObject);
                if (onAssetLoaded != null)
                {
                    onAssetLoaded(loadedGameObject);
                }
            }
                                         ));
        }
Beispiel #8
0
        public Thread LoadFromMemory(byte[] fileBytes, string filename, AssetLoaderOptions options,
                                     GameObject wrapperGameObject, ObjectLoadedHandle onAssetLoaded, string basePath = null, AssimpInterop.DataCallback dataCallback = null, AssimpInterop.ExistsCallback existsCallback = null, LoadTextureDataCallback loadTextureDataCallback = null)
#endif
        {
            if (basePath == null)
            {
                basePath = FileUtils.GetFileDirectory(filename);
            }
            var usesWrapperGameObject = wrapperGameObject != null;

            return(ThreadUtils.RunThread(delegate
            {
                InternalLoadFromMemory(fileBytes, filename, basePath, options, usesWrapperGameObject, dataCallback, existsCallback, loadTextureDataCallback);
            },
                                         delegate
            {
                var loadedGameObject = BuildGameObject(options, filename, wrapperGameObject);
                if (onAssetLoaded != null)
                {
                    onAssetLoaded(loadedGameObject);
                }
            }
                                         ));
        }
Beispiel #9
0
        /// <summary>
        /// Asynchronously loads a <see cref="UnityEngine.GameObject"/> from input filename with defined options.
        /// @warning To ensure your materials will be loaded, don´t remove the material files included in the package.
        /// </summary>
        /// <param name="filename">Filename used to load the <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="options"><see cref="AssetLoaderOptions"/> used to load the object.</param>
        /// <param name="wrapperGameObject">Use this field to load the new <see cref="UnityEngine.GameObject"/> into referenced <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="onAssetLoaded">The action that will be executed when the <see cref="UnityEngine.GameObject"/> be loaded.</param>
        /// <returns>The created Thread on NET 2.0, otherwise returns the created Task.</returns>
        /// <example>
        /// @code
        /// protected void Awake() {
        ///     GameObject myGameObject;
        ///     try {
        ///         using (var assetLoader = new AssetLoader()) {
        ///             assetLoader.LoadFromFile("mymodel.fbx", null, null, delegate(GameObject loadedGameObject) {
        ///                 Debug.Log("My object '" + loadedGameObject.name +  "' has been loaded!");
        ///             });
        ///         }
        ///     } catch (Exception e) {
        ///         Debug.LogFormat("Unable to load mymodel.fbx. The loader returned: {0}", e);
        ///     }
        /// }
        /// @endcode
        /// </example>
#if (NET_4_6 || NETFX_CORE)
        public Task LoadFromFileAsync(string filename, AssetLoaderOptions options, GameObject wrapperGameObject,
                                      ObjectLoadedHandle onAssetLoaded)

        {
            return((Task)LoadFileInternal(filename, options, wrapperGameObject, onAssetLoaded));
        }
Beispiel #10
0
 public Thread LoadFromFileAsync(string filename, AssetLoaderOptions options = null, GameObject wrapperGameObject = null,
                                 ObjectLoadedHandle onAssetLoaded            = null)
 {
     return((Thread)LoadFileInternal(filename, options, wrapperGameObject, onAssetLoaded, true));
 }
Beispiel #11
0
        /// <summary>
        /// Asynchronously loads a <see cref="UnityEngine.GameObject"/> from input byte array with defined options.
        /// @warning To ensure your materials will be loaded, don´t remove material files included in the package.
        /// </summary>
        /// <param name="fileBytes">Data used to load the <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="filename">Original file name, if you know it. Otherwise, use the original file extension instead. (Eg: ".FBX")</param>
        /// <param name="options"><see cref="AssetLoaderOptions"/> used to load the object.</param>
        /// <param name="wrapperGameObject">Use this field to load the new <see cref="UnityEngine.GameObject"/> into referenced <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="onAssetLoaded">The action that will be executed when the <see cref="UnityEngine.GameObject"/> be loaded</param>
        /// <returns>The created Thread on NET 2.0, otherwise returns the created Task.</returns>
        /// <example>
        /// @code
        /// protected void Awake() {
        ///     GameObject myGameObject;
        ///     try {
        ///         using (var assetLoader = new AssetLoader()) {
        ///             //In case you don't have a valid filename, set this to the file extension
        ///             //to help TriLib assigining a file loader to this file
        ///             //example value: ".FBX"
        ///             var filename = "c:/models/mymodel.fbx";
        ///             var fileData = File.ReadAllBytes(filename);
        ///             assetLoader.LoadFromMemory(fleData, filename, delegate(GameObject loadedGameObject) {
        ///                 Debug.Log("My object '" + loadedGameObject.name +  "' has been loaded!");
        ///             });
        ///         }
        ///     } catch (Exception e) {
        ///         Debug.LogFormat("Unable to load mymodel.fbx. The loader returned: {0}", e);
        ///     }
        /// }
        /// @endcode
        /// </example>
#if (NET_4_6 || NETFX_CORE)
        public Task LoadFromMemory(byte[] fileBytes, string filename, AssetLoaderOptions options,
                                   GameObject wrapperGameObject, ObjectLoadedHandle onAssetLoaded)
Beispiel #12
0
        /// <summary>
        /// Asynchronously loads a <see cref="UnityEngine.GameObject"/> from input filename with defined options.
        /// @warning To ensure your materials will be loaded, don´t remove the material files included in the package.
        /// </summary>
        /// <param name="filename">Filename used to load the <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="options"><see cref="AssetLoaderOptions"/> used to load the object.</param>
        /// <param name="wrapperGameObject">Use this field to load the new <see cref="UnityEngine.GameObject"/> into referenced <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="onAssetLoaded">The action that will be executed when the <see cref="UnityEngine.GameObject"/> be loaded</param>
        /// <param name="basePath">Base path from the loaded file.</param>
        /// <returns>The created Thread on NET 2.0, otherwise returns the created Task.</returns>
        /// <example>
        /// @code
        /// protected void Awake() {
        ///     GameObject myGameObject;
        ///     try {
        ///         using (var assetLoader = new AssetLoader()) {
        ///             assetLoader.LoadFromFile("mymodel.fbx", null, null, delegate(GameObject loadedGameObject) {
        ///                 Debug.Log("My object '" + loadedGameObject.name +  "' has been loaded!");
        ///             });
        ///         }
        ///     } catch (Exception e) {
        ///         Debug.LogFormat("Unable to load mymodel.fbx. The loader returned: {0}", e);
        ///     }
        /// }
        /// @endcode
        /// </example>
#if (NET_4_6 || NETFX_CORE)
        public Task LoadFromFile(string filename, AssetLoaderOptions options, GameObject wrapperGameObject,
                                 ObjectLoadedHandle onAssetLoaded, string basePath = null)
Beispiel #13
0
        /// <summary>
        /// Asynchronously loads a <see cref="UnityEngine.GameObject"/> from input byte array (Accept ZIP files).
        /// </summary>
        /// <param name="fileData">File data.</param>
        /// <param name="assetExtension">Asset extension.</param>
        /// <param name="options"><see cref="AssetLoaderOptions"/> used to load the object.</param>
        /// <param name="wrapperGameObject">Use this field to load the new <see cref="UnityEngine.GameObject"/> into referenced <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="onAssetLoaded">The action that will be executed when the <see cref="UnityEngine.GameObject"/> be loaded</param>
        /// <param name="basePath">Base path from the loaded file.</param>
        /// <param name="dataCallback">Custom resource data retrieval callback. Pass this parameter when you need to load external data while loading from memory.</param>
        /// <param name="existsCallback">Custom resource size retrieval callback. Pass this parameter when you need to load external data while loading from memory.</param>
        /// <returns>The created Thread on NET 2.0, otherwise returns the created Task.</returns>
#if (NET_4_6 || NETFX_CORE)
        public Task LoadFromMemoryWithTextures(byte[] fileData, string assetExtension, AssetLoaderOptions options, GameObject wrapperGameObject, ObjectLoadedHandle onAssetLoaded, string basePath = null, AssimpInterop.DataCallback dataCallback = null, AssimpInterop.ExistsCallback existsCallback = null)
        /// <summary>
        /// Asynchronously loads a <see cref="UnityEngine.GameObject"/> from input filename with defined options.
        /// @warning To ensure your materials will be loaded, don´t remove the material files included in the package.
        /// </summary>
        /// <param name="filename">Filename used to load the <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="options"><see cref="AssetLoaderOptions"/> used to load the object.</param>
        /// <param name="wrapperGameObject">Use this field to load the new <see cref="UnityEngine.GameObject"/> into referenced <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="onAssetLoaded">The action that will be executed when the <see cref="UnityEngine.GameObject"/> be loaded</param>
        /// <param name="basePath">Base path from the loaded file.</param>
        /// <param name="progressCallback">Callback used to retrieve file loading percentage.</param>
        /// <returns>The created Thread on NET 2.0, otherwise returns the created Task.</returns>
        /// <example>
        /// @code
        /// protected void Awake() {
        ///     GameObject myGameObject;
        ///     try {
        ///         using (var assetLoader = new AssetLoaderAsync()) {
        ///             assetLoader.LoadFromFile("mymodel.fbx", null, null, delegate(GameObject loadedGameObject) {
        ///                 Debug.Log("My object '" + loadedGameObject.name +  "' has been loaded!");
        ///             });
        ///         }
        ///     } catch (Exception e) {
        ///         Debug.LogFormat("Unable to load mymodel.fbx. The loader returned: {0}", e);
        ///     }
        /// }
        /// @endcode
        /// </example>
#if !UNITY_EDITOR && (NETFX_CORE || NET_4_6 || NET_STANDARD_2_0) && !ENABLE_IL2CPP && !ENABLE_MONO
        public Task LoadFromFile(string filename, AssetLoaderOptions options = null, GameObject wrapperGameObject = null,
                                 ObjectLoadedHandle onAssetLoaded            = null, string basePath = null, AssimpInterop.ProgressCallback progressCallback = null)
Beispiel #15
0
        /// <summary>
        /// Internally asynchronously loads a <see cref="UnityEngine.GameObject"/> from input filename with defined options.
        /// </summary>
        /// <param name="filename">Filename used to load the <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="options"><see cref="AssetLoaderOptions"/> used to load the object.</param>
        /// <param name="wrapperGameObject">Use this field to load the new <see cref="UnityEngine.GameObject"/> into referenced <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="onAssetLoaded">The action that will be executed when the <see cref="UnityEngine.GameObject"/> be loaded.</param>
        /// <param name="progressCallback">Callback used to retrieve file loading percentage.</param>
        /// <returns>The created Thread on NET 2.0, otherwise returns the created Task.</returns>
        private object AsyncLoadFileInternal(string filename, AssetLoaderOptions options, GameObject wrapperGameObject, ObjectLoadedHandle onAssetLoaded, AssimpInterop.ProgressCallback progressCallback = null)
        {
            var basePath = FileUtils.GetFileDirectory(filename);
            var usesWrapperGameObject = wrapperGameObject != null;

            return(ThreadUtils.RunThread(delegate
            {
                InternalLoadFromFile(filename, basePath, options, usesWrapperGameObject, progressCallback);
            },
                                         delegate
            {
                var loadedGameObject = BuildGameObject(options, basePath, wrapperGameObject);
                if (onAssetLoaded != null)
                {
                    onAssetLoaded(loadedGameObject);
                }
                ReleaseImport();
            }
                                         ));
        }
Beispiel #16
0
        /// <summary>
        /// Asynchronously loads a <see cref="UnityEngine.GameObject"/> from input byte array with defined options.
        /// @warning To ensure your materials will be loaded, don´t remove material files included in the package.
        /// </summary>
        /// <param name="fileBytes">Data used to load the <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="filename">Original file name, if you know it. Otherwise, use the original file extension instead. (Eg: ".FBX")</param>
        /// <param name="options"><see cref="AssetLoaderOptions"/> used to load the object.</param>
        /// <param name="wrapperGameObject">Use this field to load the new <see cref="UnityEngine.GameObject"/> into referenced <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="onAssetLoaded">The action that will be executed when the <see cref="UnityEngine.GameObject"/> be loaded</param>
        /// <param name="basePath">Base path from the loaded file.</param>        ///
        /// <param name="dataCallback">Custom resource data retrieval callback. Pass this parameter when you need to load external data while loading from memory.</param>
        /// <param name="existsCallback">Custom resource size retrieval callback. Pass this parameter when you need to load external data while loading from memory.</param>
        /// <param name="loadTextureDataCallback">Pass this callback to load texture data from custom sources.</param>
        /// <returns>The created Thread on NET 2.0, otherwise returns the created Task.</returns>
        /// <example>
        /// @code
        /// protected void Awake() {
        ///     GameObject myGameObject;
        ///     try {
        ///         using (var assetLoader = new AssetLoader()) {
        ///             //In case you don't have a valid filename, set this to the file extension
        ///             //to help TriLib assigining a file loader to this file
        ///             //example value: ".FBX"
        ///             var filename = "c:/models/mymodel.fbx";
        ///             var fileData = File.ReadAllBytes(filename);
        ///             assetLoader.LoadFromMemory(fleData, filename, delegate(GameObject loadedGameObject) {
        ///                 Debug.Log("My object '" + loadedGameObject.name +  "' has been loaded!");
        ///             });
        ///         }
        ///     } catch (Exception e) {
        ///         Debug.LogFormat("Unable to load mymodel.fbx. The loader returned: {0}", e);
        ///     }
        /// }
        /// @endcode
        /// </example>
#if (NET_4_6 || NETFX_CORE)
        public Task LoadFromMemory(byte[] fileBytes, string filename, AssetLoaderOptions options,
                                   GameObject wrapperGameObject, ObjectLoadedHandle onAssetLoaded, string basePath = null, AssimpInterop.DataCallback dataCallback = null, AssimpInterop.ExistsCallback existsCallback = null, LoadTextureDataCallback loadTextureDataCallback = null)
        public Thread LoadFromMemoryWithTextures(byte[] fileData, string assetExtension, AssetLoaderOptions options, GameObject wrapperGameObject, ObjectLoadedHandle onAssetLoaded, string basePath = null, AssimpInterop.DataCallback dataCallback = null, AssimpInterop.ExistsCallback existsCallback = null, AssimpInterop.ProgressCallback progressCallback = null)
#endif
        {
            if (basePath == null)
            {
                basePath = FileUtils.GetFileDirectory(assetExtension);
            }
            var usesWrapperGameObject = wrapperGameObject != null;

            return(ThreadUtils.RunThread(delegate
            {
                InternalLoadFromMemoryAndZip(fileData, assetExtension, basePath, options, usesWrapperGameObject, dataCallback, existsCallback, null, progressCallback);
            },
                                         delegate
            {
                var loadedGameObject = BuildGameObject(options, assetExtension, wrapperGameObject);
                if (onAssetLoaded != null)
                {
                    onAssetLoaded(loadedGameObject);
                }
                ReleaseImport();
            }
                                         ));
        }
        public Thread LoadFromMemoryWithTextures(byte[] fileData, string assetExtension, AssetLoaderOptions options, GameObject wrapperGameObject, ObjectLoadedHandle onAssetLoaded, string basePath = null)
#endif
        {
            var usesWrapperGameObject = wrapperGameObject != null;

            return(ThreadUtils.RunThread(delegate
            {
                InternalLoadFromMemoryAndZip(fileData, assetExtension, basePath, options, usesWrapperGameObject);
            },
                                         delegate
            {
                var loadedGameObject = BuildGameObject(options, assetExtension, wrapperGameObject);
                if (onAssetLoaded != null)
                {
                    onAssetLoaded(loadedGameObject);
                }
            }
                                         ));
        }
        /// <summary>
        /// Asynchronously loads a <see cref="UnityEngine.GameObject"/> from input byte array (Accept ZIP files).
        /// </summary>
        /// <param name="fileData">File data.</param>
        /// <param name="assetExtension">Asset extension.</param>
        /// <param name="options"><see cref="AssetLoaderOptions"/> used to load the object.</param>
        /// <param name="wrapperGameObject">Use this field to load the new <see cref="UnityEngine.GameObject"/> into referenced <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="onAssetLoaded">The action that will be executed when the <see cref="UnityEngine.GameObject"/> be loaded</param>
        /// <param name="basePath">Base path from the loaded file.</param>
        /// <returns>The created Thread on NET 2.0, otherwise returns the created Task.</returns>
#if (NET_4_6 || NETFX_CORE)
        public Task LoadFromMemoryWithTextures(byte[] fileData, string assetExtension, AssetLoaderOptions options, GameObject wrapperGameObject, ObjectLoadedHandle onAssetLoaded, string basePath = null)
        /// <summary>
        /// Asynchronously loads a <see cref="UnityEngine.GameObject"/> from file (Accept ZIP files).
        /// </summary>
        /// <param name="filename">Filename used to load the <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="options"><see cref="AssetLoaderOptions"/> used to load the object.</param>
        /// <param name="wrapperGameObject">Use this field to load the new <see cref="UnityEngine.GameObject"/> into referenced <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="onAssetLoaded">The action that will be executed when the <see cref="UnityEngine.GameObject"/> be loaded</param>
        /// <param name="basePath">Base path from the loaded file.</param>
        /// <returns>The created Thread on NET 2.0, otherwise returns the created Task.</returns>
#if (NET_4_6 || NETFX_CORE)
        public Task LoadFromFileWithTextures(string filename, string assetExtension, AssetLoaderOptions options, GameObject wrapperGameObject, ObjectLoadedHandle onAssetLoaded, string basePath = null)
        /// <summary>
        /// Loads a <see cref="UnityEngine.GameObject"/> from browser files.
        /// </summary>
        /// <param name="filesCount">Browser files count.</param>
        /// <param name="options"><see cref="AssetLoaderOptions"/> used to load the object.</param>
        /// <param name="wrapperGameObject">Use this field to load the new <see cref="UnityEngine.GameObject"/> into referenced <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="onAssetLoaded">The action that will be executed when the <see cref="UnityEngine.GameObject"/> be loaded</param>
        /// <param name="dataCallback">Custom resource data retrieval callback. Pass this parameter when you need to load external data while loading from memory.</param>
        /// <param name="existsCallback">Custom resource size retrieval callback. Pass this parameter when you need to load external data while loading from memory.</param>
        /// <param name="progressCallback">Callback used to retrieve file loading percentage.</param>
        /// <returns>The created Thread on NET 2.0, otherwise returns the created Task.</returns>
#if !UNITY_EDITOR && (NETFX_CORE || NET_4_6 || NET_STANDARD_2_0) && !ENABLE_IL2CPP && !ENABLE_MONO
        public Task LoadFromMemoryWithTextures(int filesCount, AssetLoaderOptions options, GameObject wrapperGameObject, ObjectLoadedHandle onAssetLoaded, AssimpInterop.DataCallback dataCallback = null, AssimpInterop.ExistsCallback existsCallback = null, AssimpInterop.ProgressCallback progressCallback = null)
Beispiel #22
0
        /// <summary>
        /// Internal asset download coroutine.
        /// </summary>
        /// <returns>The coroutine IEnumerator.</returns>
        /// <param name="assetUri">Asset URI.</param>
        /// <param name="assetExtension">Asset extension.</param>
        /// <param name="onAssetLoaded">On asset loaded event.</param>
        /// <param name="options">Asset loading options.</param>
        /// <param name="wrapperGameObject">Wrapper <see cref="UnityEngine.GameObject"/> to load the asset into.</param>
        /// <param name="progressCallback">Callback used to retrieve file loading percentage.</param>
        private IEnumerator DoDownloadAsset(string assetUri, string assetExtension, ObjectLoadedHandle onAssetLoaded,
                                            AssetLoaderOptions options = null, GameObject wrapperGameObject = null,
                                            AssimpInterop.ProgressCallback progressCallback = null)
        {
            _unityWebRequest         = UnityWebRequest.Get(assetUri);
            _unityWebRequest.timeout = Timeout;
            yield return(_unityWebRequest.SendWebRequest());

            if (string.IsNullOrEmpty(_unityWebRequest.error))
            {
                var data = _unityWebRequest.downloadHandler.data;
                try
                {
                    if (Async)
                    {
                        using (var assetLoaderAsync = new AssetLoaderAsync())
                        {
                            try
                            {
                                assetLoaderAsync.LoadFromMemoryWithTextures(data, assetExtension, options, wrapperGameObject, onAssetLoaded, null, null, null, progressCallback);
                            }
                            catch (Exception e)
                            {
                                _error = e.ToString();
                                if (onAssetLoaded != null)
                                {
                                    onAssetLoaded(null);
                                }
                            }
                        }
                    }
                    else
                    {
                        using (var assetLoader = new AssetLoader())
                        {
                            assetLoader.OnObjectLoaded += onAssetLoaded;
                            try
                            {
                                assetLoader.LoadFromMemoryWithTextures(data, assetExtension, options, wrapperGameObject, null, null, null, progressCallback);
                            }
                            catch (Exception e)
                            {
                                _error = e.ToString();
                                if (onAssetLoaded != null)
                                {
                                    onAssetLoaded(null);
                                }
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    _error = exception.ToString();
                    if (onAssetLoaded != null)
                    {
                        onAssetLoaded(null);
                    }
                }
            }
            else
            {
                _error = _unityWebRequest.error;
                if (onAssetLoaded != null)
                {
                    onAssetLoaded(null);
                }
            }
            _unityWebRequest.Dispose();
            _unityWebRequest = null;
        }
Beispiel #23
0
        /// <summary>
        /// Internal method that actually loads the <see cref="UnityEngine.GameObject"/>.
        /// </summary>
        /// <param name="filename">Filename used to load the <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="assetLoaderOptions"><see cref="AssetLoaderOptions"/> used to load the object.</param>
        /// <param name="wrapperGameObject">Use this field to load the new <see cref="UnityEngine.GameObject"/> into referenced <see cref="UnityEngine.GameObject"/>.</param>
        /// <param name="onAssetLoaded">The action that will be executed when the <see cref="UnityEngine.GameObject"/> be loaded.</param>
        /// <param name="async">Pass <c>true</c> to load the model asynchronously.</param>
        /// <param name="progressCallback">Callback used to retrieve file loading percentage.</param>
        /// <returns>Varies.</returns>
        private object LoadFileInternal(string filename, AssetLoaderOptions assetLoaderOptions = null, GameObject wrapperGameObject = null, ObjectLoadedHandle onAssetLoaded = null, bool async = false, AssimpInterop.ProgressCallback progressCallback = null)
        {
            //Gets the file extension
            var fileExtension = FileUtils.GetFileExtension(filename);

            //Checks if the URL is a ZIP file
            if (fileExtension == ".zip")
            {
#if TRILIB_USE_ZIP
                //Gets the filename hash
                var hash = GetSha256(filename);

                //Gets the folder where the file should be extracted or could be already extracted
                var localFilePath = string.Format("{0}/{1}", Application.persistentDataPath, hash);

                //If file hasn't been extracted yet, extracts all ZIP file data to the destination folder
                if (!Directory.Exists(localFilePath))
                {
                    using (var memoryStream = new MemoryStream(FileUtils.LoadFileData(filename)))
                    {
                        UnzipFromStream(memoryStream, localFilePath);
                    }
                }

                //Gets the first asset loadable by TriLib on the destination folder
                var assetPath = GetReadableAssetPath(localFilePath);
                if (assetPath == null)
                {
                    Debug.LogError("No TriLib readable file could be found on the given directory");
                    return(null);
                }

                //Loads the found asset
                if (async)
                {
                    return(AsyncLoadFileInternal(assetPath, assetLoaderOptions, wrapperGameObject, onAssetLoaded, progressCallback));
                }
                return(SyncLoadFileInternal(assetPath, assetLoaderOptions, wrapperGameObject, progressCallback));
#else
                throw new System.Exception("Please enable TriLib ZIP loading");
#endif
            }
            //If the URL is not a ZIP file, loads the file inside the folder
            if (async)
            {
                return(AsyncLoadFileInternal(filename, assetLoaderOptions, wrapperGameObject, onAssetLoaded, progressCallback));
            }
            return(SyncLoadFileInternal(filename, assetLoaderOptions, wrapperGameObject, progressCallback));
        }
Beispiel #24
0
        public Thread LoadFromFileWithTextures(string filename, AssetLoaderOptions options, GameObject wrapperGameObject, ObjectLoadedHandle onAssetLoaded, string basePath = null)
#endif
        {
            var extension = FileUtils.GetFileExtension(filename);

            if (basePath == null)
            {
                basePath = FileUtils.GetFileDirectory(filename);
            }
            var usesWrapperGameObject = wrapperGameObject != null;

            return(ThreadUtils.RunThread(delegate
            {
                var fileData = FileUtils.LoadFileData(filename);
                InternalLoadFromMemoryAndZip(fileData, extension, basePath, options, usesWrapperGameObject);
            },
                                         delegate
            {
                var loadedGameObject = BuildGameObject(options, extension, wrapperGameObject);
                if (onAssetLoaded != null)
                {
                    onAssetLoaded(loadedGameObject);
                }
            }
                                         ));
        }
Beispiel #25
0
 public Thread LoadFromFileAsync(string filename, AssetLoaderOptions options = null, GameObject wrapperGameObject                    = null,
                                 ObjectLoadedHandle onAssetLoaded            = null, AssimpInterop.ProgressCallback progressCallback = null)
 {
     return((Thread)LoadFileInternal(filename, options, wrapperGameObject, onAssetLoaded, true, progressCallback));
 }
        public Thread LoadFromBrowserFilesWithTextures(int filesCount, AssetLoaderOptions options, GameObject wrapperGameObject, ObjectLoadedHandle onAssetLoaded, AssimpInterop.DataCallback dataCallback = null, AssimpInterop.ExistsCallback existsCallback = null, AssimpInterop.ProgressCallback progressCallback = null)
#endif
        {
            byte[] fileData;
            string fileName;
            string fileExtension;

            if (!GetSupportedBrowserFileData(filesCount, out fileData, out fileName, out fileExtension))
            {
                return(null);
            }
            var usesWrapperGameObject = wrapperGameObject != null;

            return(ThreadUtils.RunThread(delegate
            {
                InternalLoadFromBrowserFiles(filesCount, fileData, fileName, fileExtension, options, usesWrapperGameObject, dataCallback, existsCallback, null, progressCallback);
            },
                                         delegate
            {
                var loadedGameObject = BuildGameObject(options, fileExtension, wrapperGameObject);
                if (onAssetLoaded != null)
                {
                    onAssetLoaded(loadedGameObject);
                }
                ReleaseImport();
            }
                                         ));
        }