HeadIconMgr.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { _decorator, Asset, assetManager, ImageAsset, Node, Sprite, SpriteFrame, Texture2D } from 'cc';
  2. import { ch } from '../../ch/ch';
  3. export class HeadIconMgr {
  4. private _map:Map<string,SpriteFrame>=new Map();
  5. private _map_uid:Map<Sprite,string>=new Map();
  6. public init():void{
  7. registerHeadImgLoader();
  8. }
  9. /** */
  10. public async showIcon(uid:string,remoteUrl:string,sp:Sprite=null,cb:Function=null,cb_target:any=null){
  11. if(this._map.has(uid)){
  12. const spr=this._map.get(uid);
  13. if(sp)sp.spriteFrame=spr;
  14. if(cb)cb.call(cb_target,spr);
  15. }else{
  16. const map=this._map;
  17. const map_uid=this._map_uid;
  18. if(sp && sp.isValid ) map_uid.set(sp,uid);
  19. // const ret = await loadRemote({url:remoteUrl, option:{ext:'.jpg'}});
  20. // const ret = await ch.sdk.loadImage(remoteUrl)
  21. const spriteFrame =await ch.util.loadImage(remoteUrl) ;
  22. // const texture = new Texture2D();
  23. // texture.image = ret as ImageAsset;
  24. // spriteFrame.texture = texture;
  25. if(sp && sp.isValid )if(map_uid.get(sp)==uid)sp.spriteFrame=spriteFrame;
  26. cb?.call(cb_target,spriteFrame);
  27. map.set(uid,spriteFrame);
  28. /*assetManager.loadRemote<ImageAsset>(remoteUrl,{ext: '.jpg'},function (err, imageAsset) {
  29. if(err){
  30. console.warn(remoteUrl,"无效的图片地址",err);
  31. return;
  32. }
  33. const spriteFrame = new SpriteFrame();
  34. const texture = new Texture2D();
  35. texture.image = imageAsset;
  36. spriteFrame.texture = texture;
  37. if(sp && sp.isValid )if(map_uid.get(sp)==uid)sp.spriteFrame=spriteFrame;
  38. cb?.call(cb_target,spriteFrame);
  39. map.set(uid,spriteFrame);
  40. });*/
  41. }
  42. }
  43. public clean():void{
  44. this._map.clear();
  45. this._map_uid.clear();
  46. }
  47. }
  48. type Constructor<T extends Asset> = new () => T;
  49. const headImgExt = ".head";
  50. /**
  51. *@description 原生加载资源
  52. * @param object {url: 远程地址 option: 参数类型}
  53. * @returns
  54. */
  55. function loadRemote<T extends Asset>(object: { url: string, option?: any }) {
  56. if (null == object.option) {
  57. object.option = {};
  58. }
  59. const { url, option } = object;
  60. return new Promise((resolve, reject) => {
  61. assetManager.loadRemote(url, option, (err: Error | null, asset: T) => {
  62. resolve && resolve(err ? null : asset);
  63. });
  64. });
  65. }
  66. /**
  67. * 自定义头像加载流程
  68. * 加载头像使用 loadRemote({url, option:{ext:headImgExt}})
  69. */
  70. function registerHeadImgLoader() {
  71. assetManager.downloader.register(headImgExt, (content, options, onComplete) => {
  72. onComplete(null, content);
  73. });
  74. assetManager.parser.register(headImgExt, downloadDomImage);
  75. assetManager.factory.register(headImgExt, createTexture);
  76. }
  77. function createTexture(id: string, data: any, options: any, onComplete: Function) {
  78. let out: Texture2D | null = null;
  79. let err: Error | null = null;
  80. try {
  81. out = new Texture2D();
  82. const imageAsset = new ImageAsset(data);
  83. out.image = imageAsset;
  84. } catch (e) {
  85. err = e as any as Error;
  86. }
  87. onComplete && onComplete(err, out);
  88. }
  89. function downloadDomImage(url: string, options: any, onComplete: Function) {
  90. const img = new Image();
  91. if (window.location.protocol !== 'file:') {
  92. img.crossOrigin = 'anonymous';
  93. }
  94. function loadCallback() {
  95. img.removeEventListener('load', loadCallback);
  96. img.removeEventListener('error', errorCallback);
  97. if (onComplete) {
  98. onComplete(null, img);
  99. }
  100. }
  101. function errorCallback() {
  102. img.removeEventListener('load', loadCallback);
  103. img.removeEventListener('error', errorCallback);
  104. if (onComplete) {
  105. onComplete(new Error(url));
  106. }
  107. }
  108. img.addEventListener('load', loadCallback);
  109. img.addEventListener('error', errorCallback);
  110. img.src = url;
  111. return img;
  112. }