Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Text.RegularExpressions;
- using UnityEditor;
- using UnityEngine;
- public class BackgroundColorScope : GUI.Scope
- {
- private readonly Color color;
- public BackgroundColorScope(Color color)
- {
- this.color = GUI.backgroundColor;
- GUI.backgroundColor = color;
- }
- protected override void CloseScope()
- {
- GUI.backgroundColor = color;
- }
- }
- public class JsonFormat : EditorWindow
- {
- static JsonFormat g_JsonFormatWindow;
- string textArea = "";
- string selectableLabel = "";
- int stringLength = 0;
- Vector2 leftScrollPos = Vector2.zero;
- [MenuItem("Window/JsonFormat")]
- static void Open()
- {
- if (g_JsonFormatWindow == null)
- {
- g_JsonFormatWindow = CreateInstance<JsonFormat>();
- }
- g_JsonFormatWindow.Show();
- }
- void OnGUI()
- {
- //エスケープを押した時に閉じる
- if (Event.current.keyCode == KeyCode.Escape)
- {
- g_JsonFormatWindow.Close();
- }
- using (new EditorGUILayout.HorizontalScope())
- {
- if (GUILayout.Button("整形する"))
- {
- selectableLabel = ToReadable(textArea);
- stringLength = selectableLabel.Length;
- }
- if (GUILayout.Button("圧縮する"))
- {
- selectableLabel = ToCompression(textArea);
- stringLength = selectableLabel.Length;
- }
- using (new BackgroundColorScope(stringLength > 0 ? Color.green : Color.clear))
- {
- //文字があれば緑色にする.
- if (GUILayout.Button("クリップボードにコピー"))
- {
- EditorGUIUtility.systemCopyBuffer = selectableLabel;
- }
- }
- Color oldColor = GUI.color;
- GUI.color = textArea.Length > 14000 ? Color.red : oldColor;
- GUILayout.Box(textArea.Length.ToString());
- GUI.color = oldColor;
- }
- textArea = EditorGUILayout.TextArea(textArea, GUILayout.Height(position.height / 3 - 30));
- leftScrollPos = EditorGUILayout.BeginScrollView(leftScrollPos, GUI.skin.box);
- {
- EditorGUILayout.SelectableLabel(selectableLabel, GUILayout.Height(6000));
- }
- EditorGUILayout.EndScrollView();
- }
- public static string ToReadable(string json)
- {
- if (string.IsNullOrWhiteSpace(json)) return json;
- int i = 0;
- int indent = 0;
- int quoteCount = 0;
- int position = -1;
- var sb = new StringBuilder();
- int lastindex = 0;
- while (true)
- {
- if (i > 0 && json[i] == '"' && json[i - 1] != '\\') quoteCount++;
- if (quoteCount % 2 == 0)
- {
- if (json[i] == '{' || json[i] == '[')
- {
- indent++;
- position = 1;
- }
- else if (json[i] == '}' || json[i] == ']')
- {
- indent--;
- position = 0;
- }
- else if (json.Length > i && json[i] == ',' /*&& json[i + 1] == '"'*/)
- {
- position = 1;
- }
- if (position >= 0)
- {
- sb.AppendLine(json.Substring(lastindex, i + position - lastindex));
- sb.Append(new string(' ', indent * 4));
- lastindex = i + position;
- position = -1;
- }
- }
- i++;
- if (json.Length <= i)
- {
- sb.Append(json.Substring(lastindex));
- break;
- }
- }
- return sb.ToString();
- }
- public static string ToCompression(string s)
- {
- string temp = s.Replace("\r", "").Replace("\n", "");
- temp = Regex.Replace(temp, @"\s", "");
- return temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement