AssetProcess.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 System.Collections.Generic;
  25. using UnityEditor;
  26. using System.IO;
  27. namespace DragonBones
  28. {
  29. public class AssetProcess : AssetPostprocessor
  30. {
  31. [System.Serializable]
  32. struct SubTextureClass
  33. {
  34. public string name;
  35. public float x, y, width, height, frameX, frameY, frameWidth, frameHeight;
  36. }
  37. [System.Serializable]
  38. class TextureDataClass
  39. {
  40. public string name=null;
  41. public string imagePath=null;
  42. public int width=0,height=0;
  43. public List<SubTextureClass> SubTexture=null;
  44. }
  45. public static void OnPostprocessAllAssets(string[]imported,string[] deletedAssets,string[] movedAssets,string[]movedFromAssetPaths)
  46. {
  47. if (imported.Length == 0)
  48. {
  49. return;
  50. }
  51. var atlasPaths = new List<string>();
  52. var imagePaths = new List<string>();
  53. var skeletonPaths = new List<string>();
  54. foreach (string str in imported)
  55. {
  56. string extension = Path.GetExtension(str).ToLower();
  57. switch (extension)
  58. {
  59. case ".png":
  60. imagePaths.Add(str);
  61. break;
  62. case ".json":
  63. if (str.EndsWith("_tex.json", System.StringComparison.Ordinal))
  64. {
  65. atlasPaths.Add(str);
  66. }
  67. else if (IsValidDragonBonesData((TextAsset)AssetDatabase.LoadAssetAtPath(str, typeof(TextAsset))))
  68. {
  69. skeletonPaths.Add(str);
  70. }
  71. else
  72. {
  73. atlasPaths.Add(str);
  74. }
  75. break;
  76. case ".dbbin":
  77. if (File.Exists(str)){
  78. // string bytesPath = Path.GetDirectoryName(str) + "/" + Path.GetFileNameWithoutExtension(str) + ".bytes";
  79. // File.Move(str,bytesPath);
  80. // AssetDatabase.Refresh();
  81. // skeletonPaths.Add(bytesPath);
  82. }
  83. break;
  84. case ".bytes":
  85. if (IsValidDragonBonesData((TextAsset)AssetDatabase.LoadAssetAtPath(str, typeof(TextAsset)))){
  86. skeletonPaths.Add(str);
  87. }
  88. break;
  89. }
  90. }
  91. if (skeletonPaths.Count == 0)
  92. {
  93. return;
  94. }
  95. foreach(string skeletonPath in skeletonPaths)
  96. {
  97. List<string> imgPaths = new List<string>();
  98. List<string> atlPaths = new List<string>();
  99. foreach(string atlasPath in atlasPaths)
  100. {
  101. if(atlasPath.IndexOf(skeletonPath.Substring(0,skeletonPath.LastIndexOf("/")))==0)
  102. {
  103. atlPaths.Add(atlasPath);
  104. imgPaths.Add(atlasPath.Substring(0,atlasPath.LastIndexOf(".json"))+".png");
  105. }
  106. }
  107. ProcessTextureAtlasData(atlPaths);
  108. }
  109. }
  110. public static bool IsValidDragonBonesData (TextAsset asset)
  111. {
  112. if (asset.name.Contains("_ske"))
  113. {
  114. return true;
  115. }
  116. if(asset.text == "DBDT")
  117. {
  118. return true;
  119. }
  120. if (asset.text.IndexOf("\"armature\":") > 0)
  121. {
  122. return true;
  123. }
  124. return false;
  125. }
  126. static void ProcessTextureAtlasData(List<string> atlasPaths)
  127. {
  128. foreach(string path in atlasPaths)
  129. {
  130. TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
  131. if(ta)
  132. {
  133. TextureDataClass tdc = JsonUtility.FromJson<TextureDataClass>(ta.text);
  134. if(tdc != null && (tdc.width == 0 || tdc.height == 0))
  135. {
  136. //add width and height
  137. string imgPath = path.Substring(0,path.IndexOf(".json"))+".png";
  138. Texture2D texture = LoadPNG(Application.dataPath+"/"+ imgPath.Substring(6));
  139. if(texture)
  140. {
  141. tdc.width = texture.width;
  142. tdc.height = texture.height;
  143. //save
  144. string json = JsonUtility.ToJson(tdc);
  145. File.WriteAllText(path,json);
  146. EditorUtility.SetDirty(ta);
  147. GameObject.DestroyImmediate(texture);
  148. }
  149. }
  150. }
  151. }
  152. AssetDatabase.Refresh();
  153. AssetDatabase.SaveAssets();
  154. }
  155. static Texture2D LoadPNG(string filePath)
  156. {
  157. Texture2D tex = null;
  158. byte[] fileData;
  159. if (File.Exists(filePath))
  160. {
  161. fileData = File.ReadAllBytes(filePath);
  162. tex = new Texture2D(2, 2);
  163. tex.LoadImage(fileData);
  164. }
  165. return tex;
  166. }
  167. }
  168. }