UnityArmatureComponent.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. using UnityEngine;
  25. #if UNITY_EDITOR
  26. using UnityEditor;
  27. #endif
  28. namespace DragonBones
  29. {
  30. /// <summary>
  31. /// The slots sorting mode
  32. /// </summary>
  33. /// <version>DragonBones 4.5</version>
  34. /// <language>en_US</language>
  35. ///
  36. /// <summary>
  37. /// 插槽排序模式
  38. /// </summary>
  39. /// <version>DragonBones 4.5</version>
  40. /// <language>zh_CN</language>
  41. public enum SortingMode
  42. {
  43. /// <summary>
  44. /// Sort by Z values
  45. /// </summary>
  46. /// <version>DragonBones 4.5</version>
  47. /// <language>en_US</language>
  48. ///
  49. /// <summary>
  50. /// 按照插槽显示对象的z值排序
  51. /// </summary>
  52. /// <version>DragonBones 4.5</version>
  53. /// <language>zh_CN</language>
  54. SortByZ,
  55. /// <summary>
  56. /// Renderer's order within a sorting layer.
  57. /// </summary>
  58. /// <version>DragonBones 4.5</version>
  59. /// <language>en_US</language>
  60. ///
  61. /// <summary>
  62. /// 在同一层sorting layer中插槽按照sortingOrder排序
  63. /// </summary>
  64. /// <version>DragonBones 4.5</version>
  65. /// <language>zh_CN</language>
  66. SortByOrder
  67. }
  68. ///<inheritDoc/>
  69. [ExecuteInEditMode, DisallowMultipleComponent]
  70. public class UnityArmatureComponent : DragonBoneEventDispatcher, IArmatureProxy
  71. {
  72. public const int ORDER_SPACE = 10;
  73. /// <private/>
  74. public UnityDragonBonesData unityData = null;
  75. /// <private/>
  76. public string armatureName = null;
  77. /// <summary>
  78. /// Is it the UGUI model?
  79. /// </summary>
  80. /// <version>DragonBones 4.5</version>
  81. /// <language>en_US</language>
  82. ///
  83. /// <summary>
  84. /// 是否是UGUI模式
  85. /// </summary>
  86. /// <version>DragonBones 4.5</version>
  87. /// <language>zh_CN</language>
  88. public bool isUGUI = false;
  89. public bool debugDraw = false;
  90. internal readonly ColorTransform _colorTransform = new ColorTransform();
  91. /// <private/>
  92. public string animationName = null;
  93. /// <private/>
  94. private bool _disposeProxy = true;
  95. /// <private/>
  96. internal Armature _armature = null;
  97. [Tooltip("0 : Loop")]
  98. [Range(0, 100)]
  99. [SerializeField]
  100. protected int _playTimes = 0;
  101. [Range(-2f, 2f)]
  102. [SerializeField]
  103. protected float _timeScale = 1.0f;
  104. [SerializeField]
  105. internal SortingMode _sortingMode = SortingMode.SortByZ;
  106. [SerializeField]
  107. internal string _sortingLayerName = "Default";
  108. [SerializeField]
  109. internal int _sortingOrder = 0;
  110. [SerializeField]
  111. internal float _zSpace = 0.0f;
  112. [SerializeField]
  113. protected bool _flipX = false;
  114. [SerializeField]
  115. protected bool _flipY = false;
  116. //default open combineMeshs
  117. [SerializeField]
  118. protected bool _closeCombineMeshs;
  119. private bool _hasSortingGroup = false;
  120. private Material _debugDrawer;
  121. //
  122. internal int _armatureZ;
  123. /// <private/>
  124. public void DBClear()
  125. {
  126. if (this._armature != null)
  127. {
  128. this._armature = null;
  129. if (this._disposeProxy)
  130. {
  131. try
  132. {
  133. var go = gameObject;
  134. UnityFactoryHelper.DestroyUnityObject(gameObject);
  135. }
  136. catch (System.Exception)
  137. {
  138. }
  139. }
  140. }
  141. this.unityData = null;
  142. this.armatureName = null;
  143. this.animationName = null;
  144. this.isUGUI = false;
  145. this.debugDraw = false;
  146. this._disposeProxy = true;
  147. this._armature = null;
  148. this._colorTransform.Identity();
  149. this._sortingMode = SortingMode.SortByZ;
  150. this._sortingLayerName = "Default";
  151. this._sortingOrder = 0;
  152. this._playTimes = 0;
  153. this._timeScale = 1.0f;
  154. this._zSpace = 0.0f;
  155. this._flipX = false;
  156. this._flipY = false;
  157. this._hasSortingGroup = false;
  158. this._debugDrawer = null;
  159. this._armatureZ = 0;
  160. this._closeCombineMeshs = false;
  161. }
  162. ///
  163. public void DBInit(Armature armature)
  164. {
  165. this._armature = armature;
  166. }
  167. public void DBUpdate()
  168. {
  169. }
  170. void CreateLineMaterial()
  171. {
  172. if (!_debugDrawer)
  173. {
  174. // Unity has a built-in shader that is useful for drawing
  175. // simple colored things.
  176. Shader shader = Shader.Find("Hidden/Internal-Colored");
  177. _debugDrawer = new Material(shader);
  178. _debugDrawer.hideFlags = HideFlags.HideAndDontSave;
  179. // Turn on alpha blending
  180. _debugDrawer.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  181. _debugDrawer.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  182. // Turn backface culling off
  183. _debugDrawer.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
  184. // Turn off depth writes
  185. _debugDrawer.SetInt("_ZWrite", 0);
  186. }
  187. }
  188. void OnRenderObject()
  189. {
  190. var drawed = DragonBones.debugDraw || this.debugDraw;
  191. if (drawed)
  192. {
  193. Color boneLineColor = new Color(0.0f, 1.0f, 1.0f, 0.7f);
  194. Color boundingBoxLineColor = new Color(1.0f, 0.0f, 1.0f, 1.0f);
  195. CreateLineMaterial();
  196. // Apply the line material
  197. _debugDrawer.SetPass(0);
  198. GL.PushMatrix();
  199. // Set transformation matrix for drawing to
  200. // match our transform
  201. GL.MultMatrix(transform.localToWorldMatrix);
  202. //
  203. var bones = this._armature.GetBones();
  204. var offset = 0.02f;
  205. // draw bone line
  206. for (int i = 0; i < bones.Count; i++)
  207. {
  208. var bone = bones[i];
  209. var boneLength = System.Math.Max(bone.boneData.length, offset);
  210. var startPos = new Vector3(bone.globalTransformMatrix.tx, bone.globalTransformMatrix.ty, 0.0f);
  211. var endPos = new Vector3(bone.globalTransformMatrix.a * boneLength, bone.globalTransformMatrix.b * boneLength, 0.0f) + startPos;
  212. var torwardDir = (startPos - endPos).normalized;
  213. var leftStartPos = Quaternion.AngleAxis(90, Vector3.forward) * torwardDir * offset + startPos;
  214. var rightStartPos = Quaternion.AngleAxis(-90, Vector3.forward) * torwardDir * offset + startPos;
  215. var newStartPos = startPos + torwardDir * offset;
  216. //
  217. GL.Begin(GL.LINES);
  218. GL.Color(boneLineColor);
  219. GL.Vertex(leftStartPos);
  220. GL.Vertex(rightStartPos);
  221. GL.End();
  222. GL.Begin(GL.LINES);
  223. GL.Color(boneLineColor);
  224. GL.Vertex(newStartPos);
  225. GL.Vertex(endPos);
  226. GL.End();
  227. }
  228. // draw boundingBox
  229. Point result = new Point();
  230. var slots = this._armature.GetSlots();
  231. for (int i = 0; i < slots.Count; i++)
  232. {
  233. var slot = slots[i] as UnitySlot;
  234. var boundingBoxData = slot.boundingBoxData;
  235. if (boundingBoxData == null)
  236. {
  237. continue;
  238. }
  239. var bone = slot.parent;
  240. slot.UpdateTransformAndMatrix();
  241. slot.UpdateGlobalTransform();
  242. var tx = slot.globalTransformMatrix.tx;
  243. var ty = slot.globalTransformMatrix.ty;
  244. var boundingBoxWidth = boundingBoxData.width;
  245. var boundingBoxHeight = boundingBoxData.height;
  246. //
  247. switch (boundingBoxData.type)
  248. {
  249. case BoundingBoxType.Rectangle:
  250. {
  251. //
  252. #if UNITY_5_6_OR_NEWER
  253. GL.Begin(GL.LINE_STRIP);
  254. #else
  255. GL.Begin(GL.LINES);
  256. #endif
  257. GL.Color(boundingBoxLineColor);
  258. var leftTopPos = new Vector3(tx - boundingBoxWidth * 0.5f, ty + boundingBoxHeight * 0.5f, 0.0f);
  259. var leftBottomPos = new Vector3(tx - boundingBoxWidth * 0.5f, ty - boundingBoxHeight * 0.5f, 0.0f);
  260. var rightTopPos = new Vector3(tx + boundingBoxWidth * 0.5f, ty + boundingBoxHeight * 0.5f, 0.0f);
  261. var rightBottomPos = new Vector3(tx + boundingBoxWidth * 0.5f, ty - boundingBoxHeight * 0.5f, 0.0f);
  262. GL.Vertex(leftTopPos);
  263. GL.Vertex(rightTopPos);
  264. GL.Vertex(rightBottomPos);
  265. GL.Vertex(leftBottomPos);
  266. GL.Vertex(leftTopPos);
  267. GL.End();
  268. }
  269. break;
  270. case BoundingBoxType.Ellipse:
  271. {
  272. }
  273. break;
  274. case BoundingBoxType.Polygon:
  275. {
  276. var vertices = (boundingBoxData as PolygonBoundingBoxData).vertices;
  277. #if UNITY_5_6_OR_NEWER
  278. GL.Begin(GL.LINE_STRIP);
  279. #else
  280. GL.Begin(GL.LINES);
  281. #endif
  282. GL.Color(boundingBoxLineColor);
  283. for (var j = 0; j < vertices.Count; j += 2)
  284. {
  285. slot.globalTransformMatrix.TransformPoint(vertices[j], vertices[j + 1], result);
  286. GL.Vertex3(result.x, result.y, 0.0f);
  287. }
  288. slot.globalTransformMatrix.TransformPoint(vertices[0], vertices[1], result);
  289. GL.Vertex3(result.x, result.y, 0.0f);
  290. GL.End();
  291. }
  292. break;
  293. default:
  294. break;
  295. }
  296. }
  297. GL.PopMatrix();
  298. }
  299. }
  300. /// <inheritDoc/>
  301. public void Dispose(bool disposeProxy = true)
  302. {
  303. _disposeProxy = disposeProxy;
  304. if (_armature != null)
  305. {
  306. _armature.Dispose();
  307. }
  308. }
  309. /// <summary>
  310. /// Get the Armature.
  311. /// </summary>
  312. /// <readOnly/>
  313. /// <version>DragonBones 4.5</version>
  314. /// <language>en_US</language>
  315. /// <summary>
  316. /// 获取骨架。
  317. /// </summary>
  318. /// <readOnly/>
  319. /// <version>DragonBones 4.5</version>
  320. /// <language>zh_CN</language>
  321. public Armature armature
  322. {
  323. get { return _armature; }
  324. }
  325. /// <summary>
  326. /// Get the animation player
  327. /// </summary>
  328. /// <readOnly/>
  329. /// <version>DragonBones 4.5</version>
  330. /// <language>en_US</language>
  331. /// <summary>
  332. /// 获取动画播放器。
  333. /// </summary>
  334. /// <readOnly/>
  335. /// <version>DragonBones 4.5</version>
  336. /// <language>zh_CN</language>
  337. public new AnimationDB animation
  338. {
  339. get { return _armature != null ? _armature.animation : null; }
  340. }
  341. /// <summary>
  342. /// The slots sorting mode
  343. /// </summary>
  344. /// <version>DragonBones 4.5</version>
  345. /// <language>en_US</language>
  346. ///
  347. /// <summary>
  348. /// 插槽排序模式
  349. /// </summary>
  350. /// <version>DragonBones 4.5</version>
  351. /// <language>zh_CN</language>
  352. public SortingMode sortingMode
  353. {
  354. get { return _sortingMode; }
  355. set
  356. {
  357. if (_sortingMode == value)
  358. {
  359. return;
  360. }
  361. #if UNITY_5_6_OR_NEWER
  362. var isWarning = false;
  363. #else
  364. var isWarning = value == SortingMode.SortByOrder;
  365. #endif
  366. if (isWarning)
  367. {
  368. LogHelper.LogWarning("SortingMode.SortByOrder is userd by Unity 5.6 or highter only.");
  369. return;
  370. }
  371. _sortingMode = value;
  372. //
  373. #if UNITY_5_6_OR_NEWER
  374. if (_sortingMode == SortingMode.SortByOrder)
  375. {
  376. _sortingGroup = GetComponent<UnityEngine.Rendering.SortingGroup>();
  377. if (_sortingGroup == null)
  378. {
  379. _sortingGroup = gameObject.AddComponent<UnityEngine.Rendering.SortingGroup>();
  380. }
  381. }
  382. else
  383. {
  384. _sortingGroup = GetComponent<UnityEngine.Rendering.SortingGroup>();
  385. if (_sortingGroup != null)
  386. {
  387. DestroyImmediate(_sortingGroup);
  388. }
  389. }
  390. #endif
  391. _UpdateSlotsSorting();
  392. }
  393. }
  394. /// <summary>
  395. /// Name of the Renderer's sorting layer.
  396. /// </summary>
  397. /// <version>DragonBones 4.5</version>
  398. /// <language>en_US</language>
  399. ///
  400. /// <summary>
  401. /// sorting layer名称。
  402. /// </summary>
  403. /// <version>DragonBones 4.5</version>
  404. /// <language>zh_CN</language>
  405. public string sortingLayerName
  406. {
  407. get { return _sortingLayerName; }
  408. set
  409. {
  410. if (_sortingLayerName == value)
  411. {
  412. //return;
  413. }
  414. _sortingLayerName = value;
  415. _UpdateSlotsSorting();
  416. }
  417. }
  418. /// <summary>
  419. /// Renderer's order within a sorting layer.
  420. /// </summary>
  421. /// <version>DragonBones 4.5</version>
  422. /// <language>en_US</language>
  423. ///
  424. /// <summary>
  425. /// 插槽按照sortingOrder在同一层sorting layer中排序
  426. /// </summary>
  427. /// <version>DragonBones 4.5</version>
  428. /// <language>zh_CN</language>
  429. public int sortingOrder
  430. {
  431. get { return _sortingOrder; }
  432. set
  433. {
  434. if (_sortingOrder == value)
  435. {
  436. //return;
  437. }
  438. _sortingOrder = value;
  439. _UpdateSlotsSorting();
  440. }
  441. }
  442. /// <summary>
  443. /// The Z axis spacing of slot display objects
  444. /// </summary>
  445. /// <version>DragonBones 4.5</version>
  446. /// <language>zh_CN</language>
  447. ///
  448. /// <summary>
  449. /// 插槽显示对象的z轴间隔
  450. /// </summary>
  451. /// <version>DragonBones 4.5</version>
  452. /// <language>zh_CN</language>
  453. public float zSpace
  454. {
  455. get { return _zSpace; }
  456. set
  457. {
  458. if (value < 0.0f || float.IsNaN(value))
  459. {
  460. value = 0.0f;
  461. }
  462. if (_zSpace == value)
  463. {
  464. return;
  465. }
  466. _zSpace = value;
  467. _UpdateSlotsSorting();
  468. }
  469. }
  470. /// <summary>
  471. /// - The armature color.
  472. /// </summary>
  473. /// <version>DragonBones 4.5</version>
  474. /// <language>en_US</language>
  475. ///
  476. /// <summary>
  477. /// - 骨架的颜色。
  478. /// </summary>
  479. /// <version>DragonBones 4.5</version>
  480. /// <language>zh_CN</language>
  481. public ColorTransform color
  482. {
  483. get { return this._colorTransform; }
  484. set
  485. {
  486. this._colorTransform.CopyFrom(value);
  487. foreach (var slot in this._armature.GetSlots())
  488. {
  489. slot._colorDirty = true;
  490. }
  491. }
  492. }
  493. #if UNITY_5_6_OR_NEWER
  494. internal UnityEngine.Rendering.SortingGroup _sortingGroup;
  495. public UnityEngine.Rendering.SortingGroup sortingGroup
  496. {
  497. get { return _sortingGroup; }
  498. }
  499. private void _UpdateSortingGroup()
  500. {
  501. //发现骨架有SortingGroup,那么子骨架也都加上,反之删除
  502. _sortingGroup = GetComponent<UnityEngine.Rendering.SortingGroup>();
  503. if (_sortingGroup != null)
  504. {
  505. _sortingMode = SortingMode.SortByOrder;
  506. _sortingLayerName = _sortingGroup.sortingLayerName;
  507. _sortingOrder = _sortingGroup.sortingOrder;
  508. foreach (UnitySlot slot in _armature.GetSlots())
  509. {
  510. if (slot.childArmature != null)
  511. {
  512. var childArmatureProxy = slot.childArmature.proxy as UnityArmatureComponent;
  513. childArmatureProxy._sortingGroup = childArmatureProxy.GetComponent<UnityEngine.Rendering.SortingGroup>();
  514. if (childArmatureProxy._sortingGroup == null)
  515. {
  516. childArmatureProxy._sortingGroup = childArmatureProxy.gameObject.AddComponent<UnityEngine.Rendering.SortingGroup>();
  517. }
  518. childArmatureProxy._sortingGroup.sortingLayerName = _sortingLayerName;
  519. childArmatureProxy._sortingGroup.sortingOrder = _sortingOrder;
  520. }
  521. }
  522. }
  523. else
  524. {
  525. _sortingMode = SortingMode.SortByZ;
  526. foreach (UnitySlot slot in _armature.GetSlots())
  527. {
  528. if (slot.childArmature != null)
  529. {
  530. var childArmatureProxy = slot.childArmature.proxy as UnityArmatureComponent;
  531. childArmatureProxy._sortingGroup = childArmatureProxy.GetComponent<UnityEngine.Rendering.SortingGroup>();
  532. if (childArmatureProxy._sortingGroup != null)
  533. {
  534. DestroyImmediate(childArmatureProxy._sortingGroup);
  535. }
  536. }
  537. }
  538. }
  539. #if UNITY_EDITOR
  540. EditorUtility.SetDirty(this);
  541. #endif
  542. _UpdateSlotsSorting();
  543. }
  544. #endif
  545. private void _UpdateSlotsSorting()
  546. {
  547. if (_armature == null)
  548. {
  549. return;
  550. }
  551. if (!isUGUI)
  552. {
  553. #if UNITY_5_6_OR_NEWER
  554. if (_sortingGroup)
  555. {
  556. _sortingMode = SortingMode.SortByOrder;
  557. _sortingGroup.sortingLayerName = _sortingLayerName;
  558. _sortingGroup.sortingOrder = _sortingOrder;
  559. #if UNITY_EDITOR
  560. if (!Application.isPlaying)
  561. {
  562. EditorUtility.SetDirty(_sortingGroup);
  563. }
  564. #endif
  565. }
  566. #endif
  567. }
  568. //
  569. foreach (UnitySlot slot in _armature.GetSlots())
  570. {
  571. var display = slot._renderDisplay;
  572. if (display == null)
  573. {
  574. continue;
  575. }
  576. slot._SetZorder(new Vector3(display.transform.localPosition.x, display.transform.localPosition.y, -slot._zOrder * (_zSpace + 0.001f)));
  577. if (slot.childArmature != null)
  578. {
  579. (slot.childArmature.proxy as UnityArmatureComponent)._UpdateSlotsSorting();
  580. }
  581. #if UNITY_EDITOR
  582. if (!Application.isPlaying && slot.meshRenderer != null)
  583. {
  584. EditorUtility.SetDirty(slot.meshRenderer);
  585. }
  586. #endif
  587. }
  588. }
  589. #if UNITY_EDITOR
  590. private bool _IsPrefab()
  591. {
  592. return PrefabUtility.GetCorrespondingObjectFromSource(gameObject) == null
  593. && PrefabUtility.GetCorrespondingObjectFromSource(gameObject) != null;
  594. }
  595. #endif
  596. /// <private/>
  597. void Awake()
  598. {
  599. #if UNITY_EDITOR
  600. if (_IsPrefab())
  601. {
  602. return;
  603. }
  604. #endif
  605. if (unityData != null && unityData.dragonBonesJSON != null && unityData.textureAtlas != null)
  606. {
  607. var dragonBonesData = UnityFactory.factory.LoadData(unityData, isUGUI);
  608. if (dragonBonesData != null && !string.IsNullOrEmpty(armatureName))
  609. {
  610. UnityFactory.factory.BuildArmatureComponent(armatureName, unityData.dataName, null, null, gameObject, isUGUI);
  611. }
  612. }
  613. if (_armature != null)
  614. {
  615. #if UNITY_5_6_OR_NEWER
  616. if (!isUGUI)
  617. {
  618. _sortingGroup = GetComponent<UnityEngine.Rendering.SortingGroup>();
  619. }
  620. #endif
  621. _UpdateSlotsSorting();
  622. _armature.flipX = _flipX;
  623. _armature.flipY = _flipY;
  624. _armature.animation.timeScale = _timeScale;
  625. if (!string.IsNullOrEmpty(animationName))
  626. {
  627. _armature.animation.Play(animationName, _playTimes);
  628. }
  629. }
  630. }
  631. void Start()
  632. {
  633. // this._closeCombineMeshs = true;
  634. //默认开启合并
  635. if (this._closeCombineMeshs)
  636. {
  637. this.CloseCombineMeshs();
  638. }
  639. else
  640. {
  641. this.OpenCombineMeshs();
  642. }
  643. }
  644. void LateUpdate()
  645. {
  646. if (_armature == null)
  647. {
  648. return;
  649. }
  650. _flipX = _armature.flipX;
  651. _flipY = _armature.flipY;
  652. #if UNITY_5_6_OR_NEWER
  653. var hasSortingGroup = GetComponent<UnityEngine.Rendering.SortingGroup>() != null;
  654. if (hasSortingGroup != _hasSortingGroup)
  655. {
  656. _hasSortingGroup = hasSortingGroup;
  657. _UpdateSortingGroup();
  658. }
  659. #endif
  660. }
  661. /// <private/>
  662. void OnDestroy()
  663. {
  664. if (_armature != null)
  665. {
  666. var armature = _armature;
  667. _armature = null;
  668. armature.Dispose();
  669. if (!Application.isPlaying)
  670. {
  671. UnityFactory.factory._dragonBones.AdvanceTime(0.0f);
  672. }
  673. }
  674. _disposeProxy = true;
  675. _armature = null;
  676. }
  677. private void OpenCombineMeshs()
  678. {
  679. if (this.isUGUI)
  680. {
  681. return;
  682. }
  683. //
  684. var cm = gameObject.GetComponent<UnityCombineMeshs>();
  685. if (cm == null)
  686. {
  687. cm = gameObject.AddComponent<UnityCombineMeshs>();
  688. }
  689. //
  690. if (this._armature == null)
  691. {
  692. return;
  693. }
  694. var slots = this._armature.GetSlots();
  695. foreach (var slot in slots)
  696. {
  697. if (slot.childArmature != null)
  698. {
  699. (slot.childArmature.proxy as UnityArmatureComponent).OpenCombineMeshs();
  700. }
  701. }
  702. }
  703. public void CloseCombineMeshs()
  704. {
  705. this._closeCombineMeshs = true;
  706. //
  707. var cm = gameObject.GetComponent<UnityCombineMeshs>();
  708. if (cm != null)
  709. {
  710. DestroyImmediate(cm);
  711. }
  712. if (this._armature == null)
  713. {
  714. return;
  715. }
  716. //
  717. var slots = this._armature.GetSlots();
  718. foreach (var slot in slots)
  719. {
  720. if (slot.childArmature != null)
  721. {
  722. (slot.childArmature.proxy as UnityArmatureComponent).CloseCombineMeshs();
  723. }
  724. }
  725. }
  726. }
  727. }