import { Texture2D } from 'cc'; import { Enum } from 'cc'; import { rect } from 'cc'; import { Sprite } from 'cc'; import { _decorator, Component } from 'cc'; const { ccclass, property } = _decorator; @ccclass('MoveUV') export class MoveUV extends Component { @property moveSpeedX = 0; @property moveSpeedY = 0; @property({ type: Enum(Texture2D.WrapMode) }) wrapMode = Texture2D.WrapMode.REPEAT; private _sprite: Sprite; private _rect = rect(); private _currentWrapMode: number; getRect() { return this._rect; } onLoad() { this._sprite = this.getComponent(Sprite); if (!this._sprite) { let tempStr = "获取Sprite失败"; throw new Error(tempStr); } this._rect.set(this._sprite.spriteFrame.rect); this._sprite.spriteFrame.packable = false; } update(dt: number) { if (this._currentWrapMode !== this.wrapMode) { this._currentWrapMode = this.wrapMode; this._sprite.spriteFrame.texture.setWrapMode(this.wrapMode, this.wrapMode); } this._rect.x += this.moveSpeedX * dt; this._rect.y += this.moveSpeedY * dt; this._sprite.spriteFrame.rect = this._rect; this._sprite.markForUpdateRenderData(); } }