LocalizedSprite.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const SpriteFrameSet = require('SpriteFrameSet');
  2. cc.Class({
  3. extends: cc.Component,
  4. editor: {
  5. executeInEditMode: true,
  6. inspector: 'packages://i18n/inspector/localized-sprite.js',
  7. menu: 'i18n/LocalizedSprite'
  8. },
  9. properties: {
  10. spriteFrameSet: {
  11. default: [],
  12. type: SpriteFrameSet
  13. }
  14. },
  15. onLoad () {
  16. this.fetchRender();
  17. },
  18. fetchRender () {
  19. let sprite = this.getComponent(cc.Sprite);
  20. if (sprite) {
  21. this.sprite = sprite;
  22. this.updateSprite(window.i18n.curLang);
  23. return;
  24. }
  25. },
  26. getSpriteFrameByLang (lang) {
  27. for (let i = 0; i < this.spriteFrameSet.length; ++i) {
  28. if (this.spriteFrameSet[i].language === lang) {
  29. return this.spriteFrameSet[i].spriteFrame;
  30. }
  31. }
  32. },
  33. updateSprite (language) {
  34. if (!this.sprite) {
  35. cc.error('Failed to update localized sprite, sprite component is invalid!');
  36. return;
  37. }
  38. let spriteFrame = this.getSpriteFrameByLang(language);
  39. if (!spriteFrame && this.spriteFrameSet[0]) {
  40. spriteFrame = this.spriteFrameSet[0].spriteFrame;
  41. }
  42. this.sprite.spriteFrame = spriteFrame;
  43. }
  44. });