MaterialUtil.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. }, async (next: NextFunction, params: any, args: any) => {
  67. assetManager.loadBundle("duplicate",
  68. // {onFileProgress: (finished: number, total: number)=>{
  69. // process.curSub = finished;
  70. // process.maxSub = total;
  71. // if(callback) callback(process);
  72. // }},
  73. (err, bundle)=>{
  74. if (!err) {
  75. process.curMain++
  76. if(callback) callback(process);
  77. next()
  78. console.log('游戏筛选图标加载完成:',queue.size);
  79. }
  80. });
  81. });
  82. process.maxMain = 3;
  83. queue.complete = () => {
  84. console.log('materialUtil初始化完成');
  85. };
  86. queue.play();
  87. }
  88. _loadRes(LoadType: AssetType, Url, Back) {
  89. resLoader.load('package', Url, LoadType, (error: Error, res) => {
  90. if (!error) {
  91. Back(res)
  92. }
  93. });
  94. }
  95. // /** 游戏入口图标 */
  96. // static getIconGameByID(id) {
  97. // return this.iconGame.spriteFrames[id];
  98. // }
  99. // /** 游戏筛选图标 */
  100. // static getIconFilterByName(name) {
  101. // return this.iconFilter.spriteFrames[name];
  102. // }
  103. /** 游戏入口图标 */
  104. static getHeadByID(id,sprite:Sprite,headType:HeadSpriteType = HeadSpriteType.Critical) {
  105. resLoader.load('package', 'texture/head/end_0_'+id+"/spriteFrame", SpriteFrame, (error: Error, res: SpriteFrame) => {
  106. if (!error && isValid(sprite)) {
  107. sprite.spriteFrame = res;
  108. }
  109. })
  110. //return this.iconGame.spriteFrames[id];
  111. }
  112. }