SpriteAtlasAnim.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import ResSpriteAtlas from "./ResSpriteAtlas";
  2. import UEBase from "./UEBase";
  3. const { ccclass, property } = cc._decorator;
  4. export interface IAnimationInfo {
  5. bundle: string,
  6. atlasUrl: string,
  7. anim: {
  8. run: { name: string, frames: string[], wrapMode: cc.WrapMode, sample: number }
  9. }
  10. }
  11. export interface Params_SpriteAtlasAnim {
  12. [anim: string]: {
  13. frames: string[],
  14. wrapMode: cc.WrapMode,
  15. sample: number, // 动画帧率,单位为帧/秒
  16. }
  17. }
  18. @ccclass
  19. export class SpriteAtlasAnim extends UEBase {
  20. private _anim: cc.Animation;
  21. private _resSpriteAtlas: ResSpriteAtlas;
  22. private _animCfg: Params_SpriteAtlasAnim;
  23. /**
  24. * @param url 动画 SpriteAtlas Url
  25. * @param animCfg 动画配置
  26. */
  27. protected LoadAnimAsync(bundleKey: string, url: string, animCfg: Params_SpriteAtlasAnim, defaultAnim: string): Promise<boolean> {
  28. const that = this;
  29. that._animCfg = animCfg;
  30. that._anim = that.node.getComponent(cc.Animation);
  31. if (!this._anim) {
  32. this.node.addComponent(cc.Animation);
  33. }
  34. that._resSpriteAtlas = that.node.getComponent(ResSpriteAtlas);
  35. if (!that._resSpriteAtlas) {
  36. that.node.addComponent(ResSpriteAtlas);
  37. }
  38. return new Promise((resolve) => {
  39. that._resSpriteAtlas.LoadSpriteAtlas(bundleKey, url, (atlas: cc.SpriteAtlas) => {
  40. if (atlas) {
  41. that.RemoveAllClips();
  42. let sp: cc.SpriteFrame;
  43. for (let clipName in that._animCfg) {
  44. let animSpriteFrames: cc.SpriteFrame[] = [];
  45. that._animCfg[clipName].frames.forEach(frameKey => {
  46. sp = atlas.getSpriteFrame(frameKey);
  47. if (sp) {
  48. animSpriteFrames.push(sp);
  49. } else {
  50. console.error(`图集(${url})中不存在动画帧: ${frameKey}`);
  51. }
  52. });
  53. that.AddClip(clipName, animSpriteFrames, that._animCfg[clipName].wrapMode, that._animCfg[clipName].sample);
  54. }
  55. if (defaultAnim) this.PlayAnim(defaultAnim);
  56. resolve(true);
  57. } else {
  58. resolve(false);
  59. }
  60. });
  61. })
  62. }
  63. /**
  64. *
  65. * @param clipName
  66. * @param animSpriteFrames
  67. * @param sample 动画帧率,单位为帧/秒
  68. * @param wrapMode
  69. */
  70. private AddClip(clipName: string, animSpriteFrames: cc.SpriteFrame[], wrapMode: cc.WrapMode, sample: number) {
  71. // 创建一个动画
  72. let clip = cc.AnimationClip.createWithSpriteFrames(animSpriteFrames, sample);
  73. clip.name = clipName;
  74. clip.wrapMode = wrapMode;
  75. this._anim.addClip(clip);
  76. }
  77. private RemoveAllClips() {
  78. if (!this._anim || !this._anim.isValid) return;
  79. const clips = this._anim.getClips();
  80. for (let i = clips.length - 1; i >= 0; --i) {
  81. this._anim.removeClip(clips[i]);
  82. }
  83. }
  84. /**
  85. * 播放动画
  86. * @param clipName
  87. */
  88. PlayAnim(clipName: string) {
  89. this._anim.play(clipName);
  90. }
  91. StopAllAnim() {
  92. this._anim.stop();
  93. }
  94. /**
  95. * 监听动画完成
  96. * @param complete
  97. */
  98. OnAnimComplete(complete: Function) {
  99. this._anim.on(cc.Animation.EventType.FINISHED, (type: string, state: cc.AnimationState) => {
  100. complete && complete(type);
  101. }, this);
  102. }
  103. }