tools.ts 2.7 KB

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