CustomSprite.ts 1018 B

1234567891011121314151617181920212223242526272829303132
  1. import { Sprite, AssetManager, assetManager, SpriteFrame } from "cc";
  2. import { EDITOR } from "cc/env";
  3. import { Debug } from "../util/Debug";
  4. const Tag: string = 'Sprite'
  5. if (!EDITOR) {
  6. Sprite.prototype.loadSpriteFrame = function (bundleName: string, path: string): void {
  7. const bundle: AssetManager.Bundle = assetManager.getBundle(bundleName)
  8. if (!bundle) {
  9. Debug.Warn(Tag, `bundle ${bundleName} not exist or loaded`)
  10. return
  11. }
  12. const spfPath: string = path + '/spriteFrame'
  13. let spf: SpriteFrame = bundle.get(spfPath, SpriteFrame)
  14. if (spf) {
  15. if (!this.isValid) return
  16. this.spriteFrame = spf
  17. } else {
  18. bundle.load(spfPath, SpriteFrame, (err, res) => {
  19. if (err) {
  20. Debug.Error(Tag, err.message)
  21. } else {
  22. if (!this.isValid) return
  23. this.spriteFrame = res
  24. }
  25. })
  26. }
  27. }
  28. }