LayerManager.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { _decorator, Component, Node } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. export class LayerManager{
  4. /**
  5. * 层次根节点
  6. */
  7. private root:Node;
  8. /**
  9. * 层次
  10. */
  11. private layers:Node[];
  12. constructor(){
  13. }
  14. /**
  15. * 初始化
  16. * @param root
  17. * @param layerCount
  18. */
  19. Init(root:Node):void{
  20. this.root=root;
  21. let layer:Node;
  22. this.layers=[];
  23. for (let index = 0; index < this.root.children.length; index++) {
  24. this.layers[index]= this.root.children[index];
  25. }
  26. }
  27. /**
  28. * 添加内容到层
  29. * @param layerIndex
  30. * @param node
  31. */
  32. AddToLayer(layerIndex:number,node:Node):void{
  33. let layer:Node=this.layers[layerIndex];
  34. if(layer==null){
  35. throw new Error("找不到:"+layerIndex+"层!");
  36. }
  37. layer.addChild(node);
  38. }
  39. /**
  40. * 从层上删除内容
  41. * @param layerIndex
  42. * @param node
  43. */
  44. RemoveFormeLayer(layerIndex:number,node:Node):void{
  45. let layer:Node=this.layers[layerIndex];
  46. if(layer==null){
  47. throw new Error("找不到:"+layerIndex+"层!");
  48. }
  49. layer.removeChild(node);
  50. }
  51. /**
  52. * 通过层索引获取层
  53. * @param layerIndex
  54. */
  55. GetLayer(layerIndex:number){
  56. let layer:Node=this.layers[layerIndex];
  57. if(layer==null){
  58. throw new Error("找不到:"+layerIndex+"层!");
  59. }
  60. return layer;
  61. }
  62. /**
  63. * 单例
  64. */
  65. private static instance:LayerManager;
  66. public static get single():LayerManager{
  67. if(this.instance==null){
  68. this.instance=new LayerManager();
  69. }
  70. return this.instance;
  71. }
  72. }