UnityArmatureComponent.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace DragonBones
  4. {
  5. /**
  6. * @inheritDoc
  7. */
  8. public class UnityArmatureComponent : UnityEventDispatcher<EventObject>, IArmatureProxy
  9. {
  10. private bool _disposeProxy = true;
  11. /**
  12. * @private
  13. */
  14. internal Armature _armature = null;
  15. /**
  16. * @private
  17. */
  18. public void _onClear()
  19. {
  20. if (_armature != null)
  21. {
  22. _armature = null;
  23. if (_disposeProxy)
  24. {
  25. #if UNITY_EDITOR
  26. Object.DestroyImmediate(gameObject);
  27. #else
  28. Object.Destroy(gameObject);
  29. #endif
  30. }
  31. }
  32. }
  33. /**
  34. * @inheritDoc
  35. */
  36. public void Dispose(bool disposeProxy = true)
  37. {
  38. _disposeProxy = disposeProxy;
  39. if (_armature != null)
  40. {
  41. _armature.Dispose();
  42. }
  43. }
  44. /**
  45. * @language zh_CN
  46. * 获取骨架。
  47. * @readOnly
  48. * @see DragonBones.Armature
  49. * @version DragonBones 4.5
  50. */
  51. public Armature armature
  52. {
  53. get { return _armature; }
  54. }
  55. /**
  56. * @language zh_CN
  57. * 获取动画控制器。
  58. * @readOnly
  59. * @see DragonBones.Animation
  60. * @version DragonBones 4.5
  61. */
  62. new public Animation animation
  63. {
  64. get { return _armature != null ? _armature.animation : null; }
  65. }
  66. /**
  67. * @private
  68. */
  69. public TextAsset dragonBonesJSON = null;
  70. /**
  71. * @private
  72. */
  73. public List<string> textureAtlasJSON = null;
  74. /**
  75. * @private
  76. */
  77. public string armatureName = null;
  78. /**
  79. * @private
  80. */
  81. public string animationName = null;
  82. [SerializeField]
  83. protected string _sortingLayerName = "Default";
  84. public string sortingLayerName
  85. {
  86. get { return _sortingLayerName; }
  87. set
  88. {
  89. if (_sortingLayerName == value)
  90. {
  91. //return;
  92. }
  93. _sortingLayerName = value;
  94. foreach (var render in GetComponentsInChildren<Renderer>(true))
  95. {
  96. render.sortingLayerName = value;
  97. }
  98. }
  99. }
  100. [SerializeField]
  101. protected int _sortingOrder = 0;
  102. public int sortingOrder
  103. {
  104. get { return _sortingOrder; }
  105. set
  106. {
  107. if (_sortingOrder == value)
  108. {
  109. //return;
  110. }
  111. _sortingOrder = value;
  112. foreach (var render in GetComponentsInChildren<Renderer>(true))
  113. {
  114. render.sortingOrder = value;
  115. }
  116. }
  117. }
  118. [SerializeField]
  119. protected float _zSpace = 0.0f;
  120. public float zSpace
  121. {
  122. get { return _zSpace; }
  123. set
  124. {
  125. if (value < 0.0f || float.IsNaN(value))
  126. {
  127. value = 0.0f;
  128. }
  129. if (_zSpace == value)
  130. {
  131. return;
  132. }
  133. _zSpace = value;
  134. foreach (var slot in _armature.GetSlots())
  135. {
  136. var display = slot.display as GameObject;
  137. if (display != null)
  138. {
  139. display.transform.localPosition = new Vector3(display.transform.localPosition.x, display.transform.localPosition.y, -slot._zOrder * (_zSpace + 0.001f));
  140. }
  141. }
  142. }
  143. }
  144. /**
  145. * @private
  146. */
  147. void Awake()
  148. {
  149. LoadData(dragonBonesJSON, textureAtlasJSON);
  150. if (!string.IsNullOrEmpty(armatureName))
  151. {
  152. UnityFactory.factory.BuildArmatureComponent(armatureName, null, null, null, gameObject);
  153. }
  154. if (_armature != null)
  155. {
  156. sortingLayerName = sortingLayerName;
  157. sortingOrder = sortingOrder;
  158. if (!string.IsNullOrEmpty(animationName))
  159. {
  160. _armature.animation.Play(animationName);
  161. }
  162. }
  163. }
  164. /**
  165. * @private
  166. */
  167. void OnDestroy()
  168. {
  169. if (_armature != null)
  170. {
  171. var armature = _armature;
  172. _armature = null;
  173. armature.Dispose();
  174. }
  175. _disposeProxy = true;
  176. _armature = null;
  177. }
  178. /**
  179. * @private
  180. */
  181. public DragonBonesData LoadData(TextAsset dragonBonesJSON, List<string> textureAtlasJSON)
  182. {
  183. DragonBonesData dragonBonesData = null;
  184. if (dragonBonesJSON != null)
  185. {
  186. dragonBonesData = UnityFactory.factory.LoadDragonBonesData(dragonBonesJSON);
  187. if (dragonBonesData != null && textureAtlasJSON != null)
  188. {
  189. foreach (var eachJSON in textureAtlasJSON)
  190. {
  191. UnityFactory.factory.LoadTextureAtlasData(eachJSON);
  192. }
  193. }
  194. }
  195. return dragonBonesData;
  196. }
  197. }
  198. }