Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. using UnityEditor;
  5. using UnityEngine;
  6.  
  7. public class BackgroundColorScope : GUI.Scope
  8. {
  9. private readonly Color color;
  10. public BackgroundColorScope(Color color)
  11. {
  12. this.color = GUI.backgroundColor;
  13. GUI.backgroundColor = color;
  14. }
  15.  
  16.  
  17. protected override void CloseScope()
  18. {
  19. GUI.backgroundColor = color;
  20. }
  21. }
  22.  
  23. public class JsonFormat : EditorWindow
  24. {
  25. static JsonFormat g_JsonFormatWindow;
  26.  
  27. string textArea = "";
  28. string selectableLabel = "";
  29. int stringLength = 0;
  30.  
  31. Vector2 leftScrollPos = Vector2.zero;
  32.  
  33. [MenuItem("Window/JsonFormat")]
  34. static void Open()
  35. {
  36. if (g_JsonFormatWindow == null)
  37. {
  38. g_JsonFormatWindow = CreateInstance<JsonFormat>();
  39. }
  40.  
  41. g_JsonFormatWindow.Show();
  42. }
  43.  
  44. void OnGUI()
  45. {
  46. //エスケープを押した時に閉じる
  47. if (Event.current.keyCode == KeyCode.Escape)
  48. {
  49. g_JsonFormatWindow.Close();
  50. }
  51.  
  52. using (new EditorGUILayout.HorizontalScope())
  53. {
  54. if (GUILayout.Button("整形する"))
  55. {
  56. selectableLabel = ToReadable(textArea);
  57. stringLength = selectableLabel.Length;
  58. }
  59. if (GUILayout.Button("圧縮する"))
  60. {
  61. selectableLabel = ToCompression(textArea);
  62. stringLength = selectableLabel.Length;
  63. }
  64. using (new BackgroundColorScope(stringLength > 0 ? Color.green : Color.clear))
  65. {
  66. //文字があれば緑色にする.
  67. if (GUILayout.Button("クリップボードにコピー"))
  68. {
  69. EditorGUIUtility.systemCopyBuffer = selectableLabel;
  70. }
  71. }
  72.  
  73. Color oldColor = GUI.color;
  74. GUI.color = textArea.Length > 14000 ? Color.red : oldColor;
  75. GUILayout.Box(textArea.Length.ToString());
  76. GUI.color = oldColor;
  77. }
  78.  
  79. textArea = EditorGUILayout.TextArea(textArea, GUILayout.Height(position.height / 3 - 30));
  80.  
  81. leftScrollPos = EditorGUILayout.BeginScrollView(leftScrollPos, GUI.skin.box);
  82. {
  83. EditorGUILayout.SelectableLabel(selectableLabel, GUILayout.Height(6000));
  84. }
  85. EditorGUILayout.EndScrollView();
  86. }
  87.  
  88. public static string ToReadable(string json)
  89. {
  90. if (string.IsNullOrWhiteSpace(json)) return json;
  91. int i = 0;
  92. int indent = 0;
  93. int quoteCount = 0;
  94. int position = -1;
  95. var sb = new StringBuilder();
  96. int lastindex = 0;
  97. while (true)
  98. {
  99. if (i > 0 && json[i] == '"' && json[i - 1] != '\\') quoteCount++;
  100.  
  101. if (quoteCount % 2 == 0)
  102. {
  103. if (json[i] == '{' || json[i] == '[')
  104. {
  105. indent++;
  106. position = 1;
  107. }
  108. else if (json[i] == '}' || json[i] == ']')
  109. {
  110. indent--;
  111. position = 0;
  112. }
  113. else if (json.Length > i && json[i] == ',' /*&& json[i + 1] == '"'*/)
  114. {
  115. position = 1;
  116. }
  117. if (position >= 0)
  118. {
  119. sb.AppendLine(json.Substring(lastindex, i + position - lastindex));
  120. sb.Append(new string(' ', indent * 4));
  121. lastindex = i + position;
  122. position = -1;
  123. }
  124. }
  125.  
  126. i++;
  127. if (json.Length <= i)
  128. {
  129. sb.Append(json.Substring(lastindex));
  130. break;
  131. }
  132. }
  133. return sb.ToString();
  134. }
  135.  
  136. public static string ToCompression(string s)
  137. {
  138. string temp = s.Replace("\r", "").Replace("\n", "");
  139. temp = Regex.Replace(temp, @"\s", "");
  140. return temp;
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement