| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- import { _decorator, Asset, assetManager, ImageAsset, Node, Sprite, SpriteFrame, Texture2D } from 'cc';
- import { ch } from '../../ch/ch';
- export class HeadIconMgr {
- private _map:Map<string,SpriteFrame>=new Map();
- private _map_uid:Map<Sprite,string>=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<ImageAsset>(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<T extends Asset> = new () => T;
- const headImgExt = ".head";
- /**
- *@description 原生加载资源
- * @param object {url: 远程地址 option: 参数类型}
- * @returns
- */
- function loadRemote<T extends Asset>(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;
- }
|