UIUtils.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. export default class UIUtils {
  2. /***
  3. * 生成子节点的唯一标识快捷访问
  4. * @param node
  5. * @param map
  6. */
  7. public static createSubNodeMap(node: cc.Node, map: Map<string, cc.Node>) {
  8. let children = node.children;
  9. if (!children) {
  10. return;
  11. }
  12. for (let t = 0, len = children.length; t < len; ++t) {
  13. let subChild = children[t];
  14. map.set(subChild.name, subChild);
  15. UIUtils.createSubNodeMap(subChild, map);
  16. }
  17. }
  18. /***
  19. * 返回当前节点所有节点,一唯一标识存在
  20. * @param node 父节点
  21. * @return {Object} 所有子节点的映射map
  22. */
  23. public static seekAllSubView(node: cc.Node): UIContainer {
  24. let map = new Map<string, cc.Node>();
  25. UIUtils.createSubNodeMap(node, map);
  26. return new UIContainer(map);
  27. }
  28. }
  29. export class UIContainer {
  30. /** 所有节点集合 */
  31. private _uiNodesMap: Map<string, cc.Node>;
  32. public constructor(nodesMap: Map<string, cc.Node>) {
  33. this._uiNodesMap = nodesMap;
  34. }
  35. /**
  36. * 根据节点名字获取节点
  37. * @param {string}name 节点名字
  38. * @return {cc.Node}
  39. */
  40. public getNode(name: string): cc.Node {
  41. return this._uiNodesMap.get(name);
  42. }
  43. /**
  44. * 根据节点名字和组件类型获取组件对象
  45. * @param {string}name 节点名字
  46. * @param {{prototype: cc.Component}}com 组建类型
  47. * @return {cc.Component}
  48. */
  49. public getComponent<T extends cc.Component>(name: string, com: { prototype: T }): T {
  50. let node = this._uiNodesMap.get(name);
  51. if (node) {
  52. return node.getComponent(com);
  53. }
  54. return null;
  55. }
  56. }