ColorRibbonBox.ts 1.3 KB

1234567891011121314151617181920212223242526272829
  1. import { _decorator, Component, Node, UITransform, Prefab, instantiate, Vec3 } from 'cc';
  2. import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('ColorRibbonBox')
  5. export class ColorRibbonBox extends Component {
  6. @property({ tooltip: "彩带(不填写默认为当前node)", type: Node }) public ribbonBox: Node;
  7. @property({ tooltip: "加载特效间隔时间,单位秒" }) public interval: number = 0.5;
  8. @property({ tooltip: "加载特效次数" }) public times: number = 20;
  9. @property({ tooltip: "特效密集度" }) public spacing: number = 40;
  10. start() {
  11. if (!this.ribbonBox) {
  12. this.ribbonBox = this.node;
  13. }
  14. this.schedule(this.addRibbon, this.interval, this.times);
  15. }
  16. public async addRibbon() {
  17. let temp = Math.ceil(this.ribbonBox.getComponent(UITransform).width / this.spacing);
  18. for (let i = 0; i < temp + 1; i++) {
  19. let _ribbon = await ResourcesUtils.load<Prefab>("Prefabs/SpecialEffects/彩带move", null, this.node);
  20. let ribbon = instantiate(_ribbon);
  21. ribbon.parent = this.ribbonBox;
  22. let _X = this.ribbonBox.getComponent(UITransform).width / 2 - ((i - 1) * this.spacing + (Math.random() * this.spacing));
  23. ribbon.setPosition(new Vec3(_X));
  24. }
  25. }
  26. }