UIOpenAndClose.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const { ccclass, property, executeInEditMode, playOnFocus } = cc._decorator;
  2. /** 效果类型 */
  3. enum EffectType {
  4. none = 0, //无
  5. scale = 1, //缩放
  6. scaleToPoint = 2, //缩放到某点
  7. centerUnfolding = 3, //从中部展开
  8. }
  9. /**
  10. * 界面打开 和 关闭效果
  11. * 组件请挂载到对应ui界面上
  12. * @author 薛鸿潇
  13. */
  14. @ccclass
  15. @executeInEditMode()
  16. @playOnFocus()
  17. export default class UIOpenAndClose extends cc.Component {
  18. @property({ type: cc.Node, displayName: '动画节点' })
  19. private effect_par: cc.Node = null
  20. @property({ type: cc.Enum(EffectType), displayName: '打开动画节点效果', visible() { return this.effect_par } })
  21. private open_effect: EffectType = EffectType.scale;
  22. @property({ type: cc.Enum(EffectType), displayName: '关闭动画节点效果', visible() { return this.effect_par } })
  23. private close_effect: EffectType = EffectType.scale;
  24. @property({ displayName: '关闭节点缩小到此节点坐标', visible() { return this.close_effect == EffectType.scaleToPoint } })
  25. private close_toPoint: string = "";
  26. private _play_open: boolean = false;
  27. @property({ displayName: "预览开" })
  28. get play_open(): boolean {
  29. if (this._play_open) {
  30. this.showEffect();
  31. this._play_open = false;
  32. }
  33. return this._play_open;
  34. }
  35. set play_open(v: boolean) {
  36. this._play_open = v;
  37. }
  38. private _play_close: boolean = false;
  39. @property({ displayName: "预览关" })
  40. get play_close(): boolean {
  41. if (this._play_close) {
  42. this.effect_par.scale = 1;
  43. this.hideEffect();
  44. this._play_close = false;
  45. }
  46. return this._play_close;
  47. }
  48. set play_close(v: boolean) {
  49. this._play_close = v;
  50. }
  51. onLoad() {
  52. // this.node.on('ui-close', this.hideEffect, this);// 界面关闭事件
  53. }
  54. start() {
  55. if (CC_EDITOR) return;
  56. this.showEffect();
  57. }
  58. /**
  59. * 界面打开效果
  60. */
  61. private showEffect() {
  62. if (this.effect_par) {
  63. switch (this.open_effect) {
  64. case EffectType.none:
  65. break;
  66. case EffectType.scale:
  67. this.effect_par.scale = 0;
  68. cc.tween(this.effect_par)
  69. .to(0.15, { scale: 1 })
  70. .start()
  71. break;
  72. case EffectType.centerUnfolding:
  73. this.effect_par.scaleX = 0;
  74. cc.tween(this.effect_par)
  75. .to(0.15, { scaleX: 1 })
  76. .start()
  77. break;
  78. }
  79. }
  80. }
  81. /**
  82. * 关闭打开效果
  83. * @param cb 结束回调
  84. */
  85. public hideEffect(cb: Function = null) {
  86. if (this.effect_par) {
  87. switch (this.close_effect) {
  88. case EffectType.none:
  89. cb && cb();
  90. break;
  91. case EffectType.scale:
  92. cc.tween(this.effect_par)
  93. .to(0.15, { scale: 0 })
  94. .call(() => {
  95. cb && cb();
  96. })
  97. .start()
  98. break;
  99. case EffectType.scaleToPoint:
  100. if (this.close_toPoint == "") {
  101. console.error('需要配置缩小到终点节点路径')
  102. } else {
  103. let path = this.close_toPoint;
  104. let node = cc.find(path);
  105. if (node) {
  106. let world_pos = node.parent.convertToWorldSpaceAR(node.getPosition());
  107. let node_pos = this.effect_par.parent.convertToNodeSpaceAR(world_pos);
  108. cc.tween(this.effect_par)
  109. .to(0.3, { scale: 0, position: new cc.Vec3(node_pos.x, node_pos.y, 0) })
  110. .call(() => {
  111. cb && cb();
  112. })
  113. .start()
  114. } else {
  115. cb && cb();
  116. cc.error('关闭时动画节点未找到,请检查!!!!!')
  117. }
  118. }
  119. break;
  120. }
  121. } else {
  122. cb && cb();
  123. }
  124. }
  125. }