UIManager.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { _decorator, Component, instantiate, Node, Prefab, Size, tween, UIOpacity, UITransform, Vec2, Vec3 } from 'cc';
  2. import { Singleton } from '../Tools/Singleton';
  3. import { BasePanel } from './BasePanel';
  4. import { SettingsPanel } from './SettingsPanel';
  5. import { RankPanel } from './RankPanel';
  6. import { InforPanel } from './InforPanel';
  7. const { ccclass, property } = _decorator;
  8. //大厅和游戏内公用一套方案
  9. @ccclass('UIManager')
  10. export class UIManager extends Component {
  11. public static Instance: UIManager;
  12. //管理不同UI界面,根据名字查找节点
  13. private UI_Map: Map<string, Node> = new Map();
  14. //使用栈保证仅有顶层UI能被触摸
  15. private UIPanel_Stack: Node[] = [];
  16. onLoad() {
  17. UIManager.Instance = this;
  18. }
  19. //显示UI
  20. public ShowUI(uiName: string, prefab: Prefab) {
  21. let uiNode = this.UI_Map.get(uiName);
  22. if (!uiNode) {
  23. //加载UI
  24. console.log(this.node);
  25. console.log("实例化设置界面");
  26. uiNode = instantiate(prefab);
  27. uiNode.setParent(this.node);
  28. this.UI_Map.set(uiName, uiNode);
  29. console.log(uiNode.name, uiNode.position, uiNode.parent); // 打印节点信息
  30. }
  31. uiNode.active = true;
  32. let opacity = uiNode.getComponent(UIOpacity);
  33. opacity.opacity = 0;
  34. tween(opacity).to(0.5, { opacity: 255 }, { easing: 'fade' }).start();
  35. let scaleUp = tween(uiNode).to(0.4, { scale: new Vec3(1.02, 1.02, 1.02) }, { easing: 'quadOut' });
  36. let scaleDown = tween(uiNode).to(0.1, { scale: new Vec3(1.0, 1.0, 1.0) }, { easing: 'quadOut' });
  37. tween(uiNode).sequence(scaleUp, scaleDown).start();
  38. this.pushUIPanel(uiNode);
  39. let basePanel = uiNode.getComponent(BasePanel);
  40. if (basePanel) {
  41. basePanel.onEnter();
  42. }
  43. }
  44. //隐藏UI
  45. public hideUI(callback) {
  46. let uiNode = this.UIPanel_Stack[this.UIPanel_Stack.length - 1];
  47. let opacity = uiNode.getComponent(UIOpacity);
  48. tween(opacity).to(0.3, { opacity: 0 }, { easing: 'fade' }).call(() => {
  49. this.popUIPanel();
  50. let basePanel = uiNode.getComponent(BasePanel);
  51. if (basePanel) {
  52. basePanel.onExit();
  53. }
  54. if(callback!==null)
  55. callback();
  56. }).start();
  57. }
  58. // 将UI面板压入栈中
  59. private pushUIPanel(uiNode: Node): void {
  60. this.UIPanel_Stack.push(uiNode);
  61. this.updateUIPanelStack();
  62. }
  63. // 从栈中弹出UI面板
  64. private popUIPanel(): void {
  65. if (this.UIPanel_Stack.length > 0) {
  66. const topUI = this.UIPanel_Stack.pop();
  67. if (topUI) {
  68. topUI.active = false;
  69. }
  70. this.updateUIPanelStack();
  71. }
  72. }
  73. // 更新UI面板栈
  74. private updateUIPanelStack(): void {
  75. for (let i = 0; i < this.UIPanel_Stack.length; i++) {
  76. const uiNode = this.UIPanel_Stack[i];
  77. uiNode.active = i === this.UIPanel_Stack.length - 1;//保证栈顶为可活动
  78. }
  79. }
  80. //清除栈
  81. public clearStack() {
  82. this.UIPanel_Stack = [];
  83. }
  84. }