AnimationDB.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /**
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2012-2017 DragonBones team and other contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. * this software and associated documentation files (the "Software"), to deal in
  8. * the Software without restriction, including without limitation the rights to
  9. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. * the Software, and to permit persons to whom the Software is furnished to do so,
  11. * subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. using System.Collections.Generic;
  24. namespace DragonBones
  25. {
  26. /// <summary>
  27. /// - The animation player is used to play the animation data and manage the animation states.
  28. /// </summary>
  29. /// <see cref="DragonBones.AnimationData"/>
  30. /// <see cref="DragonBones.AnimationState"/>
  31. /// <version>DragonBones 3.0</version>
  32. /// <language>en_US</language>
  33. /// <summary>
  34. /// - 动画播放器用来播放动画数据和管理动画状态。
  35. /// </summary>
  36. /// <see cref="DragonBones.AnimationData"/>
  37. /// <see cref="DragonBones.AnimationState"/>
  38. /// <version>DragonBones 3.0</version>
  39. /// <language>zh_CN</language>
  40. public class AnimationDB : BaseObject
  41. {
  42. /// <summary>
  43. /// - The play speed of all animations. [0: Stop, (0~1): Slow, 1: Normal, (1~N): Fast]
  44. /// </summary>
  45. /// <default>1.0</default>
  46. /// <version>DragonBones 3.0</version>
  47. /// <language>en_US</language>
  48. /// <summary>
  49. /// - 所有动画的播放速度。 [0: 停止播放, (0~1): 慢速播放, 1: 正常播放, (1~N): 快速播放]
  50. /// </summary>
  51. /// <default>1.0</default>
  52. /// <version>DragonBones 3.0</version>
  53. /// <language>zh_CN</language>
  54. public float timeScale;
  55. private bool _lockUpdate;
  56. // Update bones and slots cachedFrameIndices.
  57. private bool _animationDirty;
  58. private float _inheritTimeScale;
  59. private readonly List<string> _animationNames = new List<string>();
  60. private readonly List<AnimationState> _animationStates = new List<AnimationState>();
  61. private readonly Dictionary<string, AnimationData> _animations = new Dictionary<string, AnimationData>();
  62. private Armature _armature;
  63. private AnimationConfig _animationConfig = null; // Initial value.
  64. private AnimationState _lastAnimationState;
  65. /// <private/>
  66. protected override void _OnClear()
  67. {
  68. foreach (var animationState in this._animationStates)
  69. {
  70. animationState.ReturnToPool();
  71. }
  72. if (this._animationConfig != null)
  73. {
  74. this._animationConfig.ReturnToPool();
  75. }
  76. this.timeScale = 1.0f;
  77. this._lockUpdate = false;
  78. this._animationDirty = false;
  79. this._inheritTimeScale = 1.0f;
  80. this._animationNames.Clear();
  81. this._animationStates.Clear();
  82. this._animations.Clear();
  83. this._armature = null; //
  84. this._animationConfig = null; //
  85. this._lastAnimationState = null;
  86. }
  87. private void _FadeOut(AnimationConfig animationConfig)
  88. {
  89. switch (animationConfig.fadeOutMode)
  90. {
  91. case AnimationFadeOutMode.SameLayer:
  92. foreach (var animationState in this._animationStates)
  93. {
  94. if (animationState._parent != null)
  95. {
  96. continue;
  97. }
  98. if (animationState.layer == animationConfig.layer)
  99. {
  100. animationState.FadeOut(animationConfig.fadeOutTime, animationConfig.pauseFadeOut);
  101. }
  102. }
  103. break;
  104. case AnimationFadeOutMode.SameGroup:
  105. foreach (var animationState in this._animationStates)
  106. {
  107. if (animationState._parent != null)
  108. {
  109. continue;
  110. }
  111. if (animationState.group == animationConfig.group)
  112. {
  113. animationState.FadeOut(animationConfig.fadeOutTime, animationConfig.pauseFadeOut);
  114. }
  115. }
  116. break;
  117. case AnimationFadeOutMode.SameLayerAndGroup:
  118. foreach (var animationState in this._animationStates)
  119. {
  120. if (animationState._parent != null)
  121. {
  122. continue;
  123. }
  124. if (animationState.layer == animationConfig.layer &&
  125. animationState.group == animationConfig.group)
  126. {
  127. animationState.FadeOut(animationConfig.fadeOutTime, animationConfig.pauseFadeOut);
  128. }
  129. }
  130. break;
  131. case AnimationFadeOutMode.All:
  132. foreach (var animationState in this._animationStates)
  133. {
  134. if (animationState._parent != null)
  135. {
  136. continue;
  137. }
  138. animationState.FadeOut(animationConfig.fadeOutTime, animationConfig.pauseFadeOut);
  139. }
  140. break;
  141. case AnimationFadeOutMode.None:
  142. case AnimationFadeOutMode.Single:
  143. default:
  144. break;
  145. }
  146. }
  147. /// <internal/>
  148. /// <private/>
  149. internal void Init(Armature armature)
  150. {
  151. if (this._armature != null)
  152. {
  153. return;
  154. }
  155. this._armature = armature;
  156. this._animationConfig = BaseObject.BorrowObject<AnimationConfig>();
  157. }
  158. /// <internal/>
  159. /// <private/>
  160. internal void AdvanceTime(float passedTime)
  161. {
  162. if (passedTime < 0.0f)
  163. {
  164. // Only animationState can reverse play.
  165. passedTime = -passedTime;
  166. }
  167. if (this._armature.inheritAnimation && this._armature._parent != null)
  168. {
  169. // Inherit parent animation timeScale.
  170. this._inheritTimeScale = this._armature._parent._armature.animation._inheritTimeScale * this.timeScale;
  171. }
  172. else
  173. {
  174. this._inheritTimeScale = this.timeScale;
  175. }
  176. if (this._inheritTimeScale != 1.0f)
  177. {
  178. passedTime *= this._inheritTimeScale;
  179. }
  180. var animationStateCount = this._animationStates.Count;
  181. if (animationStateCount == 1)
  182. {
  183. var animationState = this._animationStates[0];
  184. if (animationState._fadeState > 0 && animationState._subFadeState > 0)
  185. {
  186. this._armature._dragonBones.BufferObject(animationState);
  187. this._animationStates.Clear();
  188. this._lastAnimationState = null;
  189. }
  190. else
  191. {
  192. var animationData = animationState._animationData;
  193. var cacheFrameRate = animationData.cacheFrameRate;
  194. if (this._animationDirty && cacheFrameRate > 0.0f)
  195. {
  196. // Update cachedFrameIndices.
  197. this._animationDirty = false;
  198. foreach (var bone in this._armature.GetBones())
  199. {
  200. bone._cachedFrameIndices = animationData.GetBoneCachedFrameIndices(bone.name);
  201. }
  202. foreach (var slot in this._armature.GetSlots())
  203. {
  204. var rawDisplayDatas = slot.rawDisplayDatas;
  205. if (rawDisplayDatas != null && rawDisplayDatas.Count > 0)
  206. {
  207. var rawDsplayData = rawDisplayDatas[0];
  208. if (rawDsplayData != null)
  209. {
  210. if (rawDsplayData.parent == this._armature.armatureData.defaultSkin)
  211. {
  212. slot._cachedFrameIndices = animationData.GetSlotCachedFrameIndices(slot.name);
  213. continue;
  214. }
  215. }
  216. }
  217. slot._cachedFrameIndices = null;
  218. }
  219. }
  220. animationState.AdvanceTime(passedTime, cacheFrameRate);
  221. }
  222. }
  223. else if (animationStateCount > 1)
  224. {
  225. for (int i = 0, r = 0; i < animationStateCount; ++i)
  226. {
  227. var animationState = this._animationStates[i];
  228. if (animationState._fadeState > 0 && animationState._subFadeState > 0)
  229. {
  230. r++;
  231. this._armature._dragonBones.BufferObject(animationState);
  232. this._animationDirty = true;
  233. if (this._lastAnimationState == animationState)
  234. {
  235. // Update last animation state.
  236. this._lastAnimationState = null;
  237. }
  238. }
  239. else
  240. {
  241. if (r > 0)
  242. {
  243. this._animationStates[i - r] = animationState;
  244. }
  245. animationState.AdvanceTime(passedTime, 0.0f);
  246. }
  247. if (i == animationStateCount - 1 && r > 0)
  248. {
  249. // Modify animation states size.
  250. this._animationStates.ResizeList(this._animationStates.Count - r);
  251. if (this._lastAnimationState == null && this._animationStates.Count > 0)
  252. {
  253. this._lastAnimationState = this._animationStates[this._animationStates.Count - 1];
  254. }
  255. }
  256. }
  257. this._armature._cacheFrameIndex = -1;
  258. }
  259. else
  260. {
  261. this._armature._cacheFrameIndex = -1;
  262. }
  263. }
  264. /// <summary>
  265. /// - Clear all animations states.
  266. /// </summary>
  267. /// <see cref="DragonBones.AnimationState"/>
  268. /// <version>DragonBones 4.5</version>
  269. /// <language>en_US</language>
  270. /// <summary>
  271. /// - 清除所有的动画状态。
  272. /// </summary>
  273. /// <see cref="DragonBones.AnimationState"/>
  274. /// <version>DragonBones 4.5</version>
  275. /// <language>zh_CN</language>
  276. public void Reset()
  277. {
  278. foreach (var animationState in this._animationStates)
  279. {
  280. animationState.ReturnToPool();
  281. }
  282. this._animationDirty = false;
  283. this._animationConfig.Clear();
  284. this._animationStates.Clear();
  285. this._lastAnimationState = null;
  286. }
  287. /// <summary>
  288. /// - Pause a specific animation state.
  289. /// </summary>
  290. /// <param name="animationName">- The name of animation state. (If not set, it will pause all animations)</param>
  291. /// <see cref="DragonBones.AnimationState"/>
  292. /// <version>DragonBones 3.0</version>
  293. /// <language>en_US</language>
  294. /// <summary>
  295. /// - 暂停指定动画状态的播放。
  296. /// </summary>
  297. /// <param name="animationName">- 动画状态名称。 (如果未设置,则暂停所有动画)</param>
  298. /// <see cref="DragonBones.AnimationState"/>
  299. /// <version>DragonBones 3.0</version>
  300. /// <language>zh_CN</language>
  301. public void Stop(string animationName = null)
  302. {
  303. if (animationName != null)
  304. {
  305. var animationState = this.GetState(animationName);
  306. if (animationState != null)
  307. {
  308. animationState.Stop();
  309. }
  310. }
  311. else
  312. {
  313. foreach (var animationState in this._animationStates)
  314. {
  315. animationState.Stop();
  316. }
  317. }
  318. }
  319. /// <summary>
  320. /// - Play animation with a specific animation config.
  321. /// The API is still in the experimental phase and may encounter bugs or stability or compatibility issues when used.
  322. /// </summary>
  323. /// <param name="animationConfig">- The animation config.</param>
  324. /// <returns>The playing animation state.</returns>
  325. /// <see cref="DragonBones.AnimationConfig"/>
  326. /// <beta/>
  327. /// <version>DragonBones 5.0</version>
  328. /// <language>en_US</language>
  329. /// <summary>
  330. /// - 通过指定的动画配置来播放动画。
  331. /// 该 API 仍在实验阶段,使用时可能遭遇 bug 或稳定性或兼容性问题。
  332. /// </summary>
  333. /// <param name="animationConfig">- 动画配置。</param>
  334. /// <returns>播放的动画状态。</returns>
  335. /// <see cref="DragonBones.AnimationConfig"/>
  336. /// <beta/>
  337. /// <version>DragonBones 5.0</version>
  338. /// <language>zh_CN</language>
  339. public AnimationState PlayConfig(AnimationConfig animationConfig)
  340. {
  341. var animationName = animationConfig.animation;
  342. if (!(this._animations.ContainsKey(animationName)))
  343. {
  344. Helper.Assert(false,
  345. "Non-existent animation.\n" +
  346. "DragonBones name: " + this._armature.armatureData.parent.name +
  347. "Armature name: " + this._armature.name +
  348. "Animation name: " + animationName
  349. );
  350. return null;
  351. }
  352. var animationData = this._animations[animationName];
  353. if (animationConfig.fadeOutMode == AnimationFadeOutMode.Single)
  354. {
  355. foreach (var aniState in this._animationStates)
  356. {
  357. if (aniState._animationData == animationData)
  358. {
  359. return aniState;
  360. }
  361. }
  362. }
  363. if (this._animationStates.Count == 0)
  364. {
  365. animationConfig.fadeInTime = 0.0f;
  366. }
  367. else if (animationConfig.fadeInTime < 0.0f)
  368. {
  369. animationConfig.fadeInTime = animationData.fadeInTime;
  370. }
  371. if (animationConfig.fadeOutTime < 0.0f)
  372. {
  373. animationConfig.fadeOutTime = animationConfig.fadeInTime;
  374. }
  375. if (animationConfig.timeScale <= -100.0f)
  376. {
  377. animationConfig.timeScale = 1.0f / animationData.scale;
  378. }
  379. if (animationData.frameCount > 1)
  380. {
  381. if (animationConfig.position < 0.0f)
  382. {
  383. animationConfig.position %= animationData.duration;
  384. animationConfig.position = animationData.duration - animationConfig.position;
  385. }
  386. else if (animationConfig.position == animationData.duration)
  387. {
  388. animationConfig.position -= 0.000001f; // Play a little time before end.
  389. }
  390. else if (animationConfig.position > animationData.duration)
  391. {
  392. animationConfig.position %= animationData.duration;
  393. }
  394. if (animationConfig.duration > 0.0f && animationConfig.position + animationConfig.duration > animationData.duration)
  395. {
  396. animationConfig.duration = animationData.duration - animationConfig.position;
  397. }
  398. if (animationConfig.playTimes < 0)
  399. {
  400. animationConfig.playTimes = (int)animationData.playTimes;
  401. }
  402. }
  403. else
  404. {
  405. animationConfig.playTimes = 1;
  406. animationConfig.position = 0.0f;
  407. if (animationConfig.duration > 0.0)
  408. {
  409. animationConfig.duration = 0.0f;
  410. }
  411. }
  412. if (animationConfig.duration == 0.0f)
  413. {
  414. animationConfig.duration = -1.0f;
  415. }
  416. this._FadeOut(animationConfig);
  417. var animationState = BaseObject.BorrowObject<AnimationState>();
  418. animationState.Init(this._armature, animationData, animationConfig);
  419. this._animationDirty = true;
  420. this._armature._cacheFrameIndex = -1;
  421. if (this._animationStates.Count > 0)
  422. {
  423. var added = false;
  424. for (int i = 0, l = this._animationStates.Count; i < l; ++i)
  425. {
  426. if (animationState.layer > this._animationStates[i].layer)
  427. {
  428. added = true;
  429. this._animationStates.Insert(i, animationState);
  430. break;
  431. }
  432. else if (i != l - 1 && animationState.layer > this._animationStates[i + 1].layer)
  433. {
  434. added = true;
  435. this._animationStates.Insert(i + 1, animationState);
  436. break;
  437. }
  438. }
  439. if (!added)
  440. {
  441. this._animationStates.Add(animationState);
  442. }
  443. }
  444. else
  445. {
  446. this._animationStates.Add(animationState);
  447. }
  448. // Child armature play same name animation.
  449. foreach (var slot in this._armature.GetSlots())
  450. {
  451. var childArmature = slot.childArmature;
  452. if (childArmature != null &&
  453. childArmature.inheritAnimation &&
  454. childArmature.animation.HasAnimation(animationName) &&
  455. childArmature.animation.GetState(animationName) == null)
  456. {
  457. childArmature.animation.FadeIn(animationName); //
  458. }
  459. }
  460. if (!this._lockUpdate)
  461. {
  462. if (animationConfig.fadeInTime <= 0.0f)
  463. {
  464. // Blend animation state, update armature.
  465. this._armature.AdvanceTime(0.0f);
  466. }
  467. }
  468. this._lastAnimationState = animationState;
  469. return animationState;
  470. }
  471. /// <summary>
  472. /// - Play a specific animation.
  473. /// </summary>
  474. /// <param name="animationName">- The name of animation data. (If not set, The default animation will be played, or resume the animation playing from pause status, or replay the last playing animation)</param>
  475. /// <param name="playTimes">- Playing repeat times. [-1: Use default value of the animation data, 0: No end loop playing, [1~N]: Repeat N times] (default: -1)</param>
  476. /// <returns>The playing animation state.</returns>
  477. /// <example>
  478. /// TypeScript style, for reference only.
  479. /// <pre>
  480. /// armature.animation.play("walk");
  481. /// </pre>
  482. /// </example>
  483. /// <version>DragonBones 3.0</version>
  484. /// <language>en_US</language>
  485. /// <summary>
  486. /// - 播放指定动画。
  487. /// </summary>
  488. /// <param name="animationName">- 动画数据名称。 (如果未设置,则播放默认动画,或将暂停状态切换为播放状态,或重新播放之前播放的动画)</param>
  489. /// <param name="playTimes">- 循环播放次数。 [-1: 使用动画数据默认值, 0: 无限循环播放, [1~N]: 循环播放 N 次] (默认: -1)</param>
  490. /// <returns>播放的动画状态。</returns>
  491. /// <example>
  492. /// TypeScript 风格,仅供参考。
  493. /// <pre>
  494. /// armature.animation.play("walk");
  495. /// </pre>
  496. /// </example>
  497. /// <version>DragonBones 3.0</version>
  498. /// <language>zh_CN</language>
  499. public AnimationState Play(string animationName = null, int playTimes = -1)
  500. {
  501. this._animationConfig.Clear();
  502. this._animationConfig.resetToPose = true;
  503. this._animationConfig.playTimes = playTimes;
  504. this._animationConfig.fadeInTime = 0.0f;
  505. this._animationConfig.animation = animationName != null ? animationName : "";
  506. if (animationName != null && animationName.Length > 0)
  507. {
  508. this.PlayConfig(this._animationConfig);
  509. }
  510. else if (this._lastAnimationState == null)
  511. {
  512. var defaultAnimation = this._armature.armatureData.defaultAnimation;
  513. if (defaultAnimation != null)
  514. {
  515. this._animationConfig.animation = defaultAnimation.name;
  516. this.PlayConfig(this._animationConfig);
  517. }
  518. }
  519. else if (!this._lastAnimationState.isPlaying && !this._lastAnimationState.isCompleted)
  520. {
  521. this._lastAnimationState.Play();
  522. }
  523. else
  524. {
  525. this._animationConfig.animation = this._lastAnimationState.name;
  526. this.PlayConfig(this._animationConfig);
  527. }
  528. return this._lastAnimationState;
  529. }
  530. /// <summary>
  531. /// - Fade in a specific animation.
  532. /// </summary>
  533. /// <param name="animationName">- The name of animation data.</param>
  534. /// <param name="fadeInTime">- The fade in time. [-1: Use the default value of animation data, [0~N]: The fade in time (In seconds)] (Default: -1)</param>
  535. /// <param name="playTimes">- playing repeat times. [-1: Use the default value of animation data, 0: No end loop playing, [1~N]: Repeat N times] (Default: -1)</param>
  536. /// <param name="layer">- The blending layer, the animation states in high level layer will get the blending weights with high priority, when the total blending weights are more than 1.0, there will be no more weights can be allocated to the other animation states. (Default: 0)</param>
  537. /// <param name="group">- The blending group name, it is typically used to specify the substitution of multiple animation states blending. (Default: null)</param>
  538. /// <param name="fadeOutMode">- The fade out mode, which is typically used to specify alternate mode of multiple animation states blending. (Default: AnimationFadeOutMode.SameLayerAndGroup)</param>
  539. /// <returns>The playing animation state.</returns>
  540. /// <example>
  541. /// TypeScript style, for reference only.
  542. /// <pre>
  543. /// armature.animation.fadeIn("walk", 0.3, 0, 0, "normalGroup").resetToPose = false;
  544. /// armature.animation.fadeIn("attack", 0.3, 1, 0, "attackGroup").resetToPose = false;
  545. /// </pre>
  546. /// </example>
  547. /// <version>DragonBones 4.5</version>
  548. /// <language>en_US</language>
  549. /// <summary>
  550. /// - 淡入播放指定的动画。
  551. /// </summary>
  552. /// <param name="animationName">- 动画数据名称。</param>
  553. /// <param name="fadeInTime">- 淡入时间。 [-1: 使用动画数据默认值, [0~N]: 淡入时间 (以秒为单位)] (默认: -1)</param>
  554. /// <param name="playTimes">- 播放次数。 [-1: 使用动画数据默认值, 0: 无限循环播放, [1~N]: 循环播放 N 次] (默认: -1)</param>
  555. /// <param name="layer">- 混合图层,图层高的动画状态会优先获取混合权重,当混合权重分配总和超过 1.0 时,剩余的动画状态将不能再获得权重分配。 (默认: 0)</param>
  556. /// <param name="group">- 混合组名称,该属性通常用来指定多个动画状态混合时的相互替换关系。 (默认: null)</param>
  557. /// <param name="fadeOutMode">- 淡出模式,该属性通常用来指定多个动画状态混合时的相互替换模式。 (默认: AnimationFadeOutMode.SameLayerAndGroup)</param>
  558. /// <returns>播放的动画状态。</returns>
  559. /// <example>
  560. /// TypeScript 风格,仅供参考。
  561. /// <pre>
  562. /// armature.animation.fadeIn("walk", 0.3, 0, 0, "normalGroup").resetToPose = false;
  563. /// armature.animation.fadeIn("attack", 0.3, 1, 0, "attackGroup").resetToPose = false;
  564. /// </pre>
  565. /// </example>
  566. /// <version>DragonBones 4.5</version>
  567. /// <language>zh_CN</language>
  568. public AnimationState FadeIn(string animationName, float fadeInTime = -1.0f, int playTimes = -1,
  569. int layer = 0, string group = null,
  570. AnimationFadeOutMode fadeOutMode = AnimationFadeOutMode.SameLayerAndGroup)
  571. {
  572. this._animationConfig.Clear();
  573. this._animationConfig.fadeOutMode = fadeOutMode;
  574. this._animationConfig.playTimes = playTimes;
  575. this._animationConfig.layer = layer;
  576. this._animationConfig.fadeInTime = fadeInTime;
  577. this._animationConfig.animation = animationName;
  578. this._animationConfig.group = group != null ? group : "";
  579. return this.PlayConfig(this._animationConfig);
  580. }
  581. /// <summary>
  582. /// - Play a specific animation from the specific time.
  583. /// </summary>
  584. /// <param name="animationName">- The name of animation data.</param>
  585. /// <param name="time">- The start time point of playing. (In seconds)</param>
  586. /// <param name="playTimes">- Playing repeat times. [-1: Use the default value of animation data, 0: No end loop playing, [1~N]: Repeat N times] (Default: -1)</param>
  587. /// <returns>The played animation state.</returns>
  588. /// <version>DragonBones 4.5</version>
  589. /// <language>en_US</language>
  590. /// <summary>
  591. /// - 从指定时间开始播放指定的动画。
  592. /// </summary>
  593. /// <param name="animationName">- 动画数据名称。</param>
  594. /// <param name="time">- 播放开始的时间。 (以秒为单位)</param>
  595. /// <param name="playTimes">- 循环播放次数。 [-1: 使用动画数据默认值, 0: 无限循环播放, [1~N]: 循环播放 N 次] (默认: -1)</param>
  596. /// <returns>播放的动画状态。</returns>
  597. /// <version>DragonBones 4.5</version>
  598. /// <language>zh_CN</language>
  599. public AnimationState GotoAndPlayByTime(string animationName, float time = 0.0f, int playTimes = -1)
  600. {
  601. this._animationConfig.Clear();
  602. this._animationConfig.resetToPose = true;
  603. this._animationConfig.playTimes = playTimes;
  604. this._animationConfig.position = time;
  605. this._animationConfig.fadeInTime = 0.0f;
  606. this._animationConfig.animation = animationName;
  607. return this.PlayConfig(this._animationConfig);
  608. }
  609. /// <summary>
  610. /// - Play a specific animation from the specific frame.
  611. /// </summary>
  612. /// <param name="animationName">- The name of animation data.</param>
  613. /// <param name="frame">- The start frame of playing.</param>
  614. /// <param name="playTimes">- Playing repeat times. [-1: Use the default value of animation data, 0: No end loop playing, [1~N]: Repeat N times] (Default: -1)</param>
  615. /// <returns>The played animation state.</returns>
  616. /// <version>DragonBones 4.5</version>
  617. /// <language>en_US</language>
  618. /// <summary>
  619. /// - 从指定帧开始播放指定的动画。
  620. /// </summary>
  621. /// <param name="animationName">- 动画数据名称。</param>
  622. /// <param name="frame">- 播放开始的帧数。</param>
  623. /// <param name="playTimes">- 播放次数。 [-1: 使用动画数据默认值, 0: 无限循环播放, [1~N]: 循环播放 N 次] (默认: -1)</param>
  624. /// <returns>播放的动画状态。</returns>
  625. /// <version>DragonBones 4.5</version>
  626. /// <language>zh_CN</language>
  627. public AnimationState GotoAndPlayByFrame(string animationName, uint frame = 0, int playTimes = -1)
  628. {
  629. this._animationConfig.Clear();
  630. this._animationConfig.resetToPose = true;
  631. this._animationConfig.playTimes = playTimes;
  632. this._animationConfig.fadeInTime = 0.0f;
  633. this._animationConfig.animation = animationName;
  634. var animationData = this._animations.ContainsKey(animationName) ? this._animations[animationName] : null;
  635. if (animationData != null)
  636. {
  637. this._animationConfig.position = animationData.duration * frame / animationData.frameCount;
  638. }
  639. return this.PlayConfig(this._animationConfig);
  640. }
  641. /// <summary>
  642. /// - Play a specific animation from the specific progress.
  643. /// </summary>
  644. /// <param name="animationName">- The name of animation data.</param>
  645. /// <param name="progress">- The start progress value of playing.</param>
  646. /// <param name="playTimes">- Playing repeat times. [-1: Use the default value of animation data, 0: No end loop playing, [1~N]: Repeat N times] (Default: -1)</param>
  647. /// <returns>The played animation state.</returns>
  648. /// <version>DragonBones 4.5</version>
  649. /// <language>en_US</language>
  650. /// <summary>
  651. /// - 从指定进度开始播放指定的动画。
  652. /// </summary>
  653. /// <param name="animationName">- 动画数据名称。</param>
  654. /// <param name="progress">- 开始播放的进度。</param>
  655. /// <param name="playTimes">- 播放次数。 [-1: 使用动画数据默认值, 0: 无限循环播放, [1~N]: 循环播放 N 次] (默认: -1)</param>
  656. /// <returns>播放的动画状态。</returns>
  657. /// <version>DragonBones 4.5</version>
  658. /// <language>zh_CN</language>
  659. public AnimationState GotoAndPlayByProgress(string animationName, float progress = 0.0f, int playTimes = -1)
  660. {
  661. this._animationConfig.Clear();
  662. this._animationConfig.resetToPose = true;
  663. this._animationConfig.playTimes = playTimes;
  664. this._animationConfig.fadeInTime = 0.0f;
  665. this._animationConfig.animation = animationName;
  666. var animationData = this._animations.ContainsKey(animationName) ? this._animations[animationName] : null;
  667. if (animationData != null)
  668. {
  669. this._animationConfig.position = animationData.duration * (progress > 0.0f ? progress : 0.0f);
  670. }
  671. return this.PlayConfig(this._animationConfig);
  672. }
  673. /// <summary>
  674. /// - Stop a specific animation at the specific time.
  675. /// </summary>
  676. /// <param name="animationName">- The name of animation data.</param>
  677. /// <param name="time">- The stop time. (In seconds)</param>
  678. /// <returns>The played animation state.</returns>
  679. /// <version>DragonBones 4.5</version>
  680. /// <language>en_US</language>
  681. /// <summary>
  682. /// - 在指定时间停止指定动画播放
  683. /// </summary>
  684. /// <param name="animationName">- 动画数据名称。</param>
  685. /// <param name="time">- 停止的时间。 (以秒为单位)</param>
  686. /// <returns>播放的动画状态。</returns>
  687. /// <version>DragonBones 4.5</version>
  688. /// <language>zh_CN</language>
  689. public AnimationState GotoAndStopByTime(string animationName, float time = 0.0f)
  690. {
  691. var animationState = this.GotoAndPlayByTime(animationName, time, 1);
  692. if (animationState != null)
  693. {
  694. animationState.Stop();
  695. }
  696. return animationState;
  697. }
  698. /// <summary>
  699. /// - Stop a specific animation at the specific frame.
  700. /// </summary>
  701. /// <param name="animationName">- The name of animation data.</param>
  702. /// <param name="frame">- The stop frame.</param>
  703. /// <returns>The played animation state.</returns>
  704. /// <version>DragonBones 4.5</version>
  705. /// <language>en_US</language>
  706. /// <summary>
  707. /// - 在指定帧停止指定动画的播放
  708. /// </summary>
  709. /// <param name="animationName">- 动画数据名称。</param>
  710. /// <param name="frame">- 停止的帧数。</param>
  711. /// <returns>播放的动画状态。</returns>
  712. /// <version>DragonBones 4.5</version>
  713. /// <language>zh_CN</language>
  714. public AnimationState GotoAndStopByFrame(string animationName, uint frame = 0)
  715. {
  716. var animationState = this.GotoAndPlayByFrame(animationName, frame, 1);
  717. if (animationState != null)
  718. {
  719. animationState.Stop();
  720. }
  721. return animationState;
  722. }
  723. /// <summary>
  724. /// - Stop a specific animation at the specific progress.
  725. /// </summary>
  726. /// <param name="animationName">- The name of animation data.</param>
  727. /// <param name="progress">- The stop progress value.</param>
  728. /// <returns>The played animation state.</returns>
  729. /// <version>DragonBones 4.5</version>
  730. /// <language>en_US</language>
  731. /// <summary>
  732. /// - 在指定的进度停止指定的动画播放。
  733. /// </summary>
  734. /// <param name="animationName">- 动画数据名称。</param>
  735. /// <param name="progress">- 停止进度。</param>
  736. /// <returns>播放的动画状态。</returns>
  737. /// <version>DragonBones 4.5</version>
  738. /// <language>zh_CN</language>
  739. public AnimationState GotoAndStopByProgress(string animationName, float progress = 0.0f)
  740. {
  741. var animationState = this.GotoAndPlayByProgress(animationName, progress, 1);
  742. if (animationState != null)
  743. {
  744. animationState.Stop();
  745. }
  746. return animationState;
  747. }
  748. /// <summary>
  749. /// - Get a specific animation state.
  750. /// </summary>
  751. /// <param name="animationName">- The name of animation state.</param>
  752. /// <example>
  753. /// TypeScript style, for reference only.
  754. /// <pre>
  755. /// armature.animation.play("walk");
  756. /// let walkState = armature.animation.getState("walk");
  757. /// walkState.timeScale = 0.5;
  758. /// </pre>
  759. /// </example>
  760. /// <version>DragonBones 3.0</version>
  761. /// <language>en_US</language>
  762. /// <summary>
  763. /// - 获取指定的动画状态
  764. /// </summary>
  765. /// <param name="animationName">- 动画状态名称。</param>
  766. /// <example>
  767. /// TypeScript 风格,仅供参考。
  768. /// <pre>
  769. /// armature.animation.play("walk");
  770. /// let walkState = armature.animation.getState("walk");
  771. /// walkState.timeScale = 0.5;
  772. /// </pre>
  773. /// </example>
  774. /// <version>DragonBones 3.0</version>
  775. /// <language>zh_CN</language>
  776. public AnimationState GetState(string animationName)
  777. {
  778. var i = this._animationStates.Count;
  779. while (i-- > 0)
  780. {
  781. var animationState = this._animationStates[i];
  782. if (animationState.name == animationName)
  783. {
  784. return animationState;
  785. }
  786. }
  787. return null;
  788. }
  789. /// <summary>
  790. /// - Check whether a specific animation data is included.
  791. /// </summary>
  792. /// <param name="animationName">- The name of animation data.</param>
  793. /// <see cref="DragonBones.AnimationData"/>
  794. /// <version>DragonBones 3.0</version>
  795. /// <language>en_US</language>
  796. /// <summary>
  797. /// - 检查是否包含指定的动画数据
  798. /// </summary>
  799. /// <param name="animationName">- 动画数据名称。</param>
  800. /// <see cref="DragonBones.AnimationData"/>
  801. /// <version>DragonBones 3.0</version>
  802. /// <language>zh_CN</language>
  803. public bool HasAnimation(string animationName)
  804. {
  805. return this._animations.ContainsKey(animationName);
  806. }
  807. /// <summary>
  808. /// - Get all the animation states.
  809. /// </summary>
  810. /// <version>DragonBones 5.1</version>
  811. /// <language>en_US</language>
  812. /// <summary>
  813. /// - 获取所有的动画状态
  814. /// </summary>
  815. /// <version>DragonBones 5.1</version>
  816. /// <language>zh_CN</language>
  817. public List<AnimationState> GetStates()
  818. {
  819. return this._animationStates;
  820. }
  821. /// <summary>
  822. /// - Check whether there is an animation state is playing
  823. /// </summary>
  824. /// <see cref="DragonBones.AnimationState"/>
  825. /// <version>DragonBones 3.0</version>
  826. /// <language>en_US</language>
  827. /// <summary>
  828. /// - 检查是否有动画状态正在播放
  829. /// </summary>
  830. /// <see cref="DragonBones.AnimationState"/>
  831. /// <version>DragonBones 3.0</version>
  832. /// <language>zh_CN</language>
  833. public bool isPlaying
  834. {
  835. get
  836. {
  837. foreach (var animationState in this._animationStates)
  838. {
  839. if (animationState.isPlaying)
  840. {
  841. return true;
  842. }
  843. }
  844. return false;
  845. }
  846. }
  847. /// <summary>
  848. /// - Check whether all the animation states' playing were finished.
  849. /// </summary>
  850. /// <see cref="DragonBones.AnimationState"/>
  851. /// <version>DragonBones 3.0</version>
  852. /// <language>en_US</language>
  853. /// <summary>
  854. /// - 检查是否所有的动画状态均已播放完毕。
  855. /// </summary>
  856. /// <see cref="DragonBones.AnimationState"/>
  857. /// <version>DragonBones 3.0</version>
  858. /// <language>zh_CN</language>
  859. public bool isCompleted
  860. {
  861. get
  862. {
  863. foreach (var animationState in this._animationStates)
  864. {
  865. if (!animationState.isCompleted)
  866. {
  867. return false;
  868. }
  869. }
  870. return this._animationStates.Count > 0;
  871. }
  872. }
  873. /// <summary>
  874. /// - The name of the last playing animation state.
  875. /// </summary>
  876. /// <see cref="lastAnimationState"/>
  877. /// <version>DragonBones 3.0</version>
  878. /// <language>en_US</language>
  879. /// <summary>
  880. /// - 上一个播放的动画状态名称
  881. /// </summary>
  882. /// <see cref="lastAnimationState"/>
  883. /// <version>DragonBones 3.0</version>
  884. /// <language>zh_CN</language>
  885. public string lastAnimationName
  886. {
  887. get { return this._lastAnimationState != null ? this._lastAnimationState.name : ""; }
  888. }
  889. /// <summary>
  890. /// - The name of all animation data
  891. /// </summary>
  892. /// <version>DragonBones 4.5</version>
  893. /// <language>en_US</language>
  894. /// <summary>
  895. /// - 所有动画数据的名称
  896. /// </summary>
  897. /// <version>DragonBones 4.5</version>
  898. /// <language>zh_CN</language>
  899. public List<string> animationNames
  900. {
  901. get { return this._animationNames; }
  902. }
  903. /// <summary>
  904. /// - All animation data.
  905. /// </summary>
  906. /// <version>DragonBones 4.5</version>
  907. /// <language>en_US</language>
  908. /// <summary>
  909. /// - 所有的动画数据。
  910. /// </summary>
  911. /// <version>DragonBones 4.5</version>
  912. /// <language>zh_CN</language>
  913. public Dictionary<string, AnimationData> animations
  914. {
  915. get { return this._animations; }
  916. set
  917. {
  918. if (this._animations == value)
  919. {
  920. return;
  921. }
  922. this._animationNames.Clear();
  923. this._animations.Clear();
  924. foreach (var k in value)
  925. {
  926. this._animationNames.Add(k.Key);
  927. this._animations[k.Key] = value[k.Key];
  928. }
  929. }
  930. }
  931. /// <summary>
  932. /// - An AnimationConfig instance that can be used quickly.
  933. /// </summary>
  934. /// <see cref="DragonBones.AnimationConfig"/>
  935. /// <version>DragonBones 5.0</version>
  936. /// <language>en_US</language>
  937. /// <summary>
  938. /// - 一个可以快速使用的动画配置实例。
  939. /// </summary>
  940. /// <see cref="DragonBones.AnimationConfig"/>
  941. /// <version>DragonBones 5.0</version>
  942. /// <language>zh_CN</language>
  943. public AnimationConfig animationConfig
  944. {
  945. get
  946. {
  947. this._animationConfig.Clear();
  948. return this._animationConfig;
  949. }
  950. }
  951. /// <summary>
  952. /// - The last playing animation state
  953. /// </summary>
  954. /// <see cref="DragonBones.AnimationState"/>
  955. /// <version>DragonBones 3.0</version>
  956. /// <language>en_US</language>
  957. /// <summary>
  958. /// - 上一个播放的动画状态
  959. /// </summary>
  960. /// <see cref="DragonBones.AnimationState"/>
  961. /// <version>DragonBones 3.0</version>
  962. /// <language>zh_CN</language>
  963. public AnimationState lastAnimationState
  964. {
  965. get { return this._lastAnimationState; }
  966. }
  967. }
  968. }