123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- using System.Collections.Generic;
- using UnityEngine;
- namespace DragonBones
- {
- /**
- * @inheritDoc
- */
- public class UnityArmatureComponent : UnityEventDispatcher<EventObject>, IArmatureProxy
- {
- private bool _disposeProxy = true;
- /**
- * @private
- */
- internal Armature _armature = null;
- /**
- * @private
- */
- public void _onClear()
- {
- if (_armature != null)
- {
- _armature = null;
- if (_disposeProxy)
- {
- #if UNITY_EDITOR
- Object.DestroyImmediate(gameObject);
- #else
- Object.Destroy(gameObject);
- #endif
- }
- }
- }
- /**
- * @inheritDoc
- */
- public void Dispose(bool disposeProxy = true)
- {
- _disposeProxy = disposeProxy;
- if (_armature != null)
- {
- _armature.Dispose();
- }
- }
- /**
- * @language zh_CN
- * 获取骨架。
- * @readOnly
- * @see DragonBones.Armature
- * @version DragonBones 4.5
- */
- public Armature armature
- {
- get { return _armature; }
- }
- /**
- * @language zh_CN
- * 获取动画控制器。
- * @readOnly
- * @see DragonBones.Animation
- * @version DragonBones 4.5
- */
- new public Animation animation
- {
- get { return _armature != null ? _armature.animation : null; }
- }
-
- /**
- * @private
- */
- public TextAsset dragonBonesJSON = null;
- /**
- * @private
- */
- public List<string> textureAtlasJSON = null;
- /**
- * @private
- */
- public string armatureName = null;
- /**
- * @private
- */
- public string animationName = null;
- [SerializeField]
- protected string _sortingLayerName = "Default";
- public string sortingLayerName
- {
- get { return _sortingLayerName; }
- set
- {
- if (_sortingLayerName == value)
- {
- //return;
- }
- _sortingLayerName = value;
- foreach (var render in GetComponentsInChildren<Renderer>(true))
- {
- render.sortingLayerName = value;
- }
- }
- }
- [SerializeField]
- protected int _sortingOrder = 0;
- public int sortingOrder
- {
- get { return _sortingOrder; }
- set
- {
- if (_sortingOrder == value)
- {
- //return;
- }
- _sortingOrder = value;
- foreach (var render in GetComponentsInChildren<Renderer>(true))
- {
- render.sortingOrder = value;
- }
- }
- }
- [SerializeField]
- protected float _zSpace = 0.0f;
- public float zSpace
- {
- get { return _zSpace; }
- set
- {
- if (value < 0.0f || float.IsNaN(value))
- {
- value = 0.0f;
- }
- if (_zSpace == value)
- {
- return;
- }
- _zSpace = value;
- foreach (var slot in _armature.GetSlots())
- {
- var display = slot.display as GameObject;
- if (display != null)
- {
- display.transform.localPosition = new Vector3(display.transform.localPosition.x, display.transform.localPosition.y, -slot._zOrder * (_zSpace + 0.001f));
- }
- }
- }
- }
- /**
- * @private
- */
- void Awake()
- {
- LoadData(dragonBonesJSON, textureAtlasJSON);
- if (!string.IsNullOrEmpty(armatureName))
- {
- UnityFactory.factory.BuildArmatureComponent(armatureName, null, null, null, gameObject);
- }
- if (_armature != null)
- {
- sortingLayerName = sortingLayerName;
- sortingOrder = sortingOrder;
- if (!string.IsNullOrEmpty(animationName))
- {
- _armature.animation.Play(animationName);
- }
- }
- }
- /**
- * @private
- */
- void OnDestroy()
- {
- if (_armature != null)
- {
- var armature = _armature;
- _armature = null;
- armature.Dispose();
- }
-
- _disposeProxy = true;
- _armature = null;
- }
- /**
- * @private
- */
- public DragonBonesData LoadData(TextAsset dragonBonesJSON, List<string> textureAtlasJSON)
- {
- DragonBonesData dragonBonesData = null;
- if (dragonBonesJSON != null)
- {
- dragonBonesData = UnityFactory.factory.LoadDragonBonesData(dragonBonesJSON);
- if (dragonBonesData != null && textureAtlasJSON != null)
- {
- foreach (var eachJSON in textureAtlasJSON)
- {
- UnityFactory.factory.LoadTextureAtlasData(eachJSON);
- }
- }
- }
- return dragonBonesData;
- }
- }
- }
|