728x90
반응형
제이슨을 파싱해서 봐야하는 일이 생각보다 많더라
그래서 늘 잘 만들어진 웹 서비스를 이용해왔는데
유니티에서 바로바로 확인하고 싶어서 제이슨 뷰어를 만들었다.
현재는 기본적인 기능만 있는데 앞으로는 복사, 검색, 포커싱 등등 여러가지 기능을 추가할 예정이다.
(나중에는 유니티에서 자체 제공하는 json으로만 구현된 툴로 새로 만들어야겠다..)
using UnityEngine;
using UnityEditor;
using SimpleJSON;
using System.Collections.Generic;
public class JsonViewer : EditorWindow
{
private Dictionary<string, bool> m_isFoldoutArray = new Dictionary<string, bool>();
private JSONClass jsonClass;
private Vector2 m_scrollPosition;
private string search;
public void Initialize(string response)
{
JSONNode jNode = JSONNode.Parse(response);
jsonClass = jNode as JSONClass;
SetFoldoutData(jsonClass);
}
public void SetJsonViewer()
{
search = EditorGUILayout.TextField(search, (GUIStyle)"ToolbarSeachTextField", GUILayout.ExpandWidth(true));
DrawJsonView(jsonClass, 0, 0);
}
private void SetFoldoutData(JSONNode jNode)
{
JSONClass nodeClass = jNode as JSONClass;
if (nodeClass == null)
return;
string pairHashKey = string.Empty;
string hashKey = string.Empty;
foreach (var pair in nodeClass.m_Dict)
{
var jsonArray = pair.Value as JSONArray;
var jsonClass = pair.Value as JSONClass;
pairHashKey = pair.GetHashCode().ToString();
if (jsonArray != null)
{
if (!m_isFoldoutArray.ContainsKey(pairHashKey))
m_isFoldoutArray.Add(pairHashKey, false);
for (int i = 0; i < jsonArray.Count; ++i)
{
hashKey = jsonArray[i].GetHashCode().ToString();
if (!m_isFoldoutArray.ContainsKey(hashKey))
m_isFoldoutArray.Add(hashKey, false);
SetFoldoutData(jsonArray[i]);
}
}
else if (jsonClass != null)
{
if (!m_isFoldoutArray.ContainsKey(pairHashKey))
m_isFoldoutArray.Add(pairHashKey, false);
hashKey = jsonClass.GetHashCode().ToString();
if (!m_isFoldoutArray.ContainsKey(hashKey))
m_isFoldoutArray.Add(hashKey, false);
SetFoldoutData(jsonClass);
}
}
}
private void DrawJsonView(JSONNode jNode, int index, int loop = 0)
{
JSONClass nodeClass = jNode as JSONClass;
if (nodeClass == null)
{
JSONData nodeData = jNode as JSONData;
if (nodeData != null)
SetSearchColor(index + " : " + nodeData.Value);
return;
}
foreach (var pair in nodeClass.m_Dict)
{
var jsonArray = pair.Value as JSONArray;
var jsonClass = pair.Value as JSONClass;
var jsonData = pair.Value as JSONData;
if (jsonArray != null)
SetArrayGUI(jsonArray, pair.Key, pair.GetHashCode().ToString(), loop);
else if (jsonClass != null)
SetClassGUI(jsonClass, pair.Key, pair.GetHashCode().ToString(), loop);
else if (jsonData != null)
SetSearchColor(pair.Key + " : " + jsonData.Value);
else
SetSearchColor(pair.Key + " : " + (pair.Value == null ? "null" : pair.Value));
}
}
private void SetSearchColor(string text)
{
GUIStyle white = new GUIStyle(EditorStyles.label);
white.normal.textColor = Color.yellow;
if (!string.IsNullOrEmpty(search) && !string.IsNullOrEmpty(text) && text.Contains(search))
{
GUILayout.BeginHorizontal();
{
string whiteColorText = text.Substring(0, text.IndexOf(search));
string tempText = text.Substring(whiteColorText.Length, (text.Length - whiteColorText.Length));
GUILayout.Label(whiteColorText, GUILayout.ExpandWidth(false));
while (true)
{
int indexOf = tempText.IndexOf(search);
if (indexOf == -1)
{
GUILayout.Label(tempText, GUILayout.ExpandWidth(false));
break;
}
indexOf += search.Length;
string yollowColorText = tempText.Substring(0, indexOf);
GUILayout.Label(yollowColorText, white, GUILayout.ExpandWidth(false));
tempText = tempText.Substring(indexOf, (tempText.Length - indexOf));
}
}
GUILayout.EndHorizontal();
}
else
{
GUILayout.Label(text);
}
}
private void SetArrayGUI(JSONArray jsonArray, string key, string hashKey, int loop)
{
if (m_isFoldoutArray.ContainsKey(hashKey))
{
string arrayName = (key + " : [" + jsonArray.Count + "]");
m_isFoldoutArray[hashKey] = EditorGUILayout.Foldout(m_isFoldoutArray[hashKey], arrayName, true);
if (m_isFoldoutArray[hashKey])
{
DrawJsonViewBeginGUI(5, () =>
{
SetSearchColor("[");
DrawJsonViewBeginGUI(5, () =>
{
if (jsonArray.Count == 1)
{
DrawJsonViewGUI(jsonArray[0], 0, (loop + 1));
}
else
{
for (int i = 0; i < jsonArray.Count; ++i)
{
string subHashKey = jsonArray[i].GetHashCode().ToString();
if (m_isFoldoutArray.ContainsKey(subHashKey))
{
if (jsonArray[i].Count == 1)
{
DrawJsonViewGUI(jsonArray[i], i, (loop + 1));
}
else
{
string subArrayName = (i + " : {" + jsonArray[i].Count + "}");
m_isFoldoutArray[subHashKey] = EditorGUILayout.Foldout(m_isFoldoutArray[subHashKey], subArrayName, true);
if (m_isFoldoutArray[subHashKey])
{
DrawJsonViewGUI(jsonArray[i], i, (loop + 1));
}
}
}
}
}
});
SetSearchColor("]");
});
}
}
}
private void SetClassGUI(JSONClass jsonClass, string key, string hashKey, int loop)
{
if (m_isFoldoutArray.ContainsKey(hashKey))
{
string className = (key + " : {" + jsonClass.Count + "}");
m_isFoldoutArray[hashKey] = EditorGUILayout.Foldout(m_isFoldoutArray[hashKey], className, true);
if (m_isFoldoutArray[hashKey])
{
DrawJsonViewBeginGUI(5, () =>
{
SetSearchColor("{");
DrawJsonViewGUI(jsonClass, 0, (loop + 1));
SetSearchColor("}");
});
}
}
}
private void DrawJsonViewBeginGUI(int space, System.Action action)
{
GUILayout.BeginHorizontal();
{
GUILayout.Space(space);
GUILayout.BeginVertical();
{
action();
}
GUILayout.EndVertical();
}
GUILayout.EndHorizontal();
}
private void DrawJsonViewGUI(JSONNode node, int index, int loop)
{
GUILayout.BeginHorizontal();
{
GUILayout.Space(25);
GUILayout.BeginVertical();
{
DrawJsonView(node, index, loop);
}
GUILayout.EndVertical();
}
GUILayout.EndHorizontal();
}
private void OnGUI()
{
m_scrollPosition = EditorGUILayout.BeginScrollView(m_scrollPosition);
SetJsonViewer();
EditorGUILayout.EndScrollView();
}
}
반응형
'유니티 > 기본기' 카테고리의 다른 글
유니티 Shader 공부 (Properties) (2) | 2020.07.27 |
---|---|
유니티 JsonViewer 툴 V2 (Newtonsoft.json) (3) | 2020.06.30 |
네스티드 프리팹 (Netsted Prefabs) (2) | 2020.04.06 |
GUI (ezgui-ngui-ugui) (0) | 2020.04.06 |
단축키 (0) | 2020.04.06 |