import { _decorator, Asset, assetManager, ImageAsset, Node, Sprite, SpriteFrame, Texture2D } from 'cc'; import { ch } from '../../ch/ch'; export class HeadIconMgr { private _map:Map=new Map(); private _map_uid:Map=new Map(); public init():void{ registerHeadImgLoader(); } /** */ public async showIcon(uid:string,remoteUrl:string,sp:Sprite=null,cb:Function=null,cb_target:any=null){ if(this._map.has(uid)){ const spr=this._map.get(uid); if(sp)sp.spriteFrame=spr; if(cb)cb.call(cb_target,spr); }else{ const map=this._map; const map_uid=this._map_uid; if(sp && sp.isValid ) map_uid.set(sp,uid); // const ret = await loadRemote({url:remoteUrl, option:{ext:'.jpg'}}); // const ret = await ch.sdk.loadImage(remoteUrl) const spriteFrame =await ch.util.loadImage(remoteUrl) ; // const texture = new Texture2D(); // texture.image = ret as ImageAsset; // spriteFrame.texture = texture; if(sp && sp.isValid )if(map_uid.get(sp)==uid)sp.spriteFrame=spriteFrame; cb?.call(cb_target,spriteFrame); map.set(uid,spriteFrame); /*assetManager.loadRemote(remoteUrl,{ext: '.jpg'},function (err, imageAsset) { if(err){ console.warn(remoteUrl,"无效的图片地址",err); return; } const spriteFrame = new SpriteFrame(); const texture = new Texture2D(); texture.image = imageAsset; spriteFrame.texture = texture; if(sp && sp.isValid )if(map_uid.get(sp)==uid)sp.spriteFrame=spriteFrame; cb?.call(cb_target,spriteFrame); map.set(uid,spriteFrame); });*/ } } public clean():void{ this._map.clear(); this._map_uid.clear(); } } type Constructor = new () => T; const headImgExt = ".head"; /** *@description 原生加载资源 * @param object {url: 远程地址 option: 参数类型} * @returns */ function loadRemote(object: { url: string, option?: any }) { if (null == object.option) { object.option = {}; } const { url, option } = object; return new Promise((resolve, reject) => { assetManager.loadRemote(url, option, (err: Error | null, asset: T) => { resolve && resolve(err ? null : asset); }); }); } /** * 自定义头像加载流程 * 加载头像使用 loadRemote({url, option:{ext:headImgExt}}) */ function registerHeadImgLoader() { assetManager.downloader.register(headImgExt, (content, options, onComplete) => { onComplete(null, content); }); assetManager.parser.register(headImgExt, downloadDomImage); assetManager.factory.register(headImgExt, createTexture); } function createTexture(id: string, data: any, options: any, onComplete: Function) { let out: Texture2D | null = null; let err: Error | null = null; try { out = new Texture2D(); const imageAsset = new ImageAsset(data); out.image = imageAsset; } catch (e) { err = e as any as Error; } onComplete && onComplete(err, out); } function downloadDomImage(url: string, options: any, onComplete: Function) { const img = new Image(); if (window.location.protocol !== 'file:') { img.crossOrigin = 'anonymous'; } function loadCallback() { img.removeEventListener('load', loadCallback); img.removeEventListener('error', errorCallback); if (onComplete) { onComplete(null, img); } } function errorCallback() { img.removeEventListener('load', loadCallback); img.removeEventListener('error', errorCallback); if (onComplete) { onComplete(new Error(url)); } } img.addEventListener('load', loadCallback); img.addEventListener('error', errorCallback); img.src = url; return img; }