DragonBonesLoader.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #if FAIRYGUI_DRAGONBONES
  2. using UnityEngine;
  3. using DragonBones;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public partial class GLoader3D : GObject
  10. {
  11. UnityArmatureComponent _armatureComponent;
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. /// <value></value>
  16. public UnityArmatureComponent armatureComponent
  17. {
  18. get { return _armatureComponent; }
  19. }
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. /// <param name="asset"></param>
  24. /// <param name="width"></param>
  25. /// <param name="height"></param>
  26. /// <param name="anchor"></param>
  27. public void SetDragonBones(DragonBonesData asset, int width, int height, Vector2 anchor)
  28. {
  29. if (_armatureComponent != null)
  30. FreeDragonBones();
  31. _armatureComponent = UnityFactory.factory.BuildArmatureComponent(asset.armatureNames[0], asset.name, null, asset.name);
  32. _armatureComponent.gameObject.transform.localScale = new Vector3(100, 100, 1);
  33. _armatureComponent.gameObject.transform.localPosition = new Vector3(anchor.x, -anchor.y, 0);
  34. SetWrapTarget(_armatureComponent.gameObject, true, width, height);
  35. var ct = _armatureComponent.color;
  36. ct.redMultiplier = _color.r;
  37. ct.greenMultiplier = _color.g;
  38. ct.blueMultiplier = _color.b;
  39. _armatureComponent.color = ct;
  40. OnChangeDragonBones(null);
  41. }
  42. protected void LoadDragonBones()
  43. {
  44. DragonBonesData asset = (DragonBonesData)_contentItem.skeletonAsset;
  45. if (asset == null)
  46. return;
  47. SetDragonBones(asset, _contentItem.width, _contentItem.height, _contentItem.skeletonAnchor);
  48. }
  49. protected void OnChangeDragonBones(string propertyName)
  50. {
  51. if (_armatureComponent == null)
  52. return;
  53. if (propertyName == "color")
  54. {
  55. var ct = _armatureComponent.color;
  56. ct.redMultiplier = _color.r;
  57. ct.greenMultiplier = _color.g;
  58. ct.blueMultiplier = _color.b;
  59. _armatureComponent.color = ct;
  60. return;
  61. }
  62. if (!string.IsNullOrEmpty(_animationName))
  63. {
  64. if (_playing)
  65. _armatureComponent.animation.Play(_animationName, _loop ? 0 : 1);
  66. else
  67. _armatureComponent.animation.GotoAndStopByFrame(_animationName, (uint)_frame);
  68. }
  69. else
  70. _armatureComponent.animation.Reset();
  71. }
  72. protected void FreeDragonBones()
  73. {
  74. if (_armatureComponent != null)
  75. {
  76. _armatureComponent.Dispose();
  77. if (Application.isPlaying)
  78. GameObject.Destroy(_armatureComponent.gameObject);
  79. else
  80. GameObject.DestroyImmediate(_armatureComponent.gameObject);
  81. }
  82. }
  83. protected void OnUpdateDragonBones(UpdateContext context)
  84. {
  85. if (_armatureComponent != null)
  86. {
  87. var ct = _armatureComponent.color;
  88. ct.alphaMultiplier = context.alpha * _content.alpha;
  89. _armatureComponent.color = ct;
  90. }
  91. }
  92. }
  93. }
  94. #endif