Exemplo n.º 1
0
        /// <summary>
        /// 异步加载Asset:协程形式(需要手动释放LoadHandle)
        /// </summary>
        /// <param name="assetPath">资源路径</param>
        /// <param name="catchAsset">是否缓存</param>
        /// <returns></returns>
        public BaseAssetAsyncLoadHandle CoLoadAssetAsync(string assetPath, Action <BaseAssetAsyncLoadHandle> callback = null, bool catchAsset = true)
        {
            if (string.IsNullOrEmpty(assetPath))
            {
                Debug.LogError("CoLoadAssetAsync Error : AssetPath is null or empty!");
                return(null);
            }

            if (!IsRuntime())
            {
                var    editorLoader = new EditorAssetAsyncLoadHandle();
                Object asset        = EditorLoadAsset <Object>(assetPath);
                editorLoader.Init(assetPath, asset, callback, false);
                return(editorLoader);
            }
            else
            {
                var tempLoader = AssetBundleManager.Instance.LoadAssetAsync(assetPath, typeof(Object), callback, false, catchAsset);
                return(tempLoader);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 异步加载Asset:回调形式
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="assetPath">资源路径</param>
        /// <param name="callback">请求回调</param>
        /// <param name="catchAsset">是否缓存</param>
        /// <returns></returns>
        public BaseAssetAsyncLoadHandle LoadAssetAsync <T>(string assetPath, Action <T> callback, bool catchAsset = true) where T : Object
        {
            if (string.IsNullOrEmpty(assetPath))
            {
                Debug.LogError("LoadAssetAsync Error :AssetPath is null !");
                return(null);
            }

            Action <BaseAssetAsyncLoadHandle> handleCallback = (BaseAssetAsyncLoadHandle loader) =>
            {
                if (loader != null)
                {
                    T obj = loader.asset as T;
                    if (callback != null)
                    {
                        callback.Invoke(obj);
                    }
                }
                else
                {
                    if (callback != null)
                    {
                        callback.Invoke(null);
                    }
                }
            };

            if (!IsRuntime())
            {
                var editorLoader = new EditorAssetAsyncLoadHandle();
                T   asset        = EditorLoadAsset <T>(assetPath);
                editorLoader.Init(assetPath, asset, handleCallback, true);
                return(editorLoader);
            }
            else
            {
                var tempLoader = AssetBundleManager.Instance.LoadAssetAsync(assetPath, typeof(T), handleCallback, true, catchAsset);
                return(tempLoader);
            }
        }