BaseFactory.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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. /// <internal/>
  27. /// <private/>
  28. public class BuildArmaturePackage
  29. {
  30. public string dataName = "";
  31. public string textureAtlasName = "";
  32. public DragonBonesData data;
  33. public ArmatureData armature;
  34. public SkinData skin;
  35. }
  36. /// <summary>
  37. /// - Base class for the factory that create the armatures. (Typically only one global factory instance is required)
  38. /// The factory instance create armatures by parsed and added DragonBonesData instances and TextureAtlasData instances.
  39. /// Once the data has been parsed, it has been cached in the factory instance and does not need to be parsed again until it is cleared by the factory instance.
  40. /// </summary>
  41. /// <see cref="DragonBones.DragonBonesData"/>
  42. /// <see cref="DragonBones.TextureAtlasData"/>
  43. /// <see cref="DragonBones.ArmatureData"/>
  44. /// <see cref="DragonBones.Armature"/>
  45. /// <version>DragonBones 3.0</version>
  46. /// <language>en_US</language>
  47. /// <summary>
  48. /// - 创建骨架的工厂基类。 (通常只需要一个全局工厂实例)
  49. /// 工厂通过解析并添加的 DragonBonesData 实例和 TextureAtlasData 实例来创建骨架。
  50. /// 当数据被解析过之后,已经添加到工厂中,在没有被工厂清理之前,不需要再次解析。
  51. /// </summary>
  52. /// <see cref="DragonBones.DragonBonesData"/>
  53. /// <see cref="DragonBones.TextureAtlasData"/>
  54. /// <see cref="DragonBones.ArmatureData"/>
  55. /// <see cref="DragonBones.Armature"/>
  56. /// <version>DragonBones 3.0</version>
  57. /// <language>zh_CN</language>
  58. public abstract class BaseFactory
  59. {
  60. /// <private/>
  61. protected static ObjectDataParser _objectParser = null;
  62. /// <private/>
  63. protected static BinaryDataParser _binaryParser = null;
  64. /// <private/>
  65. public bool autoSearch = false;
  66. /// <private/>
  67. protected readonly Dictionary<string, DragonBonesData> _dragonBonesDataMap = new Dictionary<string, DragonBonesData>();
  68. /// <private/>
  69. protected readonly Dictionary<string, List<TextureAtlasData>> _textureAtlasDataMap = new Dictionary<string, List<TextureAtlasData>>();
  70. /// <private/>
  71. public DragonBones _dragonBones = null;
  72. /// <private/>
  73. protected DataParser _dataParser = null;
  74. /// <summary>
  75. /// - Create a factory instance. (typically only one global factory instance is required)
  76. /// </summary>
  77. /// <version>DragonBones 3.0</version>
  78. /// <language>en_US</language>
  79. /// <summary>
  80. /// - 创建一个工厂实例。 (通常只需要一个全局工厂实例)
  81. /// </summary>
  82. /// <version>DragonBones 3.0</version>
  83. /// <language>zh_CN</language>
  84. public BaseFactory(DataParser dataParser = null)
  85. {
  86. if (BaseFactory._objectParser == null)
  87. {
  88. BaseFactory._objectParser = new ObjectDataParser();
  89. }
  90. if (BaseFactory._binaryParser == null)
  91. {
  92. BaseFactory._binaryParser = new BinaryDataParser();
  93. }
  94. this._dataParser = dataParser != null ? dataParser : BaseFactory._objectParser;
  95. }
  96. /// <private/>
  97. protected bool _IsSupportMesh()
  98. {
  99. return true;
  100. }
  101. /// <private/>
  102. protected TextureData _GetTextureData(string textureAtlasName, string textureName)
  103. {
  104. if (this._textureAtlasDataMap.ContainsKey(textureAtlasName))
  105. {
  106. foreach (var textureAtlasData in this._textureAtlasDataMap[textureAtlasName])
  107. {
  108. var textureData = textureAtlasData.GetTexture(textureName);
  109. if (textureData != null)
  110. {
  111. return textureData;
  112. }
  113. }
  114. }
  115. if (this.autoSearch)
  116. {
  117. // Will be search all data, if the autoSearch is true.
  118. foreach (var values in this._textureAtlasDataMap.Values)
  119. {
  120. foreach (var textureAtlasData in values)
  121. {
  122. if (textureAtlasData.autoSearch)
  123. {
  124. var textureData = textureAtlasData.GetTexture(textureName);
  125. if (textureData != null)
  126. {
  127. return textureData;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. return null;
  134. }
  135. /// <private/>
  136. protected bool _FillBuildArmaturePackage(BuildArmaturePackage dataPackage,
  137. string dragonBonesName,
  138. string armatureName,
  139. string skinName,
  140. string textureAtlasName)
  141. {
  142. DragonBonesData dragonBonesData = null;
  143. ArmatureData armatureData = null;
  144. var isAvailableName = !string.IsNullOrEmpty(dragonBonesName);
  145. if (isAvailableName)
  146. {
  147. if (this._dragonBonesDataMap.ContainsKey(dragonBonesName))
  148. {
  149. dragonBonesData = this._dragonBonesDataMap[dragonBonesName];
  150. armatureData = dragonBonesData.GetArmature(armatureName);
  151. }
  152. }
  153. if (armatureData == null && (!isAvailableName || this.autoSearch))
  154. {
  155. // Will be search all data, if do not give a data name or the autoSearch is true.
  156. foreach (var key in this._dragonBonesDataMap.Keys)
  157. {
  158. dragonBonesData = this._dragonBonesDataMap[key];
  159. if (!isAvailableName || dragonBonesData.autoSearch)
  160. {
  161. armatureData = dragonBonesData.GetArmature(armatureName);
  162. if (armatureData != null)
  163. {
  164. dragonBonesName = key;
  165. break;
  166. }
  167. }
  168. }
  169. }
  170. if (armatureData != null)
  171. {
  172. dataPackage.dataName = dragonBonesName;
  173. dataPackage.textureAtlasName = textureAtlasName;
  174. dataPackage.data = dragonBonesData;
  175. dataPackage.armature = armatureData;
  176. dataPackage.skin = null;
  177. if (!string.IsNullOrEmpty(skinName))
  178. {
  179. dataPackage.skin = armatureData.GetSkin(skinName);
  180. if (dataPackage.skin == null && this.autoSearch)
  181. {
  182. foreach (var k in this._dragonBonesDataMap.Keys)
  183. {
  184. var skinDragonBonesData = this._dragonBonesDataMap[k];
  185. var skinArmatureData = skinDragonBonesData.GetArmature(skinName);
  186. if (skinArmatureData != null)
  187. {
  188. dataPackage.skin = skinArmatureData.defaultSkin;
  189. break;
  190. }
  191. }
  192. }
  193. }
  194. if (dataPackage.skin == null)
  195. {
  196. dataPackage.skin = armatureData.defaultSkin;
  197. }
  198. return true;
  199. }
  200. return false;
  201. }
  202. /// <private/>
  203. protected void _BuildBones(BuildArmaturePackage dataPackage, Armature armature)
  204. {
  205. var bones = dataPackage.armature.sortedBones;
  206. for (int i = 0, l = bones.Count; i < l; ++i)
  207. {
  208. var boneData = bones[i];
  209. var bone = BaseObject.BorrowObject<Bone>();
  210. bone.Init(boneData, armature);
  211. }
  212. }
  213. /// <private/>
  214. protected void _BuildSlots(BuildArmaturePackage dataPackage, Armature armature)
  215. {
  216. var currentSkin = dataPackage.skin;
  217. var defaultSkin = dataPackage.armature.defaultSkin;
  218. if (currentSkin == null || defaultSkin == null)
  219. {
  220. return;
  221. }
  222. Dictionary<string, List<DisplayData>> skinSlots = new Dictionary<string, List<DisplayData>>();
  223. foreach (var key in defaultSkin.displays.Keys)
  224. {
  225. var displays = defaultSkin.GetDisplays(key);
  226. skinSlots[key] = displays;
  227. }
  228. if (currentSkin != defaultSkin)
  229. {
  230. foreach (var k in currentSkin.displays.Keys)
  231. {
  232. var displays = currentSkin.GetDisplays(k);
  233. skinSlots[k] = displays;
  234. }
  235. }
  236. foreach (var slotData in dataPackage.armature.sortedSlots)
  237. {
  238. var displayDatas = skinSlots.ContainsKey(slotData.name) ? skinSlots[slotData.name] : null;
  239. var slot = this._BuildSlot(dataPackage, slotData, armature);
  240. slot.rawDisplayDatas = displayDatas;
  241. if (displayDatas != null)
  242. {
  243. var displayList = new List<object>();
  244. for (int i = 0, l = displayDatas.Count; i < l; ++i)
  245. {
  246. var displayData = displayDatas[i];
  247. if (displayData != null)
  248. {
  249. displayList.Add(this._GetSlotDisplay(dataPackage, displayData, null, slot));
  250. }
  251. else
  252. {
  253. displayList.Add(null);
  254. }
  255. }
  256. slot._SetDisplayList(displayList);
  257. }
  258. slot._SetDisplayIndex(slotData.displayIndex, true);
  259. }
  260. }
  261. /// <private/>
  262. protected void _BuildConstraints(BuildArmaturePackage dataPackage, Armature armature)
  263. {
  264. var constraints = dataPackage.armature.constraints;
  265. foreach (var constraintData in constraints.Values)
  266. {
  267. // TODO more constraint type.
  268. var constraint = BaseObject.BorrowObject<IKConstraint>();
  269. constraint.Init(constraintData, armature);
  270. armature._AddConstraint(constraint);
  271. }
  272. }
  273. /// <private/>
  274. protected virtual Armature _BuildChildArmature(BuildArmaturePackage dataPackage, Slot slot, DisplayData displayData)
  275. {
  276. return this.BuildArmature(displayData.path, dataPackage != null ? dataPackage.dataName : "", "", dataPackage != null ? dataPackage.textureAtlasName : "");
  277. }
  278. /// <private/>
  279. protected object _GetSlotDisplay(BuildArmaturePackage dataPackage, DisplayData displayData, DisplayData rawDisplayData, Slot slot)
  280. {
  281. var dataName = dataPackage != null ? dataPackage.dataName : displayData.parent.parent.parent.name;
  282. object display = null;
  283. switch (displayData.type)
  284. {
  285. case DisplayType.Image:
  286. {
  287. var imageDisplayData = displayData as ImageDisplayData;
  288. if (imageDisplayData.texture == null)
  289. {
  290. imageDisplayData.texture = this._GetTextureData(dataName, displayData.path);
  291. }
  292. else if (dataPackage != null && !string.IsNullOrEmpty(dataPackage.textureAtlasName))
  293. {
  294. imageDisplayData.texture = this._GetTextureData(dataPackage.textureAtlasName, displayData.path);
  295. }
  296. if (rawDisplayData != null && rawDisplayData.type == DisplayType.Mesh && this._IsSupportMesh())
  297. {
  298. display = slot.meshDisplay;
  299. }
  300. else
  301. {
  302. display = slot.rawDisplay;
  303. }
  304. }
  305. break;
  306. case DisplayType.Mesh:
  307. {
  308. var meshDisplayData = displayData as MeshDisplayData;
  309. if (meshDisplayData.texture == null)
  310. {
  311. meshDisplayData.texture = this._GetTextureData(dataName, meshDisplayData.path);
  312. }
  313. else if (dataPackage != null && !string.IsNullOrEmpty(dataPackage.textureAtlasName))
  314. {
  315. meshDisplayData.texture = this._GetTextureData(dataPackage.textureAtlasName, meshDisplayData.path);
  316. }
  317. if (this._IsSupportMesh())
  318. {
  319. display = slot.meshDisplay;
  320. }
  321. else
  322. {
  323. display = slot.rawDisplay;
  324. }
  325. }
  326. break;
  327. case DisplayType.Armature:
  328. {
  329. var armatureDisplayData = displayData as ArmatureDisplayData;
  330. var childArmature = this._BuildChildArmature(dataPackage, slot, displayData);
  331. if (childArmature != null)
  332. {
  333. childArmature.inheritAnimation = armatureDisplayData.inheritAnimation;
  334. if (!childArmature.inheritAnimation)
  335. {
  336. var actions = armatureDisplayData.actions.Count > 0 ? armatureDisplayData.actions : childArmature.armatureData.defaultActions;
  337. if (actions.Count > 0)
  338. {
  339. foreach (var action in actions)
  340. {
  341. var eventObject = BaseObject.BorrowObject<EventObject>();
  342. EventObject.ActionDataToInstance(action, eventObject, slot.armature);
  343. eventObject.slot = slot;
  344. slot.armature._BufferAction(eventObject, false);
  345. }
  346. }
  347. else
  348. {
  349. childArmature.animation.Play();
  350. }
  351. }
  352. armatureDisplayData.armature = childArmature.armatureData; //
  353. }
  354. display = childArmature;
  355. }
  356. break;
  357. case DisplayType.BoundingBox:
  358. break;
  359. }
  360. return display;
  361. }
  362. /// <private/>
  363. protected abstract TextureAtlasData _BuildTextureAtlasData(TextureAtlasData textureAtlasData, object textureAtlas);
  364. /// <private/>
  365. protected abstract Armature _BuildArmature(BuildArmaturePackage dataPackage);
  366. /// <private/>
  367. protected abstract Slot _BuildSlot(BuildArmaturePackage dataPackage, SlotData slotData, Armature armature);
  368. /// <summary>
  369. /// - Parse the raw data to a DragonBonesData instance and cache it to the factory.
  370. /// </summary>
  371. /// <param name="rawData">- The raw data.</param>
  372. /// <param name="name">- Specify a cache name for the instance so that the instance can be obtained through this name. (If not set, use the instance name instead)</param>
  373. /// <param name="scale">- Specify a scaling value for all armatures. (Default: 1.0)</param>
  374. /// <returns>DragonBonesData instance</returns>
  375. /// <see cref="GetDragonBonesData()"/>
  376. /// <see cref="AddDragonBonesData()"/>
  377. /// <see cref="RemoveDragonBonesData()"/>
  378. /// <see cref="DragonBones.DragonBonesData"/>
  379. /// <version>DragonBones 4.5</version>
  380. /// <language>en_US</language>
  381. /// <summary>
  382. /// - 将原始数据解析为 DragonBonesData 实例,并缓存到工厂中。
  383. /// </summary>
  384. /// <param name="rawData">- 原始数据。</param>
  385. /// <param name="name">- 为该实例指定一个缓存名称,以便可以通过此名称获取该实例。 (如果未设置,则使用该实例中的名称)</param>
  386. /// <param name="scale">- 为所有的骨架指定一个缩放值。 (默认: 1.0)</param>
  387. /// <returns>DragonBonesData 实例</returns>
  388. /// <see cref="GetDragonBonesData()"/>
  389. /// <see cref="AddDragonBonesData()"/>
  390. /// <see cref="RemoveDragonBonesData()"/>
  391. /// <see cref="DragonBones.DragonBonesData"/>
  392. /// <version>DragonBones 4.5</version>
  393. /// <language>zh_CN</language>
  394. public DragonBonesData ParseDragonBonesData(object rawData, string name = null, float scale = 1.0f)
  395. {
  396. var dataParser = rawData is byte[] ? BaseFactory._binaryParser : this._dataParser;
  397. DragonBonesData dragonBonesData = dataParser.ParseDragonBonesData(rawData, scale);
  398. while (true)
  399. {
  400. var textureAtlasData = this._BuildTextureAtlasData(null, null);
  401. if (dataParser.ParseTextureAtlasData(null, textureAtlasData, scale))
  402. {
  403. this.AddTextureAtlasData(textureAtlasData, name);
  404. }
  405. else
  406. {
  407. textureAtlasData.ReturnToPool();
  408. break;
  409. }
  410. }
  411. if (dragonBonesData != null)
  412. {
  413. this.AddDragonBonesData(dragonBonesData, name);
  414. }
  415. return dragonBonesData;
  416. }
  417. /// <summary>
  418. /// - Parse the raw texture atlas data and the texture atlas object to a TextureAtlasData instance and cache it to the factory.
  419. /// </summary>
  420. /// <param name="rawData">- The raw texture atlas data.</param>
  421. /// <param name="textureAtlas">- The texture atlas object.</param>
  422. /// <param name="name">- Specify a cache name for the instance so that the instance can be obtained through this name. (If not set, use the instance name instead)</param>
  423. /// <param name="scale">- Specify a scaling value for the map set. (Default: 1.0)</param>
  424. /// <returns>TextureAtlasData instance</returns>
  425. /// <see cref="GetTextureAtlasData()"/>
  426. /// <see cref="AddTextureAtlasData()"/>
  427. /// <see cref="RemoveTextureAtlasData()"/>
  428. /// <see cref="DragonBones.TextureAtlasData"/>
  429. /// <version>DragonBones 4.5</version>
  430. /// <language>en_US</language>
  431. /// <summary>
  432. /// - 将原始贴图集数据和贴图集对象解析为 TextureAtlasData 实例,并缓存到工厂中。
  433. /// </summary>
  434. /// <param name="rawData">- 原始贴图集数据。</param>
  435. /// <param name="textureAtlas">- 贴图集对象。</param>
  436. /// <param name="name">- 为该实例指定一个缓存名称,以便可以通过此名称获取该实例。 (如果未设置,则使用该实例中的名称)</param>
  437. /// <param name="scale">- 为贴图集指定一个缩放值。 (默认: 1.0)</param>
  438. /// <returns>TextureAtlasData 实例</returns>
  439. /// <see cref="GetTextureAtlasData()"/>
  440. /// <see cref="AddTextureAtlasData()"/>
  441. /// <see cref="RemoveTextureAtlasData()"/>
  442. /// <see cref="DragonBones.TextureAtlasData"/>
  443. /// <version>DragonBones 4.5</version>
  444. /// <language>zh_CN</language>
  445. public TextureAtlasData ParseTextureAtlasData(Dictionary<string, object> rawData, object textureAtlas, string name = null, float scale = 1.0f)
  446. {
  447. var textureAtlasData = this._BuildTextureAtlasData(null, null);
  448. this._dataParser.ParseTextureAtlasData(rawData, textureAtlasData, scale);
  449. this._BuildTextureAtlasData(textureAtlasData, textureAtlas);
  450. this.AddTextureAtlasData(textureAtlasData, name);
  451. return textureAtlasData;
  452. }
  453. /// <private/>
  454. public void UpdateTextureAtlasData(string name, List<object> textureAtlases)
  455. {
  456. var textureAtlasDatas = this.GetTextureAtlasData(name);
  457. if (textureAtlasDatas != null)
  458. {
  459. for (int i = 0, l = textureAtlasDatas.Count; i < l; ++i)
  460. {
  461. if (i < textureAtlases.Count)
  462. {
  463. this._BuildTextureAtlasData(textureAtlasDatas[i], textureAtlases[i]);
  464. }
  465. }
  466. }
  467. }
  468. /// <summary>
  469. /// - Get a specific DragonBonesData instance.
  470. /// </summary>
  471. /// <param name="name">- The DragonBonesData instance cache name.</param>
  472. /// <returns>DragonBonesData instance</returns>
  473. /// <see cref="ParseDragonBonesData()"/>
  474. /// <see cref="AddDragonBonesData()"/>
  475. /// <see cref="RemoveDragonBonesData()"/>
  476. /// <see cref="DragonBones.DragonBonesData"/>
  477. /// <version>DragonBones 3.0</version>
  478. /// <language>en_US</language>
  479. /// <summary>
  480. /// - 获取特定的 DragonBonesData 实例。
  481. /// </summary>
  482. /// <param name="name">- DragonBonesData 实例的缓存名称。</param>
  483. /// <returns>DragonBonesData 实例</returns>
  484. /// <see cref="ParseDragonBonesData()"/>
  485. /// <see cref="AddDragonBonesData()"/>
  486. /// <see cref="RemoveDragonBonesData()"/>
  487. /// <see cref="DragonBones.DragonBonesData"/>
  488. /// <version>DragonBones 3.0</version>
  489. /// <language>zh_CN</language>
  490. public DragonBonesData GetDragonBonesData(string name)
  491. {
  492. return this._dragonBonesDataMap.ContainsKey(name) ? this._dragonBonesDataMap[name] : null;
  493. }
  494. /// <summary>
  495. /// - Cache a DragonBonesData instance to the factory.
  496. /// </summary>
  497. /// <param name="data">- The DragonBonesData instance.</param>
  498. /// <param name="name">- Specify a cache name for the instance so that the instance can be obtained through this name. (if not set, use the instance name instead)</param>
  499. /// <see cref="ParseDragonBonesData()"/>
  500. /// <see cref="GetDragonBonesData()"/>
  501. /// <see cref="RemoveDragonBonesData()"/>
  502. /// <see cref="DragonBones.DragonBonesData"/>
  503. /// <version>DragonBones 3.0</version>
  504. /// <language>en_US</language>
  505. /// <summary>
  506. /// - 将 DragonBonesData 实例缓存到工厂中。
  507. /// </summary>
  508. /// <param name="data">- DragonBonesData 实例。</param>
  509. /// <param name="name">- 为该实例指定一个缓存名称,以便可以通过此名称获取该实例。 (如果未设置,则使用该实例中的名称)</param>
  510. /// <see cref="ParseDragonBonesData()"/>
  511. /// <see cref="GetDragonBonesData()"/>
  512. /// <see cref="RemoveDragonBonesData()"/>
  513. /// <see cref="DragonBones.DragonBonesData"/>
  514. /// <version>DragonBones 3.0</version>
  515. /// <language>zh_CN</language>
  516. public void AddDragonBonesData(DragonBonesData data, string name = null)
  517. {
  518. name = !string.IsNullOrEmpty(name) ? name : data.name;
  519. if (this._dragonBonesDataMap.ContainsKey(name))
  520. {
  521. if (this._dragonBonesDataMap[name] == data)
  522. {
  523. return;
  524. }
  525. Helper.Assert(false, "Can not add same name data: " + name);
  526. return;
  527. }
  528. this._dragonBonesDataMap[name] = data;
  529. }
  530. /// <summary>
  531. /// - Remove a DragonBonesData instance.
  532. /// </summary>
  533. /// <param name="name">- The DragonBonesData instance cache name.</param>
  534. /// <param name="disposeData">- Whether to dispose data. (Default: true)</param>
  535. /// <see cref="ParseDragonBonesData()"/>
  536. /// <see cref="GetDragonBonesData()"/>
  537. /// <see cref="AddDragonBonesData()"/>
  538. /// <see cref="DragonBones.DragonBonesData"/>
  539. /// <version>DragonBones 3.0</version>
  540. /// <language>en_US</language>
  541. /// <summary>
  542. /// - 移除 DragonBonesData 实例。
  543. /// </summary>
  544. /// <param name="name">- DragonBonesData 实例缓存名称。</param>
  545. /// <param name="disposeData">- 是否释放数据。 (默认: true)</param>
  546. /// <see cref="ParseDragonBonesData()"/>
  547. /// <see cref="GetDragonBonesData()"/>
  548. /// <see cref="AddDragonBonesData()"/>
  549. /// <see cref="DragonBones.DragonBonesData"/>
  550. /// <version>DragonBones 3.0</version>
  551. /// <language>zh_CN</language>
  552. public virtual void RemoveDragonBonesData(string name, bool disposeData = true)
  553. {
  554. if (this._dragonBonesDataMap.ContainsKey(name))
  555. {
  556. if (disposeData)
  557. {
  558. this._dragonBones.BufferObject(this._dragonBonesDataMap[name]);
  559. }
  560. this._dragonBonesDataMap.Remove(name);
  561. }
  562. }
  563. /// <summary>
  564. /// - Get a list of specific TextureAtlasData instances.
  565. /// </summary>
  566. /// <param name="name">- The TextureAtlasData cahce name.</param>
  567. /// <see cref="ParseTextureAtlasData()"/>
  568. /// <see cref="AddTextureAtlasData()"/>
  569. /// <see cref="RemoveTextureAtlasData()"/>
  570. /// <see cref="DragonBones.TextureAtlasData"/>
  571. /// <version>DragonBones 3.0</version>
  572. /// <language>en_US</language>
  573. /// <summary>
  574. /// - 获取特定的 TextureAtlasData 实例列表。
  575. /// </summary>
  576. /// <param name="name">- TextureAtlasData 实例缓存名称。</param>
  577. /// <see cref="ParseTextureAtlasData()"/>
  578. /// <see cref="AddTextureAtlasData()"/>
  579. /// <see cref="RemoveTextureAtlasData()"/>
  580. /// <see cref="DragonBones.TextureAtlasData"/>
  581. /// <version>DragonBones 3.0</version>
  582. /// <language>zh_CN</language>
  583. public List<TextureAtlasData> GetTextureAtlasData(string name)
  584. {
  585. return this._textureAtlasDataMap.ContainsKey(name) ? this._textureAtlasDataMap[name] : null;
  586. }
  587. /// <summary>
  588. /// - Cache a TextureAtlasData instance to the factory.
  589. /// </summary>
  590. /// <param name="data">- The TextureAtlasData instance.</param>
  591. /// <param name="name">- Specify a cache name for the instance so that the instance can be obtained through this name. (if not set, use the instance name instead)</param>
  592. /// <see cref="ParseTextureAtlasData()"/>
  593. /// <see cref="GetTextureAtlasData()"/>
  594. /// <see cref="RemoveTextureAtlasData()"/>
  595. /// <see cref="DragonBones.TextureAtlasData"/>
  596. /// <version>DragonBones 3.0</version>
  597. /// <language>en_US</language>
  598. /// <summary>
  599. /// - 将 TextureAtlasData 实例缓存到工厂中。
  600. /// </summary>
  601. /// <param name="data">- TextureAtlasData 实例。</param>
  602. /// <param name="name">- 为该实例指定一个缓存名称,以便可以通过此名称获取该实例。 (如果未设置,则使用该实例中的名称)</param>
  603. /// <see cref="ParseTextureAtlasData()"/>
  604. /// <see cref="GetTextureAtlasData()"/>
  605. /// <see cref="RemoveTextureAtlasData()"/>
  606. /// <see cref="DragonBones.TextureAtlasData"/>
  607. /// <version>DragonBones 3.0</version>
  608. /// <language>zh_CN</language>
  609. public void AddTextureAtlasData(TextureAtlasData data, string name = null)
  610. {
  611. name = !string.IsNullOrEmpty(name) ? name : data.name;
  612. var textureAtlasList = (this._textureAtlasDataMap.ContainsKey(name)) ?
  613. this._textureAtlasDataMap[name] :
  614. (this._textureAtlasDataMap[name] = new List<TextureAtlasData>());
  615. if (!textureAtlasList.Contains(data))
  616. {
  617. textureAtlasList.Add(data);
  618. }
  619. }
  620. /// <summary>
  621. /// - Remove a TextureAtlasData instance.
  622. /// </summary>
  623. /// <param name="name">- The TextureAtlasData instance cache name.</param>
  624. /// <param name="disposeData">- Whether to dispose data.</param>
  625. /// <see cref="ParseTextureAtlasData()"/>
  626. /// <see cref="GetTextureAtlasData()"/>
  627. /// <see cref="AddTextureAtlasData()"/>
  628. /// <see cref="DragonBones.TextureAtlasData"/>
  629. /// <version>DragonBones 3.0</version>
  630. /// <language>en_US</language>
  631. /// <summary>
  632. /// - 移除 TextureAtlasData 实例。
  633. /// </summary>
  634. /// <param name="name">- TextureAtlasData 实例的缓存名称。</param>
  635. /// <param name="disposeData">- 是否释放数据。</param>
  636. /// <see cref="ParseTextureAtlasData()"/>
  637. /// <see cref="GetTextureAtlasData()"/>
  638. /// <see cref="AddTextureAtlasData()"/>
  639. /// <see cref="DragonBones.TextureAtlasData"/>
  640. /// <version>DragonBones 3.0</version>
  641. /// <language>zh_CN</language>
  642. public virtual void RemoveTextureAtlasData(string name, bool disposeData = true)
  643. {
  644. if (this._textureAtlasDataMap.ContainsKey(name))
  645. {
  646. var textureAtlasDataList = this._textureAtlasDataMap[name];
  647. if (disposeData)
  648. {
  649. foreach (var textureAtlasData in textureAtlasDataList)
  650. {
  651. this._dragonBones.BufferObject(textureAtlasData);
  652. }
  653. }
  654. this._textureAtlasDataMap.Remove(name);
  655. }
  656. }
  657. /// <summary>
  658. /// - Get a specific armature data.
  659. /// </summary>
  660. /// <param name="name">- The armature data name.</param>
  661. /// <param name="dragonBonesName">- The cached name for DragonbonesData instance.</param>
  662. /// <see cref="DragonBones.ArmatureData"/>
  663. /// <version>DragonBones 5.1</version>
  664. /// <language>en_US</language>
  665. /// <summary>
  666. /// - 获取特定的骨架数据。
  667. /// </summary>
  668. /// <param name="name">- 骨架数据名称。</param>
  669. /// <param name="dragonBonesName">- DragonBonesData 实例的缓存名称。</param>
  670. /// <see cref="DragonBones.ArmatureData"/>
  671. /// <version>DragonBones 5.1</version>
  672. /// <language>zh_CN</language>
  673. public virtual ArmatureData GetArmatureData(string name, string dragonBonesName = "")
  674. {
  675. var dataPackage = new BuildArmaturePackage();
  676. if (!this._FillBuildArmaturePackage(dataPackage, dragonBonesName, name, "", ""))
  677. {
  678. return null;
  679. }
  680. return dataPackage.armature;
  681. }
  682. /// <summary>
  683. /// - Clear all cached DragonBonesData instances and TextureAtlasData instances.
  684. /// </summary>
  685. /// <param name="disposeData">- Whether to dispose data.</param>
  686. /// <version>DragonBones 4.5</version>
  687. /// <language>en_US</language>
  688. /// <summary>
  689. /// - 清除缓存的所有 DragonBonesData 实例和 TextureAtlasData 实例。
  690. /// </summary>
  691. /// <param name="disposeData">- 是否释放数据。</param>
  692. /// <version>DragonBones 4.5</version>
  693. /// <language>zh_CN</language>
  694. public virtual void Clear(bool disposeData = true)
  695. {
  696. if (disposeData)
  697. {
  698. foreach (var dragonBoneData in this._dragonBonesDataMap.Values)
  699. {
  700. this._dragonBones.BufferObject(dragonBoneData);
  701. }
  702. foreach (var textureAtlasDatas in this._textureAtlasDataMap.Values)
  703. {
  704. foreach (var textureAtlasData in textureAtlasDatas)
  705. {
  706. this._dragonBones.BufferObject(textureAtlasData);
  707. }
  708. }
  709. }
  710. _dragonBonesDataMap.Clear();
  711. _textureAtlasDataMap.Clear();
  712. }
  713. /// <summary>
  714. /// - Create a armature from cached DragonBonesData instances and TextureAtlasData instances.
  715. /// Note that when the created armature that is no longer in use, you need to explicitly dispose {@link #dragonBones.Armature#dispose()}.
  716. /// </summary>
  717. /// <param name="armatureName">- The armature data name.</param>
  718. /// <param name="dragonBonesName">- The cached name of the DragonBonesData instance. (If not set, all DragonBonesData instances are retrieved, and when multiple DragonBonesData instances contain a the same name armature data, it may not be possible to accurately create a specific armature)</param>
  719. /// <param name="skinName">- The skin name, you can set a different ArmatureData name to share it's skin data. (If not set, use the default skin data)</param>
  720. /// <returns>The armature.</returns>
  721. /// <example>
  722. /// TypeScript style, for reference only.
  723. /// <pre>
  724. /// let armature = factory.buildArmature("armatureName", "dragonBonesName");
  725. /// armature.clock = factory.clock;
  726. /// </pre>
  727. /// </example>
  728. /// <see cref="DragonBones.DragonBonesData"/>
  729. /// <see cref="DragonBones.ArmatureData"/>
  730. /// <version>DragonBones 3.0</version>
  731. /// <language>en_US</language>
  732. /// <summary>
  733. /// - 通过缓存的 DragonBonesData 实例和 TextureAtlasData 实例创建一个骨架。
  734. /// 注意,创建的骨架不再使用时,需要显式释放 {@link #dragonBones.Armature#dispose()}。
  735. /// </summary>
  736. /// <param name="armatureName">- 骨架数据名称。</param>
  737. /// <param name="dragonBonesName">- DragonBonesData 实例的缓存名称。 (如果未设置,将检索所有的 DragonBonesData 实例,当多个 DragonBonesData 实例中包含同名的骨架数据时,可能无法准确的创建出特定的骨架)</param>
  738. /// <param name="skinName">- 皮肤名称,可以设置一个其他骨架数据名称来共享其皮肤数据。(如果未设置,则使用默认的皮肤数据)</param>
  739. /// <returns>骨架。</returns>
  740. /// <example>
  741. /// TypeScript 风格,仅供参考。
  742. /// <pre>
  743. /// let armature = factory.buildArmature("armatureName", "dragonBonesName");
  744. /// armature.clock = factory.clock;
  745. /// </pre>
  746. /// </example>
  747. /// <see cref="DragonBones.DragonBonesData"/>
  748. /// <see cref="DragonBones.ArmatureData"/>
  749. /// <version>DragonBones 3.0</version>
  750. /// <language>zh_CN</language>
  751. public virtual Armature BuildArmature(string armatureName, string dragonBonesName = "", string skinName = null, string textureAtlasName = null)
  752. {
  753. var dataPackage = new BuildArmaturePackage();
  754. if (!this._FillBuildArmaturePackage(dataPackage, dragonBonesName, armatureName, skinName, textureAtlasName))
  755. {
  756. Helper.Assert(false, "No armature data: " + armatureName + ", " + (dragonBonesName != "" ? dragonBonesName : ""));
  757. return null;
  758. }
  759. var armature = this._BuildArmature(dataPackage);
  760. this._BuildBones(dataPackage, armature);
  761. this._BuildSlots(dataPackage, armature);
  762. this._BuildConstraints(dataPackage, armature);
  763. armature.InvalidUpdate(null, true);
  764. // Update armature pose.
  765. armature.AdvanceTime(0.0f);
  766. return armature;
  767. }
  768. /// <private/>
  769. public virtual void ReplaceDisplay(Slot slot, DisplayData displayData, int displayIndex = -1)
  770. {
  771. if (displayIndex < 0)
  772. {
  773. displayIndex = slot.displayIndex;
  774. }
  775. if (displayIndex < 0)
  776. {
  777. displayIndex = 0;
  778. }
  779. slot.ReplaceDisplayData(displayData, displayIndex);
  780. var displayList = slot.displayList; // Copy.
  781. if (displayList.Count <= displayIndex)
  782. {
  783. displayList.ResizeList(displayIndex + 1);
  784. for (int i = 0, l = displayList.Count; i < l; ++i)
  785. {
  786. // Clean undefined.
  787. displayList[i] = null;
  788. }
  789. }
  790. if (displayData != null)
  791. {
  792. var rawDisplayDatas = slot.rawDisplayDatas;
  793. DisplayData rawDisplayData = null;
  794. if (rawDisplayDatas != null)
  795. {
  796. if (displayIndex < rawDisplayDatas.Count)
  797. {
  798. rawDisplayData = rawDisplayDatas[displayIndex];
  799. }
  800. }
  801. displayList[displayIndex] = this._GetSlotDisplay(null, displayData, rawDisplayData, slot);
  802. }
  803. else
  804. {
  805. displayList[displayIndex] = null;
  806. }
  807. slot.displayList = displayList;
  808. }
  809. /// <summary>
  810. /// - Replaces the current display data for a particular slot with a specific display data.
  811. /// Specify display data with "dragonBonesName/armatureName/slotName/displayName".
  812. /// </summary>
  813. /// <param name="dragonBonesName">- The DragonBonesData instance cache name.</param>
  814. /// <param name="armatureName">- The armature data name.</param>
  815. /// <param name="slotName">- The slot data name.</param>
  816. /// <param name="displayName">- The display data name.</param>
  817. /// <param name="slot">- The slot.</param>
  818. /// <param name="displayIndex">- The index of the display data that is replaced. (If it is not set, replaces the current display data)</param>
  819. /// <example>
  820. /// TypeScript style, for reference only.
  821. /// <pre>
  822. /// let slot = armature.getSlot("weapon");
  823. /// factory.replaceSlotDisplay("dragonBonesName", "armatureName", "slotName", "displayName", slot);
  824. /// </pre>
  825. /// </example>
  826. /// <version>DragonBones 4.5</version>
  827. /// <language>en_US</language>
  828. /// <summary>
  829. /// - 用特定的显示对象数据替换特定插槽当前的显示对象数据。
  830. /// 用 "dragonBonesName/armatureName/slotName/displayName" 指定显示对象数据。
  831. /// </summary>
  832. /// <param name="dragonBonesName">- DragonBonesData 实例的缓存名称。</param>
  833. /// <param name="armatureName">- 骨架数据名称。</param>
  834. /// <param name="slotName">- 插槽数据名称。</param>
  835. /// <param name="displayName">- 显示对象数据名称。</param>
  836. /// <param name="slot">- 插槽。</param>
  837. /// <param name="displayIndex">- 被替换的显示对象数据的索引。 (如果未设置,则替换当前的显示对象数据)</param>
  838. /// <example>
  839. /// TypeScript 风格,仅供参考。
  840. /// <pre>
  841. /// let slot = armature.getSlot("weapon");
  842. /// factory.replaceSlotDisplay("dragonBonesName", "armatureName", "slotName", "displayName", slot);
  843. /// </pre>
  844. /// </example>
  845. /// <version>DragonBones 4.5</version>
  846. /// <language>zh_CN</language>
  847. public bool ReplaceSlotDisplay(string dragonBonesName,
  848. string armatureName,
  849. string slotName,
  850. string displayName,
  851. Slot slot, int displayIndex = -1)
  852. {
  853. var armatureData = this.GetArmatureData(armatureName, dragonBonesName);
  854. if (armatureData == null || armatureData.defaultSkin == null)
  855. {
  856. return false;
  857. }
  858. var displayData = armatureData.defaultSkin.GetDisplay(slotName, displayName);
  859. if (displayData == null)
  860. {
  861. return false;
  862. }
  863. this.ReplaceDisplay(slot, displayData, displayIndex);
  864. return true;
  865. }
  866. /// <private/>
  867. public bool ReplaceSlotDisplayList(string dragonBonesName, string armatureName, string slotName, Slot slot)
  868. {
  869. var armatureData = this.GetArmatureData(armatureName, dragonBonesName);
  870. if (armatureData == null || armatureData.defaultSkin == null)
  871. {
  872. return false;
  873. }
  874. var displays = armatureData.defaultSkin.GetDisplays(slotName);
  875. if (displays == null)
  876. {
  877. return false;
  878. }
  879. var displayIndex = 0;
  880. // for (const displayData of displays)
  881. for (int i = 0, l = displays.Count; i < l; ++i)
  882. {
  883. var displayData = displays[i];
  884. this.ReplaceDisplay(slot, displayData, displayIndex++);
  885. }
  886. return true;
  887. }
  888. /// <summary>
  889. /// - Share specific skin data with specific armature.
  890. /// </summary>
  891. /// <param name="armature">- The armature.</param>
  892. /// <param name="skin">- The skin data.</param>
  893. /// <param name="isOverride">- Whether it completely override the original skin. (Default: false)</param>
  894. /// <param name="exclude">- A list of slot names that do not need to be replace.</param>
  895. /// <example>
  896. /// TypeScript style, for reference only.
  897. /// <pre>
  898. /// let armatureA = factory.buildArmature("armatureA", "dragonBonesA");
  899. /// let armatureDataB = factory.getArmatureData("armatureB", "dragonBonesB");
  900. /// if (armatureDataB && armatureDataB.defaultSkin) {
  901. /// factory.replaceSkin(armatureA, armatureDataB.defaultSkin, false, ["arm_l", "weapon_l"]);
  902. /// }
  903. /// </pre>
  904. /// </example>
  905. /// <see cref="DragonBones.Armature"/>
  906. /// <see cref="DragonBones.SkinData"/>
  907. /// <version>DragonBones 5.6</version>
  908. /// <language>en_US</language>
  909. /// <summary>
  910. /// - 将特定的皮肤数据共享给特定的骨架使用。
  911. /// </summary>
  912. /// <param name="armature">- 骨架。</param>
  913. /// <param name="skin">- 皮肤数据。</param>
  914. /// <param name="isOverride">- 是否完全覆盖原来的皮肤。 (默认: false)</param>
  915. /// <param name="exclude">- 不需要被替换的插槽名称列表。</param>
  916. /// <example>
  917. /// TypeScript 风格,仅供参考。
  918. /// <pre>
  919. /// let armatureA = factory.buildArmature("armatureA", "dragonBonesA");
  920. /// let armatureDataB = factory.getArmatureData("armatureB", "dragonBonesB");
  921. /// if (armatureDataB && armatureDataB.defaultSkin) {
  922. /// factory.replaceSkin(armatureA, armatureDataB.defaultSkin, false, ["arm_l", "weapon_l"]);
  923. /// }
  924. /// </pre>
  925. /// </example>
  926. /// <see cref="DragonBones.Armature"/>
  927. /// <see cref="DragonBones.SkinData"/>
  928. /// <version>DragonBones 5.6</version>
  929. /// <language>zh_CN</language>
  930. public bool ReplaceSkin(Armature armature, SkinData skin, bool isOverride = false, List<string> exclude = null)
  931. {
  932. var success = false;
  933. var defaultSkin = skin.parent.defaultSkin;
  934. foreach (var slot in armature.GetSlots())
  935. {
  936. if (exclude != null && exclude.Contains(slot.name))
  937. {
  938. continue;
  939. }
  940. var displays = skin.GetDisplays(slot.name);
  941. if (displays == null)
  942. {
  943. if (defaultSkin != null && skin != defaultSkin)
  944. {
  945. displays = defaultSkin.GetDisplays(slot.name);
  946. }
  947. if (displays == null)
  948. {
  949. if (isOverride)
  950. {
  951. slot.rawDisplayDatas = null;
  952. slot.displayList.Clear(); //
  953. }
  954. continue;
  955. }
  956. }
  957. var displayCount = displays.Count;
  958. var displayList = slot.displayList; // Copy.
  959. displayList.ResizeList(displayCount); // Modify displayList length.
  960. for (int i = 0, l = displayCount; i < l; ++i)
  961. {
  962. var displayData = displays[i];
  963. if (displayData != null)
  964. {
  965. displayList[i] = this._GetSlotDisplay(null, displayData, null, slot);
  966. }
  967. else
  968. {
  969. displayList[i] = null;
  970. }
  971. }
  972. success = true;
  973. slot.rawDisplayDatas = displays;
  974. slot.displayList = displayList;
  975. }
  976. return success;
  977. }
  978. /// <summary>
  979. /// - Replaces the existing animation data for a specific armature with the animation data for the specific armature data.
  980. /// This enables you to make a armature template so that other armature without animations can share it's animations.
  981. /// </summary>
  982. /// <param name="armature">- The armtaure.</param>
  983. /// <param name="armatureData">- The armature data.</param>
  984. /// <param name="isOverride">- Whether to completely overwrite the original animation. (Default: false)</param>
  985. /// <example>
  986. /// TypeScript style, for reference only.
  987. /// <pre>
  988. /// let armatureA = factory.buildArmature("armatureA", "dragonBonesA");
  989. /// let armatureDataB = factory.getArmatureData("armatureB", "dragonBonesB");
  990. /// if (armatureDataB) {
  991. /// factory.replaceAnimation(armatureA, armatureDataB);
  992. /// }
  993. /// </pre>
  994. /// </example>
  995. /// <see cref="DragonBones.Armature"/>
  996. /// <see cref="DragonBones.ArmatureData"/>
  997. /// <version>DragonBones 5.6</version>
  998. /// <language>en_US</language>
  999. /// <summary>
  1000. /// - 用特定骨架数据的动画数据替换特定骨架现有的动画数据。
  1001. /// 这样就能实现制作一个骨架动画模板,让其他没有制作动画的骨架共享该动画。
  1002. /// </summary>
  1003. /// <param name="armature">- 骨架。</param>
  1004. /// <param name="armatureData">- 骨架数据。</param>
  1005. /// <param name="isOverride">- 是否完全覆盖原来的动画。(默认: false)</param>
  1006. /// <example>
  1007. /// TypeScript 风格,仅供参考。
  1008. /// <pre>
  1009. /// let armatureA = factory.buildArmature("armatureA", "dragonBonesA");
  1010. /// let armatureDataB = factory.getArmatureData("armatureB", "dragonBonesB");
  1011. /// if (armatureDataB) {
  1012. /// factory.replaceAnimation(armatureA, armatureDataB);
  1013. /// }
  1014. /// </pre>
  1015. /// </example>
  1016. /// <see cref="DragonBones.Armature"/>
  1017. /// <see cref="DragonBones.ArmatureData"/>
  1018. /// <version>DragonBones 5.6</version>
  1019. /// <language>zh_CN</language>
  1020. public bool ReplaceAnimation(Armature armature,
  1021. ArmatureData armatureData,
  1022. bool isOverride = true)
  1023. {
  1024. var skinData = armatureData.defaultSkin;
  1025. if (skinData == null)
  1026. {
  1027. return false;
  1028. }
  1029. if (isOverride)
  1030. {
  1031. armature.animation.animations = armatureData.animations;
  1032. }
  1033. else
  1034. {
  1035. var rawAnimations = armature.animation.animations;
  1036. Dictionary<string, AnimationData> animations = new Dictionary<string, AnimationData>();
  1037. foreach (var k in rawAnimations.Keys)
  1038. {
  1039. animations[k] = rawAnimations[k];
  1040. }
  1041. foreach (var k in armatureData.animations.Keys)
  1042. {
  1043. animations[k] = armatureData.animations[k];
  1044. }
  1045. armature.animation.animations = animations;
  1046. }
  1047. foreach (var slot in armature.GetSlots())
  1048. {
  1049. var index = 0;
  1050. foreach (var display in slot.displayList)
  1051. {
  1052. if (display is Armature)
  1053. {
  1054. var displayDatas = skinData.GetDisplays(slot.name);
  1055. if (displayDatas != null && index < displayDatas.Count)
  1056. {
  1057. var displayData = displayDatas[index];
  1058. if (displayData != null && displayData.type == DisplayType.Armature)
  1059. {
  1060. var childArmatureData = this.GetArmatureData(displayData.path, displayData.parent.parent.parent.name);
  1061. if (childArmatureData != null)
  1062. {
  1063. this.ReplaceAnimation(display as Armature, childArmatureData, isOverride);
  1064. }
  1065. }
  1066. }
  1067. }
  1068. }
  1069. }
  1070. return true;
  1071. }
  1072. /// <private/>
  1073. public Dictionary<string, DragonBonesData> GetAllDragonBonesData()
  1074. {
  1075. return this._dragonBonesDataMap;
  1076. }
  1077. /// <private/>
  1078. public Dictionary<string, List<TextureAtlasData>> GetAllTextureAtlasData()
  1079. {
  1080. return this._textureAtlasDataMap;
  1081. }
  1082. /// <summary>
  1083. /// - An Worldclock instance updated by engine.
  1084. /// </summary>
  1085. /// <version>DragonBones 5.7</version>
  1086. /// <language>en_US</language>
  1087. /// <summary>
  1088. /// - 由引擎驱动的 WorldClock 实例。
  1089. /// </summary>
  1090. /// <version>DragonBones 5.7</version>
  1091. /// <language>zh_CN</language>
  1092. public WorldClock clock
  1093. {
  1094. get { return this._dragonBones.clock; }
  1095. }
  1096. /// <summary>
  1097. /// - Deprecated, please refer to {@link #replaceSkin}.
  1098. /// </summary>
  1099. /// <language>en_US</language>
  1100. /// <summary>
  1101. /// - 已废弃,请参考 {@link #replaceSkin}。
  1102. /// </summary>
  1103. /// <language>zh_CN</language>
  1104. [System.Obsolete("")]
  1105. public bool ChangeSkin(Armature armature, SkinData skin, List<string> exclude = null)
  1106. {
  1107. return ReplaceSkin(armature, skin, false, exclude);
  1108. }
  1109. }
  1110. }