Beispiel #1
0
        /// <summary>
        /// Load the assets directly from the asset folder, noted that it can operate in editor only.
        /// **SHOULD BE ASYNCHRONOUS FUNCTION THEORETICALLY BUT NOT SUPPORTED IN PERSONAL VERSION**
        /// </summary>
        public static LocalAssetsLoader LoadAssetsInEditor(string assetBundleName, string assetName, System.Type type, Action <Exception> errorHandler)
        {
            LocalAssetsLoader loader;

            string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(assetBundleName, assetName);
            if (assetPaths.Length == 0)
            {
                errorHandler(new Exception("Asset Not Found!"));
            }
            // get the first asset we found
            loader = new LocalAssetsLoader(assetPaths[0], type);
            return(loader);
        }
Beispiel #2
0
        /// <summary>
        /// Loads the assets from specific path or assets folder.
        /// Noted that assets bundle should be downloaded and be stored before calling this function.
        /// </summary>
        public static IEnumerator LoadAssets <T>(string assetBundleName, string assetName, string filePath, Action <float> progressFunction, Action <T> completeFunction, Action <Exception> errorHandler) where T : UnityEngine.Object
        {
                        #if UNITY_EDITOR
            if (loadFromAssetFolder)
            {
                LocalAssetsLoader loader = LoadAssetsInEditor(assetBundleName, assetName, typeof(T), errorHandler);
                while (!loader.isDone)
                {
                    yield return(null);
                }
                if (loader.isDone)
                {
                    completeFunction(loader.GetAsset <T>());
                }
            }
            else
            {
                        #endif
            // ensure no assetbundle is being loaded at the moment
            while (loadingAssetBundle)
            {
                // wait until asset bundle is being loaded
                yield return(new WaitForSecondsRealtime(0.1f));
            }
            AssetBundle assetBundle = assetBundles.Find(x => x.name.Equals(assetBundleName));
            if (assetBundle == null)
            {
                yield return(LoadAssetsFromFolderAsync(filePath + assetBundleName, progressFunction, errorHandler));

                while (loadingAssetBundle)
                {
                    // wait until asset bundle is being loaded
                    yield return(new WaitForSecondsRealtime(0.1f));
                }
            }
            AssetBundleRequest request;
            request = assetBundle.LoadAssetAsync(assetName, typeof(T));
            while (!request.isDone)
            {
                yield return(null);
            }
            if (request.isDone)
            {
                completeFunction((T)request.asset);
            }
                        #if UNITY_EDITOR
        }
                        #endif
        }