EditorAuxiliary.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Text.RegularExpressions;
  10. using LitJson;
  11. [CustomEditor(typeof(Auxiliary))]
  12. public class EditorAuxiliary : Editor
  13. {
  14. #region 变量
  15. private Auxiliary Script;
  16. #endregion
  17. private void OnEnable()
  18. {
  19. Script = (Auxiliary) target;
  20. }
  21. public override void OnInspectorGUI()
  22. {
  23. base.OnInspectorGUI();
  24. if (GUILayout.Button("Test"))
  25. {
  26. Debug.Log(Script.Go.GetComponent<Image>().rectTransform.sizeDelta);
  27. }
  28. if (GUILayout.Button("CreateDragonboneTex"))
  29. {
  30. Match match = Regex.Match(Script.DragonboneSke.text, "\"name\":\"[^\"]+\"", RegexOptions.RightToLeft);
  31. string projectName = match.Value.Replace("\"name\":\"", "").Replace("\"", "");
  32. foreach (var sprite in Script.Sprites)
  33. {
  34. string content = $"{{ \"imagePath\":\"{sprite.name}.png\",\"SubTexture\":[{{\"width\":{sprite.rect.width},\"y\":0,\"height\":{sprite.rect.height},\"name\":\"{sprite.name}\",\"x\":0}}],\"name\":\"{projectName}\"}}";
  35. File.WriteAllText($"{Application.dataPath}/Resource/Sprite/Editor/Dress/{sprite.name}_tex.json", content);
  36. }
  37. AssetDatabase.Refresh();
  38. }
  39. if (GUILayout.Button("PrintBounds"))
  40. {
  41. PrintBounds();
  42. }
  43. if (GUILayout.Button("PrintPosition"))
  44. {
  45. PrintPosition();
  46. }
  47. if (GUILayout.Button("PrintPositionLocal"))
  48. {
  49. PrintPositionLocal();
  50. }
  51. if (GUILayout.Button("ChangeFont"))
  52. {
  53. ChangeFont();
  54. }
  55. if (GUILayout.Button("ChangePivot"))
  56. {
  57. ChangePivot();
  58. }
  59. if (GUILayout.Button("AddSuffix"))
  60. {
  61. AddSuffix();
  62. }
  63. if (GUILayout.Button("AddPrefix"))
  64. {
  65. AddPrefix();
  66. }
  67. if (GUILayout.Button("ChangePrefix"))
  68. {
  69. ChangePrefix();
  70. }
  71. if (GUILayout.Button("ReplaceFromTo"))
  72. {
  73. ReplaceFromTo();
  74. }
  75. if (GUILayout.Button("CopyCurve"))
  76. {
  77. //复制动画
  78. //EditorCurveBinding[] bindings = AnimationUtility.GetCurveBindings(Script.ClipOrigin);
  79. //for (int i = 0; i < bindings.Length; i++)
  80. //{
  81. // Debug.Log(string.Format("path : {0} type : {1} name : {2}", bindings[i].path, bindings[i].type, bindings[i].propertyName));
  82. //}
  83. //EditorCurveBinding bindingOrigin = new EditorCurveBinding();
  84. //bindingOrigin.type = typeof(SpriteRenderer);
  85. //bindingOrigin.path = "Flash";
  86. //bindingOrigin.propertyName = "m_Color.a";
  87. //AnimationCurve curveOrigin = AnimationUtility.GetEditorCurve(Script.ClipOrigin, bindingOrigin);
  88. //EditorCurveBinding bindingNew = new EditorCurveBinding();
  89. //bindingNew.type = typeof(Image);
  90. //bindingNew.path = "UIFlash";
  91. //bindingNew.propertyName = "m_Color.a";
  92. //AnimationCurve curveNew = AnimationUtility.GetEditorCurve(Script.ClipNew, bindingNew);
  93. //curveNew.keys = curveOrigin.keys;
  94. //AnimationUtility.SetEditorCurve(Script.ClipNew, bindingNew, curveNew);
  95. //EditorUtility.SetDirty(Script.ClipNew);
  96. }
  97. if (GUILayout.Button("InstaciateAll"))
  98. {
  99. for (int i = 0; i < Script.Sprites.Count; i++)
  100. {
  101. GameObject go = new GameObject();
  102. go.transform.parent = Script.Go.transform;
  103. go.AddComponent<SpriteRenderer>().sprite = Script.Sprites[i];
  104. }
  105. }
  106. if (GUILayout.Button("SliceByDragonboneData"))
  107. {
  108. SliceByDragonboneData();
  109. }
  110. if (GUILayout.Button("ClearPackingTag"))
  111. {
  112. List<string> searchExtension = new List<string>()
  113. {
  114. ".png",
  115. };
  116. List<string> pathList = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories).Where
  117. (
  118. path =>
  119. {
  120. string extension = Path.GetExtension(path);
  121. if (searchExtension.Contains(extension))
  122. {
  123. return true;
  124. }
  125. else
  126. {
  127. return false;
  128. }
  129. }
  130. )
  131. .ToList();
  132. for (int i = 0; i < pathList.Count; i++)
  133. {
  134. string path = "Assets" + pathList[i].Replace(Application.dataPath, "");
  135. TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
  136. if (textureImporter == null)
  137. {
  138. continue;
  139. }
  140. textureImporter.spritePackingTag = "";
  141. textureImporter.SaveAndReimport();
  142. }
  143. }
  144. }
  145. private void SliceByDragonboneData()
  146. {
  147. string assetPath = AssetDatabase.GetAssetPath(Script.Texture);
  148. TextureImporter textureImporter = (TextureImporter) AssetImporter.GetAtPath(assetPath);
  149. JsonData jsonData = JsonMapper.ToObject(Script.TextAsset.text);
  150. jsonData = jsonData["SubTexture"];
  151. List<SpriteMetaData>spriteMetaDataList = new List<SpriteMetaData>();
  152. for (int i = 0; i < jsonData.Count; i++)
  153. {
  154. SpriteMetaData spriteMetaData = new SpriteMetaData();
  155. int x = int.Parse(jsonData[i].Inst_Object["x"].ToJson().Trim('"'));
  156. int y = int.Parse(jsonData[i].Inst_Object["y"].ToJson().Trim('"'))/* - 2050*/;
  157. int width = int.Parse(jsonData[i].Inst_Object["width"].ToJson().Trim('"'));
  158. int height = int.Parse(jsonData[i].Inst_Object["height"].ToJson().Trim('"'));
  159. string spriteName = jsonData[i].Inst_Object["name"].ToJson().Trim('"');
  160. spriteMetaData.rect = new Rect(x, Script.Texture.width - y - height, width, height);
  161. spriteMetaData.name = spriteName;
  162. spriteMetaDataList.Add(spriteMetaData);
  163. }
  164. textureImporter.spritesheet = spriteMetaDataList.ToArray();
  165. textureImporter.textureType = TextureImporterType.Sprite;
  166. textureImporter.spriteImportMode = SpriteImportMode.Multiple;
  167. textureImporter.SaveAndReimport();
  168. }
  169. private void PrintBounds()
  170. {
  171. Bounds bounds = Script.Go.GetComponent<Renderer>().bounds;
  172. Debug.Log(string.Format("x : {0:0.00} y : {1:0.00} z : {2:0.00}", bounds.extents.x, bounds.extents.y, bounds.extents.z));
  173. }
  174. private void PrintPosition()
  175. {
  176. Debug.Log(Script.Go.transform.position);
  177. }
  178. private void PrintPositionLocal()
  179. {
  180. Debug.Log(Script.Go.transform.localPosition);
  181. }
  182. private void ChangeFont()
  183. {
  184. for (int j = 0; j < Script.GoList.Count; j++)
  185. {
  186. Transform[] transforms = Script.GoList[j].GetComponentsInChildren<Transform>(true);
  187. for (int i = 0; i < transforms.Length; i++)
  188. {
  189. Text text = transforms[i].GetComponent<Text>();
  190. TextMesh textMesh = transforms[i].GetComponent<TextMesh>();
  191. if (text != null)
  192. {
  193. text.font = Script.Font;
  194. text.fontStyle = FontStyle.Normal;
  195. }
  196. if (textMesh != null)
  197. {
  198. textMesh.font = Script.Font;
  199. textMesh.fontStyle = FontStyle.Normal;
  200. }
  201. }
  202. }
  203. }
  204. private void ChangePivot()
  205. {
  206. for (int i = 0; i < Script.SrList.Count; i++)
  207. {
  208. string path = AssetDatabase.GetAssetPath(Script.SrList[i].sprite);
  209. TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(path);
  210. float offsetX = -Script.SrList[i].transform.position.x * 100 / Script.SrList[i].sprite.texture.width;
  211. float offsetY = -Script.SrList[i].transform.position.y * 100 / Script.SrList[i].sprite.texture.height;
  212. textureImporter.spritePivot = new Vector2(textureImporter.spritePivot.x + offsetX, textureImporter.spritePivot.y + offsetY);
  213. AssetDatabase.ImportAsset(path);
  214. }
  215. }
  216. private void AddSuffix()
  217. {
  218. Transform[] transforms = Script.Go.GetComponentsInChildren<Transform>(true);
  219. for (int i = 0; i < transforms.Length; i++)
  220. {
  221. transforms[i].name = transforms[i].name + Script.String1;
  222. }
  223. }
  224. private void AddPrefix()
  225. {
  226. Transform[] transforms = Script.Go.GetComponentsInChildren<Transform>(true);
  227. for (int i = 0; i < transforms.Length; i++)
  228. {
  229. transforms[i].name = Script.String1 + transforms[i].name;
  230. }
  231. }
  232. private void ChangePrefix()
  233. {
  234. Transform[] transforms = Script.Go.GetComponentsInChildren<Transform>(true);
  235. for (int i = 0; i < transforms.Length; i++)
  236. {
  237. int index = transforms[i].name.IndexOf('_');
  238. transforms[i].name = Script.String1 + transforms[i].name.Remove(0, index + 1);
  239. }
  240. }
  241. private void ReplaceFromTo()
  242. {
  243. Transform[] transforms = Script.Go.GetComponentsInChildren<Transform>(true);
  244. for (int i = 0; i < transforms.Length; i++)
  245. {
  246. transforms[i].name = transforms[i].name.Replace(Script.String1, Script.String2);
  247. }
  248. }
  249. }