UnityArmatureEditor.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. using UnityEditor;
  26. using UnityEditorInternal;
  27. using System.Reflection;
  28. using UnityEditor.SceneManagement;
  29. namespace DragonBones
  30. {
  31. [CustomEditor(typeof(UnityArmatureComponent))]
  32. public class UnityArmatureEditor : Editor
  33. {
  34. private long _nowTime = 0;
  35. private float _frameRate = 1.0f / 24.0f;
  36. private int _armatureIndex = -1;
  37. private int _animationIndex = -1;
  38. private int _sortingModeIndex = -1;
  39. private int _sortingLayerIndex = -1;
  40. private List<string> _armatureNames = null;
  41. private List<string> _animationNames = null;
  42. private List<string> _sortingLayerNames = null;
  43. private UnityArmatureComponent _armatureComponent = null;
  44. private SerializedProperty _playTimesPro;
  45. private SerializedProperty _timeScalePro;
  46. private SerializedProperty _flipXPro;
  47. private SerializedProperty _flipYPro;
  48. private SerializedProperty _closeCombineMeshsPro;
  49. private string[] _sortingMode = new string[] { SortingMode.SortByZ.ToString(), SortingMode.SortByOrder.ToString() };
  50. void ClearUp()
  51. {
  52. this._armatureIndex = -1;
  53. this._animationIndex = -1;
  54. // this._sortingModeIndex = -1;
  55. // this._sortingLayerIndex = -1;
  56. this._armatureNames = null;
  57. this._animationNames = null;
  58. // this._sortingLayerNames = null;
  59. }
  60. void OnDisable()
  61. {
  62. }
  63. void OnEnable()
  64. {
  65. this._armatureComponent = target as UnityArmatureComponent;
  66. if (_IsPrefab())
  67. {
  68. return;
  69. }
  70. //
  71. this._nowTime = System.DateTime.Now.Ticks;
  72. this._sortingModeIndex = (int)this._armatureComponent.sortingMode;
  73. this._sortingLayerNames = _GetSortingLayerNames();
  74. this._sortingLayerIndex = this._sortingLayerNames.IndexOf(this._armatureComponent.sortingLayerName);
  75. this._playTimesPro = serializedObject.FindProperty("_playTimes");
  76. this._timeScalePro = serializedObject.FindProperty("_timeScale");
  77. this._flipXPro = serializedObject.FindProperty("_flipX");
  78. this._flipYPro = serializedObject.FindProperty("_flipY");
  79. this._closeCombineMeshsPro = serializedObject.FindProperty("_closeCombineMeshs");
  80. // Update armature.
  81. if (!EditorApplication.isPlayingOrWillChangePlaymode &&
  82. _armatureComponent.armature == null &&
  83. _armatureComponent.unityData != null &&
  84. !string.IsNullOrEmpty(_armatureComponent.armatureName))
  85. {
  86. // Clear cache
  87. UnityFactory.factory.Clear(true);
  88. // Unload
  89. EditorUtility.UnloadUnusedAssetsImmediate();
  90. System.GC.Collect();
  91. // Load data.
  92. var dragonBonesData = UnityFactory.factory.LoadData(_armatureComponent.unityData);
  93. // Refresh texture atlas.
  94. UnityFactory.factory.RefreshAllTextureAtlas(_armatureComponent);
  95. // Refresh armature.
  96. UnityEditor.ChangeArmatureData(_armatureComponent, _armatureComponent.armatureName, dragonBonesData.name);
  97. // Refresh texture.
  98. _armatureComponent.armature.InvalidUpdate(null, true);
  99. // Play animation.
  100. if (!string.IsNullOrEmpty(_armatureComponent.animationName))
  101. {
  102. _armatureComponent.animation.Play(_armatureComponent.animationName, _playTimesPro.intValue);
  103. }
  104. }
  105. // Update hideFlags.
  106. if (!EditorApplication.isPlayingOrWillChangePlaymode &&
  107. _armatureComponent.armature != null &&
  108. _armatureComponent.armature.parent != null)
  109. {
  110. _armatureComponent.gameObject.hideFlags = HideFlags.NotEditable;
  111. }
  112. else
  113. {
  114. _armatureComponent.gameObject.hideFlags = HideFlags.None;
  115. }
  116. _UpdateParameters();
  117. }
  118. public override void OnInspectorGUI()
  119. {
  120. if (_IsPrefab())
  121. {
  122. return;
  123. }
  124. serializedObject.Update();
  125. if (_armatureIndex == -1)
  126. {
  127. _UpdateParameters();
  128. }
  129. // DragonBones Data
  130. EditorGUILayout.BeginHorizontal();
  131. _armatureComponent.unityData = EditorGUILayout.ObjectField("DragonBones Data", _armatureComponent.unityData, typeof(UnityDragonBonesData), false) as UnityDragonBonesData;
  132. var created = false;
  133. if (_armatureComponent.unityData != null)
  134. {
  135. if (_armatureComponent.armature == null)
  136. {
  137. if (GUILayout.Button("Create"))
  138. {
  139. created = true;
  140. }
  141. }
  142. else
  143. {
  144. if (GUILayout.Button("Reload"))
  145. {
  146. if (EditorUtility.DisplayDialog("DragonBones Alert", "Are you sure you want to reload data", "Yes", "No"))
  147. {
  148. created = true;
  149. }
  150. }
  151. }
  152. }
  153. else
  154. {
  155. //create UnityDragonBonesData by a json data
  156. if (GUILayout.Button("JSON"))
  157. {
  158. PickJsonDataWindow.OpenWindow(_armatureComponent);
  159. }
  160. }
  161. if (created)
  162. {
  163. //clear cache
  164. UnityFactory.factory.Clear(true);
  165. ClearUp();
  166. _armatureComponent.animationName = null;
  167. if (UnityEditor.ChangeDragonBonesData(_armatureComponent, _armatureComponent.unityData.dragonBonesJSON))
  168. {
  169. _UpdateParameters();
  170. }
  171. }
  172. EditorGUILayout.EndHorizontal();
  173. EditorGUILayout.Space();
  174. if (_armatureComponent.armature != null)
  175. {
  176. var dragonBonesData = _armatureComponent.armature.armatureData.parent;
  177. // Armature
  178. if (UnityFactory.factory.GetAllDragonBonesData().ContainsValue(dragonBonesData) && _armatureNames != null)
  179. {
  180. var armatureIndex = EditorGUILayout.Popup("Armature", _armatureIndex, _armatureNames.ToArray());
  181. if (_armatureIndex != armatureIndex)
  182. {
  183. _armatureIndex = armatureIndex;
  184. var armatureName = _armatureNames[_armatureIndex];
  185. UnityEditor.ChangeArmatureData(_armatureComponent, armatureName, dragonBonesData.name);
  186. _UpdateParameters();
  187. _armatureComponent.gameObject.name = armatureName;
  188. MarkSceneDirty();
  189. }
  190. }
  191. // Animation
  192. if (_animationNames != null && _animationNames.Count > 0)
  193. {
  194. EditorGUILayout.BeginHorizontal();
  195. List<string> anims = new List<string>(_animationNames);
  196. anims.Insert(0, "<None>");
  197. var animationIndex = EditorGUILayout.Popup("Animation", _animationIndex + 1, anims.ToArray()) - 1;
  198. if (animationIndex != _animationIndex)
  199. {
  200. _animationIndex = animationIndex;
  201. if (animationIndex >= 0)
  202. {
  203. _armatureComponent.animationName = _animationNames[animationIndex];
  204. var animationData = _armatureComponent.animation.animations[_armatureComponent.animationName];
  205. _armatureComponent.animation.Play(_armatureComponent.animationName, _playTimesPro.intValue);
  206. _UpdateParameters();
  207. }
  208. else
  209. {
  210. _armatureComponent.animationName = null;
  211. _playTimesPro.intValue = 0;
  212. _armatureComponent.animation.Stop();
  213. }
  214. MarkSceneDirty();
  215. }
  216. if (_animationIndex >= 0)
  217. {
  218. if (_armatureComponent.animation.isPlaying)
  219. {
  220. if (GUILayout.Button("Stop"))
  221. {
  222. _armatureComponent.animation.Stop();
  223. }
  224. }
  225. else
  226. {
  227. if (GUILayout.Button("Play"))
  228. {
  229. _armatureComponent.animation.Play(null, _playTimesPro.intValue);
  230. }
  231. }
  232. }
  233. EditorGUILayout.EndHorizontal();
  234. //playTimes
  235. EditorGUILayout.BeginHorizontal();
  236. var playTimes = _playTimesPro.intValue;
  237. EditorGUILayout.PropertyField(_playTimesPro, false);
  238. if (playTimes != _playTimesPro.intValue)
  239. {
  240. if (!string.IsNullOrEmpty(_armatureComponent.animationName))
  241. {
  242. _armatureComponent.animation.Reset();
  243. _armatureComponent.animation.Play(_armatureComponent.animationName, _playTimesPro.intValue);
  244. }
  245. }
  246. EditorGUILayout.EndHorizontal();
  247. // TimeScale
  248. var timeScale = _timeScalePro.floatValue;
  249. EditorGUILayout.PropertyField(_timeScalePro, false);
  250. if (timeScale != _timeScalePro.floatValue)
  251. {
  252. _armatureComponent.animation.timeScale = _timeScalePro.floatValue;
  253. }
  254. }
  255. //
  256. EditorGUILayout.Space();
  257. if (!_armatureComponent.isUGUI)
  258. {
  259. //Sorting Mode
  260. _sortingModeIndex = EditorGUILayout.Popup("Sorting Mode", (int)_armatureComponent.sortingMode, _sortingMode);
  261. if (_sortingModeIndex != (int)_armatureComponent.sortingMode)
  262. {
  263. Undo.RecordObject(_armatureComponent, "Sorting Mode");
  264. _armatureComponent.sortingMode = (SortingMode)_sortingModeIndex;
  265. // 里面return了,没有赋值成功
  266. if (_armatureComponent.sortingMode != (SortingMode)_sortingModeIndex)
  267. {
  268. _sortingModeIndex = (int)_armatureComponent.sortingMode;
  269. }
  270. MarkSceneDirty();
  271. }
  272. // Sorting Layer
  273. _sortingLayerIndex = EditorGUILayout.Popup("Sorting Layer", _sortingLayerIndex, _sortingLayerNames.ToArray());
  274. if (_sortingLayerNames[_sortingLayerIndex] != _armatureComponent.sortingLayerName)
  275. {
  276. Undo.RecordObject(_armatureComponent, "Sorting Layer");
  277. _armatureComponent.sortingLayerName = _sortingLayerNames[_sortingLayerIndex];
  278. MarkSceneDirty();
  279. }
  280. // Sorting Order
  281. var sortingOrder = EditorGUILayout.IntField("Order in Layer", _armatureComponent.sortingOrder);
  282. if (sortingOrder != _armatureComponent.sortingOrder)
  283. {
  284. Undo.RecordObject(_armatureComponent, "Edit Sorting Order");
  285. _armatureComponent.sortingOrder = sortingOrder;
  286. MarkSceneDirty();
  287. }
  288. // ZSpace
  289. EditorGUILayout.BeginHorizontal();
  290. _armatureComponent.zSpace = EditorGUILayout.Slider("Z Space", _armatureComponent.zSpace, 0.0f, 0.5f);
  291. EditorGUILayout.EndHorizontal();
  292. }
  293. EditorGUILayout.Space();
  294. // Flip
  295. EditorGUILayout.BeginHorizontal();
  296. EditorGUILayout.PrefixLabel("Flip");
  297. var flipX = _flipXPro.boolValue;
  298. var flipY = _flipYPro.boolValue;
  299. _flipXPro.boolValue = GUILayout.Toggle(_flipXPro.boolValue, "X", GUILayout.Width(30));
  300. _flipYPro.boolValue = GUILayout.Toggle(_flipYPro.boolValue, "Y", GUILayout.Width(30));
  301. if (flipX != _flipXPro.boolValue || flipY != _flipYPro.boolValue)
  302. {
  303. _armatureComponent.armature.flipX = _flipXPro.boolValue;
  304. _armatureComponent.armature.flipY = _flipYPro.boolValue;
  305. MarkSceneDirty();
  306. }
  307. EditorGUILayout.EndHorizontal();
  308. EditorGUILayout.Space();
  309. }
  310. if (_armatureComponent.armature != null && _armatureComponent.armature.parent == null)
  311. {
  312. if (!Application.isPlaying && !this._armatureComponent.isUGUI)
  313. {
  314. //
  315. var oldValue = this._closeCombineMeshsPro.boolValue;
  316. if (!this._closeCombineMeshsPro.boolValue)
  317. {
  318. this._closeCombineMeshsPro.boolValue = EditorGUILayout.Toggle("CloseCombineMeshs", this._closeCombineMeshsPro.boolValue);
  319. if (GUILayout.Button("Show Slots"))
  320. {
  321. ShowSlotsWindow.OpenWindow(this._armatureComponent);
  322. }
  323. }
  324. if (oldValue != this._closeCombineMeshsPro.boolValue)
  325. {
  326. if (this._closeCombineMeshsPro.boolValue)
  327. {
  328. this._armatureComponent.CloseCombineMeshs();
  329. }
  330. }
  331. }
  332. }
  333. serializedObject.ApplyModifiedProperties();
  334. if (!EditorApplication.isPlayingOrWillChangePlaymode && Selection.activeObject == _armatureComponent.gameObject)
  335. {
  336. EditorUtility.SetDirty(_armatureComponent);
  337. HandleUtility.Repaint();
  338. }
  339. }
  340. void OnSceneGUI()
  341. {
  342. if (!EditorApplication.isPlayingOrWillChangePlaymode && _armatureComponent.armature != null)
  343. {
  344. var dt = (System.DateTime.Now.Ticks - _nowTime) * 0.0000001f;
  345. if (dt >= _frameRate)
  346. {
  347. _armatureComponent.armature.AdvanceTime(dt);
  348. foreach (var slot in _armatureComponent.armature.GetSlots())
  349. {
  350. if (slot.childArmature != null)
  351. {
  352. slot.childArmature.AdvanceTime(dt);
  353. }
  354. }
  355. //
  356. _nowTime = System.DateTime.Now.Ticks;
  357. }
  358. }
  359. }
  360. private void _UpdateParameters()
  361. {
  362. if (_armatureComponent.armature != null)
  363. {
  364. _frameRate = 1.0f / (float)_armatureComponent.armature.armatureData.frameRate;
  365. if (_armatureComponent.armature.armatureData.parent != null)
  366. {
  367. _armatureNames = _armatureComponent.armature.armatureData.parent.armatureNames;
  368. _animationNames = _armatureComponent.animation.animationNames;
  369. _armatureIndex = _armatureNames.IndexOf(_armatureComponent.armature.name);
  370. //
  371. if (!string.IsNullOrEmpty(_armatureComponent.animationName))
  372. {
  373. _animationIndex = _animationNames.IndexOf(_armatureComponent.animationName);
  374. }
  375. }
  376. else
  377. {
  378. _armatureNames = null;
  379. _animationNames = null;
  380. _armatureIndex = -1;
  381. _animationIndex = -1;
  382. }
  383. }
  384. else
  385. {
  386. _armatureNames = null;
  387. _animationNames = null;
  388. _armatureIndex = -1;
  389. _animationIndex = -1;
  390. }
  391. }
  392. private bool _IsPrefab()
  393. {
  394. return PrefabUtility.GetCorrespondingObjectFromSource(_armatureComponent.gameObject) == null
  395. && PrefabUtility.GetCorrespondingObjectFromSource(_armatureComponent.gameObject) != null;
  396. }
  397. private List<string> _GetSortingLayerNames()
  398. {
  399. var internalEditorUtilityType = typeof(InternalEditorUtility);
  400. var sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
  401. return new List<string>(sortingLayersProperty.GetValue(null, new object[0]) as string[]);
  402. }
  403. private void MarkSceneDirty()
  404. {
  405. EditorUtility.SetDirty(_armatureComponent);
  406. //
  407. if (!Application.isPlaying && !_IsPrefab())
  408. {
  409. EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  410. }
  411. }
  412. }
  413. }