tools.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { _decorator, assetManager, Component, ImageAsset, instantiate, Node, Prefab, resources, Sprite, SpriteFrame, Texture2D } from 'cc';
  2. import { main } from './main';
  3. import { toast } from './toast';
  4. import { gameManager } from './run/gameManager';
  5. import { config } from './config';
  6. export class tools {
  7. public static platform = config.Platform.BROWSER //平台类型
  8. public static show_dialog(title="是否确定?",sure_callback,cancel_callback=null){
  9. // resources.load("prefab/dialog_view",Prefab,(err, prefab)=>{
  10. // let node = instantiate(prefab);
  11. // node.parent = main.g_canvas.node;
  12. // node.getComponent(dialog_view).show(title,sure_callback,cancel_callback)
  13. // })
  14. }
  15. public static showToast(text:string, isShow:boolean=false){
  16. if(isShow==false) {
  17. return
  18. }
  19. resources.load("prefab/run/toast",Prefab,(err, prefab)=>{
  20. let node = instantiate(prefab);
  21. // node.parent = main.g_canvas.node;
  22. node.parent = gameManager.Singleton.getTopFloorLayer()
  23. node.getComponent(toast).show(text)
  24. })
  25. }
  26. public static loadUrl(url:string,spr:Sprite){
  27. if(url.length<=0){
  28. return null;
  29. }
  30. assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
  31. if (!err && imageAsset2) {
  32. const texture = new Texture2D();
  33. texture.image = imageAsset2;
  34. let spFrame2 = new SpriteFrame();
  35. spFrame2.texture = texture;
  36. spr.spriteFrame = spFrame2;
  37. }
  38. });
  39. }
  40. public static getLocalTime(n){
  41. return new Date(parseInt(n)).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
  42. }
  43. public static loadSceneImg(url:string,call){
  44. if(url.length<=0){
  45. return null;
  46. }
  47. assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
  48. if (!err && imageAsset2) {
  49. const texture = new Texture2D();
  50. texture.image = imageAsset2;
  51. let spFrame2 = new SpriteFrame();
  52. spFrame2.texture = texture;
  53. spFrame2.addRef()
  54. call({"url":url,"sf":spFrame2})
  55. }
  56. });
  57. }
  58. public static loadSceneMp3(url:string,call){
  59. if(url.length<=0){
  60. return null;
  61. }
  62. assetManager.loadRemote(url, (err: any, clip: any)=> {
  63. clip.addRef()
  64. if (!err && clip) {
  65. call({"url":url,"clip":clip})
  66. }
  67. });
  68. }
  69. // 去除左边和右边空格
  70. public static trimLeftAndRightblank(str: string):string {
  71. const reg = /^\s+|\s+$/g;
  72. return str.replace(reg, '')
  73. }
  74. }