import { ResourceData , ResourceCacheData , ResourceInfo } from "../base/Defines" ; import { Manager } from "../Framework" ; export enum ResourceLoaderError { description LOADING , description NO_FOUND_LOAD_RESOURCE , description SUCCESS } description export default class ResourceLoader { description private _resources : Map < string , ResourceData > = new Map < string , ResourceData > ( ) ; description private _loadedCount : number = 0 ; description private _loadedResource : Map < string , ResourceInfo > = new Map < string , ResourceInfo > ( ) ; description private _isLoading : boolean = false ; description private _tag : string = null ; public get tag ( ) { return this . _tag ; } public set tag ( tag ) { this . _tag = tag ; } description private _onLoadComplete : ( error : ResourceLoaderError ) => void = null ; public set onLoadComplete ( cb ) { this . _onLoadComplete = cb ; } public get onLoadComplete ( ) { return this . _onLoadComplete ; } description public _onLoadProgress : ( loadedCount : number , toatl : number , data : ResourceCacheData ) => void ; public set onLoadProgress ( value ) { this . _onLoadProgress = value ; } public get onLoadProgress ( ) { return this . _onLoadProgress ; } description private _getLoadResource : ( ) => ResourceData [ ] = null ; public set getLoadResources ( func ) { this . _getLoadResource = func ; } public get getLoadResources ( ) : ( ) => ResourceData [ ] { return this . _getLoadResource ; } description public loadResources ( ) { if ( ! this . getLoadResources ) { if ( CC_DEBUG ) cc . error ( "未指定 getLoadResources 函数" ) ; this . onLoadComplete && this . onLoadComplete ( ResourceLoaderError . NO_FOUND_LOAD_RESOURCE ) ; return ; } let res = this . getLoadResources ( ) ; if ( ! res ) { if ( CC_DEBUG ) cc . error ( `未指定加载资源` ) ; this . onLoadComplete && this . onLoadComplete ( ResourceLoaderError . NO_FOUND_LOAD_RESOURCE ) ; return ; } if ( res . length <= 0 ) { if ( CC_DEBUG ) cc . warn ( `加载的资源为空` ) ; this . onLoadComplete && this . onLoadComplete ( ResourceLoaderError . NO_FOUND_LOAD_RESOURCE ) ; return ; } if ( this . _isLoading ) { if ( CC_DEBUG ) cc . warn ( `资源加载中，未完成加载` ) ; this . onLoadComplete && this . onLoadComplete ( ResourceLoaderError . LOADING ) ; return ; } if ( this . _resources . size > 0 && this . isLoadComplete ( ) ) { if ( CC_DEBUG ) cc . warn ( `资源已经加载完成，使用已经加载完成的资源` ) ; this . onLoadComplete && this . onLoadComplete ( ResourceLoaderError . SUCCESS ) ; this . onLoadResourceComplete ( ) ; return ; } this . _isLoading = true ; res . forEach ( ( value , index ) => { if ( value . url ) { this . _resources . set ( value . url , value ) ; } else { if ( value . preloadView ) this . _resources . set ( value . preloadView . getPrefabUrl ( ) , value ) ; } } ) ; this . _loadedCount = 0 ; this . _resources . forEach ( ( value , key , source ) => { if ( value . preloadView ) { Manager . uiManager . preload ( value . preloadView , value . bundle ) . then ( ( view ) => { let cache = new ResourceCacheData ( ) ; cache . isLoaded = true ; cache . data = < any > view ; cache . url = value . preloadView . getPrefabUrl ( ) ; cache . bundle = value . bundle ; this . _onLoadResourceComplete ( cache ) ; } ) ; } else { Manager . assetManager . load ( value . bundle , value . url , value . type , null , this . _onLoadResourceComplete . bind ( this ) ) ; } } ) ; } description public unLoadResources ( ) { this . _unLoadResources ( ) ; } private _unLoadResources ( ) { if ( this . _isLoading || this . _resources . size <= 0 ) { if ( this . _isLoading ) { cc . log ( "resources is loading , waiting for unload!!!" ) ; } return ; } if ( this . _resources . size > 0 ) { this . _resources . forEach ( ( value ) => { if ( value . url ) { if ( this . _loadedResource . has ( value . url ) ) { let data = this . _loadedResource . get ( value . url ) ; if ( data ) { Manager . assetManager . releaseAsset ( data ) ; } this . _loadedResource . delete ( value . url ) ; } } } ) ; } this . _isLoading = false ; this . _loadedCount = 0 ; this . _resources . clear ( ) ; } private _onLoadResourceComplete ( data ) { this . _loadedCount ++ ; if ( this . _onLoadProgress ) { if ( this . _loadedCount > this . _resources . size ) { this . _loadedCount = this . _resources . size ; } this . _onLoadProgress ( this . _loadedCount , this . _resources . size , data ) ; } if ( data && data . data instanceof cc . Asset ) { let info = new ResourceInfo ; info . url = data . url ; info . type = data . assetType ; info . data = data . data ; info . bundle = data . bundle ; this . _loadedResource . set ( info . url , info ) ; } this . checkLoadResourceComplete ( ) ; } description protected checkLoadResourceComplete ( ) { if ( this . isLoadComplete ( ) ) { this . _isLoading = false ; this . onLoadComplete && this . onLoadComplete ( ResourceLoaderError . SUCCESS ) ; this . onLoadResourceComplete ( ) ; } } description protected onLoadResourceComplete ( ) { } public isLoadComplete ( ) { return this . _loadedCount >= this . _resources . size ; } }