PickJsonDataWindow.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 UnityEngine;
  24. using UnityEditor;
  25. using System.Collections.Generic;
  26. namespace DragonBones
  27. {
  28. /// <summary>
  29. /// JSON数据拾取,为UnityArmatureComponent创建UnityDragonBonesData
  30. /// </summary>
  31. public class PickJsonDataWindow : EditorWindow
  32. {
  33. private const string ObjectSelectorUpdated = "ObjectSelectorUpdated";
  34. private const string ObjectSelectorClosed = "ObjectSelectorClosed";
  35. //private const string PickFileFileter = "_ske t:TextAsset";
  36. private const string PickFileFileter = "t:TextAsset";
  37. private UnityArmatureComponent _armatureComp;
  38. private TextAsset _dragonBoneJSONData;
  39. private bool _isOpenPickWindow = false;
  40. private int _controlID;
  41. //
  42. public static void OpenWindow(UnityArmatureComponent armatureComp)
  43. {
  44. if (armatureComp == null)
  45. {
  46. return;
  47. }
  48. //
  49. var win = GetWindow<PickJsonDataWindow>();
  50. win._armatureComp = armatureComp;
  51. }
  52. private void OnDestroy()
  53. {
  54. _armatureComp = null;
  55. _dragonBoneJSONData = null;
  56. _isOpenPickWindow = false;
  57. _controlID = 0;
  58. }
  59. private void Awake()
  60. {
  61. _dragonBoneJSONData = null;
  62. _isOpenPickWindow = false;
  63. _controlID = 0;
  64. this.maxSize = Vector2.one;
  65. this.minSize = Vector2.one;
  66. }
  67. private void OnGUI()
  68. {
  69. ShowPickJsonWindow();
  70. string commandName = Event.current.commandName;
  71. if (commandName == ObjectSelectorUpdated)
  72. {
  73. //更新JSON数据
  74. _dragonBoneJSONData = EditorGUIUtility.GetObjectPickerObject() as TextAsset;
  75. }
  76. else if (commandName == ObjectSelectorClosed)
  77. {
  78. //根据选择的JSON数据设置DragonBonesData
  79. //这里不仅创建了DragonBonesData,并且更新了场景中的显示对象
  80. //UnityEditor.ChangeDragonBonesData(_armatureComp, _dragonBoneJSONData);
  81. if (_dragonBoneJSONData != null)
  82. {
  83. SetUnityDragonBonesData();
  84. }
  85. Repaint();
  86. this.Close();
  87. }
  88. }
  89. private void ShowPickJsonWindow()
  90. {
  91. if (_isOpenPickWindow)
  92. {
  93. return;
  94. }
  95. _controlID = EditorGUIUtility.GetControlID(FocusType.Passive);
  96. EditorGUIUtility.ShowObjectPicker<TextAsset>(null, false, PickFileFileter, _controlID);
  97. _isOpenPickWindow = true;
  98. }
  99. private void SetUnityDragonBonesData()
  100. {
  101. List<string> textureAtlasJSONs = new List<string>();
  102. UnityEditor.GetTextureAtlasConfigs(textureAtlasJSONs, AssetDatabase.GetAssetPath(_dragonBoneJSONData.GetInstanceID()));
  103. UnityDragonBonesData.TextureAtlas[] textureAtlas = UnityEditor.GetTextureAtlasByJSONs(textureAtlasJSONs);
  104. UnityDragonBonesData data = UnityEditor.CreateUnityDragonBonesData(_dragonBoneJSONData, textureAtlas);
  105. _armatureComp.unityData = data;
  106. }
  107. }
  108. }