Skip to content

Commit 69024ef

Browse files
Check webservices from the editor window
1 parent 2a340de commit 69024ef

File tree

1 file changed

+269
-0
lines changed

1 file changed

+269
-0
lines changed

Editor/MyJsonScript.cs

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using UnityEditor;
4+
using System.Collections.Generic;
5+
6+
/// <summary>
7+
/// You can modify the script as you want and do leave me a courtesy thanks.
8+
/// Enjoy the free script.
9+
/// if you need any help contact me on twitter @_prashantsingh_
10+
/// </summary>
11+
public class MyJsonScript : EditorWindow {
12+
13+
[MenuItem("[Master_Tools]/Web Service Checker")]
14+
static void Init() {
15+
16+
GetWindow(typeof(MyJsonScript));
17+
GetWindow(typeof(MyJsonScript)).minSize = new Vector2(600,500);
18+
GetWindow(typeof(MyJsonScript)).titleContent = new GUIContent("J.SON");
19+
}
20+
21+
[MenuItem("[Master_Tools]/About")]
22+
static void SupportDeveloper()
23+
{
24+
Application.OpenURL("https://github.com/prashant-singh");
25+
}
26+
27+
public MyJsonScript()
28+
{
29+
parametersNames = new List<string>();
30+
parametersValues = new List<string>();
31+
}
32+
33+
float width,height;
34+
35+
GUIStyle styleHelpboxInner;
36+
GUIStyle titleLabel,normalLabel,subtitleLabel,leftNormalLabel;
37+
string _data = "";
38+
string URL = "";
39+
string[] tabNames;
40+
int currentSettingNumber = 0;
41+
void InitStyles()
42+
{
43+
titleLabel = new GUIStyle();
44+
titleLabel.fontSize = 16;
45+
titleLabel.normal.textColor = Color.black;
46+
titleLabel.alignment = TextAnchor.UpperCenter;
47+
titleLabel.fixedHeight = 18;
48+
49+
normalLabel = new GUIStyle();
50+
normalLabel.fontSize = 12;
51+
normalLabel.normal.textColor = Color.black;
52+
normalLabel.fixedHeight = 14;
53+
normalLabel.alignment = TextAnchor.MiddleRight;
54+
55+
leftNormalLabel = new GUIStyle();
56+
leftNormalLabel.fontSize = 12;
57+
leftNormalLabel.normal.textColor = Color.black;
58+
leftNormalLabel.fixedHeight = 14;
59+
leftNormalLabel.alignment = TextAnchor.MiddleLeft;
60+
61+
subtitleLabel = new GUIStyle();
62+
subtitleLabel.fontSize = 14;
63+
subtitleLabel.normal.textColor = Color.black;
64+
subtitleLabel.fixedHeight = 15;
65+
subtitleLabel.alignment = TextAnchor.MiddleLeft;
66+
67+
styleHelpboxInner = new GUIStyle("HelpBox");
68+
styleHelpboxInner.padding = new RectOffset(6, 6, 6, 6);
69+
}
70+
71+
void OnGUI()
72+
{
73+
InitStyles();
74+
styleHelpboxInner = new GUIStyle("HelpBox");
75+
styleHelpboxInner.padding = new RectOffset(6, 6, 6, 6);
76+
// URL textfield
77+
GUILayout.BeginVertical(styleHelpboxInner);
78+
GUILayout.Label("Master Tools version 0.2",normalLabel);
79+
URL = EditorGUILayout.TextField("URL", URL);
80+
if(URL.Equals(""))
81+
{
82+
EditorGUILayout.HelpBox("Enter URL in the text field and then choose your options", MessageType.Info);
83+
}
84+
GUILayout.EndVertical();
85+
86+
// Tabs
87+
GUILayout.BeginVertical(styleHelpboxInner);
88+
tabNames = new string[]{"JSON","Post"};
89+
GUILayout.BeginHorizontal();
90+
GUILayout.Label("Select your option",leftNormalLabel);
91+
currentSettingNumber = GUILayout.Toolbar(currentSettingNumber,tabNames,GUILayout.MinWidth(200),GUILayout.MaxWidth(300),GUILayout.Height(30));
92+
GUILayout.EndHorizontal();
93+
GUILayout.EndVertical();
94+
95+
switch (currentSettingNumber)
96+
{
97+
case 0:
98+
GetJsonByURL();
99+
break;
100+
case 1:
101+
WebServicePosMethod();
102+
break;
103+
}
104+
}
105+
Vector2 scrollPos;
106+
107+
// Getting JSON result with the url
108+
private void GetJsonByURL()
109+
{
110+
//Get JSON button
111+
GUILayout.BeginVertical(styleHelpboxInner);
112+
113+
GUILayout.BeginHorizontal();
114+
if (GUILayout.Button("Get JSON",GUILayout.Width(100),GUILayout.Height(30)) )
115+
{
116+
if(URL.Equals("")) // If URL field is empty
117+
{
118+
_data = "Enter the URL";
119+
return;
120+
}
121+
122+
// Getting data
123+
WWW test = new WWW(URL);
124+
while (!test.isDone) ;
125+
if (!string.IsNullOrEmpty(test.error)) {
126+
Debug.Log(test.error);
127+
_data = test.error;
128+
} else {
129+
Debug.Log(test.text);
130+
_data = FormatDataTodisplyWell(test.text);
131+
}
132+
}
133+
134+
if(GUILayout.Button("Clear", GUILayout.Width(100), GUILayout.Height(30)))
135+
{
136+
_data = "";
137+
}
138+
GUILayout.EndHorizontal();
139+
// Showing data on the label
140+
EditorGUILayout.TextArea(_data,new GUIStyle(GUI.skin.label));
141+
GUILayout.EndVertical();
142+
143+
}
144+
145+
List<string> parametersNames,parametersValues;
146+
int parameterCounters = 0;
147+
bool canShowMinusButton = false;
148+
149+
// Firing post method with parameters
150+
private void WebServicePosMethod()
151+
{
152+
153+
// Showing the parameters text fields
154+
GUILayout.BeginVertical(styleHelpboxInner);
155+
GUILayout.BeginVertical();
156+
157+
for (int count = 0; count < parametersNames.Count; count++)
158+
{
159+
GUILayout.BeginHorizontal();
160+
161+
parametersNames[count] = EditorGUILayout.TextField(parametersNames[count],GUILayout.MinWidth(260));
162+
parametersValues[count] = EditorGUILayout.TextField(parametersValues[count],GUILayout.MinWidth(260));
163+
164+
GUILayout.EndHorizontal();
165+
}
166+
GUILayout.EndVertical();
167+
168+
169+
if(parameterCounters<=0)
170+
{
171+
canShowMinusButton = false;
172+
}
173+
else
174+
{
175+
canShowMinusButton = true;
176+
}
177+
178+
179+
// plus minus buttons
180+
GUILayout.BeginHorizontal(styleHelpboxInner);
181+
GUILayout.Label("Add/Remove Parameters ",new GUIStyle(GUI.skin.label));
182+
if(GUILayout.Button("+", GUILayout.Width(30), GUILayout.Height(30)))
183+
{
184+
parameterCounters++;
185+
parametersNames.Add("var "+(parameterCounters));
186+
parametersValues.Add("value "+(parameterCounters));
187+
}
188+
189+
if(canShowMinusButton)
190+
{
191+
if(GUILayout.Button("-", GUILayout.Width(30), GUILayout.Height(30)))
192+
{
193+
parametersNames.RemoveAt(parameterCounters-1);
194+
parametersValues.RemoveAt(parameterCounters-1);
195+
parameterCounters--;
196+
}
197+
}
198+
199+
200+
if(GUILayout.Button("Clear", GUILayout.Width(100), GUILayout.Height(30)))
201+
{
202+
parameterCounters = 0;
203+
parametersNames.Clear();
204+
_data = "";
205+
}
206+
207+
208+
209+
210+
// Submit button to get the response
211+
if (GUILayout.Button("Submit", GUILayout.Width(100), GUILayout.Height(30)))
212+
{
213+
if(URL.Equals(""))
214+
{
215+
_data = "Enter the URL";
216+
return;
217+
}
218+
WWWForm form = new WWWForm();
219+
220+
// This fills parameters and values in the form
221+
for (int count = 0; count < parametersNames.Count; count++)
222+
{
223+
form.AddField(parametersNames[count],parametersValues[count]);
224+
}
225+
226+
WWW www = new WWW(URL,form);
227+
228+
while (!www.isDone) ;
229+
if (!string.IsNullOrEmpty(www.error))
230+
{
231+
Debug.Log(www.error);
232+
_data = www.error;
233+
}
234+
else
235+
{
236+
Debug.Log(www.text);
237+
_data = FormatDataTodisplyWell(www.text);
238+
}
239+
}
240+
GUILayout.EndHorizontal();
241+
242+
243+
if(!string.IsNullOrEmpty(_data))
244+
{
245+
// Scroll view for displaying data
246+
scrollPos = EditorGUILayout.BeginScrollView(scrollPos,false, false, GUILayout.Width(position.width), GUILayout.Height(position.height));
247+
GUI.skin.label.wordWrap = true;
248+
GUILayout.TextArea(_data,new GUIStyle(GUI.skin.label));
249+
GUILayout.EndScrollView();
250+
}
251+
GUILayout.EndVertical();
252+
}
253+
254+
// this is a little trick for formating the data to look like well formatted JSON data
255+
string FormatDataTodisplyWell(string currentData)
256+
{
257+
string tempNewData = "";
258+
tempNewData = currentData.Replace("{","\n{\n\t");
259+
tempNewData = tempNewData.Replace(",",",\n\t");
260+
tempNewData = tempNewData.Replace("}","\n}\n");
261+
return tempNewData;
262+
}
263+
264+
265+
266+
267+
268+
269+
}

0 commit comments

Comments
 (0)