MaterialUtil.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { assetManager, isValid, Sprite, SpriteAtlas, SpriteFrame } from "cc"
  2. import { AssetType, resLoader } from "../res/ResLoader"
  3. import { AsyncQueue, NextFunction } from "../queue/AsyncQueue";
  4. //头像类型
  5. export enum HeadSpriteType{
  6. //圆形
  7. Critical = 0,
  8. //方块
  9. Square,
  10. //圆角
  11. Round,
  12. }
  13. //进度数据
  14. export interface ProcessData{
  15. curMain: number;
  16. maxMain: number;
  17. curSub: number;
  18. maxSub: number;
  19. }
  20. export class MaterialUtil {
  21. static iconGame: SpriteAtlas = null;
  22. static iconFilter: SpriteAtlas = null;
  23. //头像
  24. static iconHead: SpriteFrame = null;
  25. static Restart() {
  26. }
  27. static init(callback) {
  28. let queue = new AsyncQueue();
  29. let process:ProcessData = {
  30. curMain: 0,
  31. maxMain: 0,
  32. curSub: 0,
  33. maxSub: 0
  34. }
  35. queue.pushMulti("InitPool", async (next: NextFunction, params: any, args: any) => {
  36. resLoader.load('package', 'texture/head/end_0_1/spriteFrame', SpriteFrame,
  37. // (finished: number, total: number)=>{
  38. // process.curSub = finished;
  39. // process.maxSub = total;
  40. // if(callback) callback(process);
  41. // },
  42. (error: Error, res: SpriteFrame) => {
  43. if (!error) {
  44. this.iconHead = res;
  45. process.curMain++
  46. if(callback) callback(process);
  47. next()
  48. console.log('头像图标加载完成');
  49. }
  50. });
  51. }, async (next: NextFunction, params: any, args: any) => {
  52. assetManager.loadBundle("common",
  53. // {onFileProgress: (finished: number, total: number)=>{
  54. // process.curSub = finished;
  55. // process.maxSub = total;
  56. // if(callback) callback(process);
  57. // }},
  58. (err, bundle)=>{
  59. if (!err) {
  60. process.curMain++
  61. if(callback) callback(process);
  62. next()
  63. console.log('游戏筛选图标加载完成:',queue.size);
  64. }
  65. });
  66. });
  67. process.maxMain = 2;
  68. queue.complete = () => {
  69. console.log('materialUtil初始化完成');
  70. };
  71. queue.play();
  72. }
  73. _loadRes(LoadType: AssetType, Url, Back) {
  74. resLoader.load('package', Url, LoadType, (error: Error, res) => {
  75. if (!error) {
  76. Back(res)
  77. }
  78. });
  79. }
  80. // /** 游戏入口图标 */
  81. // static getIconGameByID(id) {
  82. // return this.iconGame.spriteFrames[id];
  83. // }
  84. // /** 游戏筛选图标 */
  85. // static getIconFilterByName(name) {
  86. // return this.iconFilter.spriteFrames[name];
  87. // }
  88. /** 游戏入口图标 */
  89. static getHeadByID(id,sprite:Sprite,headType:HeadSpriteType = HeadSpriteType.Critical) {
  90. resLoader.load('package', 'texture/head/end_0_'+id+"/spriteFrame", SpriteFrame, (error: Error, res: SpriteFrame) => {
  91. if (!error && isValid(sprite)) {
  92. sprite.spriteFrame = res;
  93. }
  94. })
  95. //return this.iconGame.spriteFrames[id];
  96. }
  97. }