Skip to content

Commit 2a340de

Browse files
Other handy tools that are important
This script contains the tools like know how much size the images are taking in your project and you can Set, Get and Delete the player prefs from the editor and you can enable the auto save the current scene before playing to avoid unfortunate loss of unsaved scene when the editor crashes after playing.
1 parent b3a7246 commit 2a340de

File tree

1 file changed

+339
-0
lines changed

1 file changed

+339
-0
lines changed

Editor/OtherMasterTools.cs

Lines changed: 339 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using UnityEditor;
4+
using System.IO;
5+
using System.Collections.Generic;
6+
//using System;
7+
public enum OPTIONS {
8+
Int = 0,
9+
String = 1,
10+
Float = 2
11+
}
12+
public class OtherMasterTools : EditorWindow {
13+
public OPTIONS op;
14+
[MenuItem("[Master_Tools]/Tools")]
15+
static void Init() {
16+
17+
GetWindow(typeof(OtherMasterTools));
18+
GetWindow(typeof(OtherMasterTools)).minSize = new Vector2(600,500);
19+
GetWindow(typeof(OtherMasterTools)).titleContent = new GUIContent("Master Tools");
20+
_playerPrefsKey = "Key";
21+
_playerPrefsValue = "Value";
22+
_playerPrefsFloatValue = 0.0f;
23+
_playerPrefsIntValue = 0;
24+
isAutoSaveBeforePlay = EditorPrefs.GetBool("autoSaveOnPlay",false);
25+
}
26+
27+
28+
public OtherMasterTools()
29+
{
30+
EditorApplication.playmodeStateChanged += SaveCurrentScene;
31+
}
32+
33+
GUIStyle styleHelpboxInner;
34+
GUIStyle titleLabel,normalLabel,subtitleLabel;
35+
string _allTextureSizeInAsset = "";
36+
static string _playerPrefsKey,_playerPrefsValue;
37+
static float _playerPrefsFloatValue;
38+
static int _playerPrefsIntValue;
39+
40+
string _playerPrefsNotificationLabel;
41+
42+
bool _playerPrefBoolValue;
43+
static bool isAutoSaveBeforePlay,isAutoSaveWithinTime;
44+
45+
void OnGUI()
46+
{
47+
InitStyles();
48+
49+
GUILayout.BeginVertical(styleHelpboxInner);
50+
GUILayout.Label("Master Tools version 0.2",normalLabel);
51+
GUILayout.Label(" Other Tools ",titleLabel);
52+
GUILayout.Space(20);
53+
GUILayout.Label(" Total size of Images in project folder ",subtitleLabel);
54+
GUILayout.BeginHorizontal(styleHelpboxInner);
55+
// Get All the
56+
if(GUILayout.Button(" Refresh ",GUILayout.MinWidth(80),GUILayout.MaxWidth(100),GUILayout.MinHeight(30)))
57+
{
58+
totalSize = 0;
59+
GetCompressedFileSize();
60+
}
61+
_allTextureSizeInAsset = GetFormattedSize(totalSize);
62+
GUILayout.Label(" Size : "+_allTextureSizeInAsset,normalLabel);
63+
GUILayout.EndHorizontal();
64+
GUILayout.Space(20);
65+
GUILayout.Label(" Edit any player prefs ",subtitleLabel);
66+
GUILayout.BeginVertical();
67+
GUILayout.BeginHorizontal(styleHelpboxInner);
68+
69+
_playerPrefsKey = EditorGUILayout.TextField(_playerPrefsKey,GUILayout.MinHeight(20));
70+
71+
switch (op) {
72+
default:
73+
_playerPrefsValue = EditorGUILayout.TextField(_playerPrefsValue,GUILayout.MinHeight(20));
74+
break;
75+
case OPTIONS.Float:
76+
_playerPrefsFloatValue = EditorGUILayout.FloatField(_playerPrefsFloatValue,GUILayout.MinHeight(20));
77+
break;
78+
case OPTIONS.Int:
79+
_playerPrefsIntValue = EditorGUILayout.IntField(_playerPrefsIntValue,GUILayout.MinHeight(20));
80+
break;
81+
}
82+
op = (OPTIONS) EditorGUILayout.EnumPopup( op,GUILayout.MaxWidth(80));
83+
84+
GUI.SetNextControlName("clearButton");
85+
if(GUILayout.Button("X",GUILayout.MaxWidth(20),GUILayout.Height(20)))
86+
{
87+
_playerPrefsKey = "Key";
88+
_playerPrefsValue = "Value";
89+
_playerPrefsNotificationLabel = "";
90+
GUI.FocusControl("clearButton");
91+
}
92+
93+
GUILayout.EndHorizontal();
94+
95+
GUILayout.BeginHorizontal();
96+
97+
if(GUILayout.Button("Save",GUILayout.MinWidth(100),GUILayout.MaxWidth(150),GUILayout.MinHeight(25)))
98+
{
99+
if(!string.IsNullOrEmpty(_playerPrefsKey))
100+
{
101+
if(op == OPTIONS.String)
102+
{
103+
PlayerPrefs.SetString(_playerPrefsKey,_playerPrefsValue.ToString());
104+
}
105+
else if(op == OPTIONS.Int)
106+
{
107+
PlayerPrefs.SetInt(_playerPrefsKey,_playerPrefsIntValue);
108+
}
109+
else if(op == OPTIONS.Float)
110+
{
111+
PlayerPrefs.SetFloat(_playerPrefsKey,_playerPrefsFloatValue);
112+
}
113+
_playerPrefsNotificationLabel = "Data saved";
114+
}
115+
else
116+
{
117+
_playerPrefsNotificationLabel = "Enter the PlayerPrefs Key";
118+
}
119+
}
120+
if(GUILayout.Button("Get",GUILayout.MinWidth(100),GUILayout.MaxWidth(150),GUILayout.MinHeight(25)))
121+
{
122+
if(!string.IsNullOrEmpty(_playerPrefsKey))
123+
{
124+
if(op == OPTIONS.String)
125+
{
126+
_playerPrefsValue = PlayerPrefs.GetString(_playerPrefsKey,_playerPrefsValue.ToString());
127+
}
128+
else if(op == OPTIONS.Int)
129+
{
130+
_playerPrefsIntValue = PlayerPrefs.GetInt(_playerPrefsKey,_playerPrefsIntValue);
131+
}
132+
else if(op == OPTIONS.Float)
133+
{
134+
_playerPrefsFloatValue = PlayerPrefs.GetFloat(_playerPrefsKey,_playerPrefsFloatValue);
135+
}
136+
_playerPrefsNotificationLabel = "Data loaded";
137+
}
138+
else
139+
{
140+
_playerPrefsNotificationLabel = "Enter the PlayerPrefs Key";
141+
}
142+
}
143+
144+
if(GUILayout.Button("Delete this key",GUILayout.MinWidth(100),GUILayout.MaxWidth(150),GUILayout.MinHeight(25)))
145+
{
146+
if(!string.IsNullOrEmpty(_playerPrefsKey))
147+
{
148+
bool option = EditorUtility.DisplayDialog( "Do you really want to Delete "+_playerPrefsKey+ "'s data???",
149+
"Gotta confirm it bro!",
150+
"I know what I'm doing.",
151+
"No bro, I swear I didn't do anything."
152+
);
153+
if(option)
154+
{
155+
PlayerPrefs.DeleteAll();
156+
_playerPrefsNotificationLabel = "Deleted current key's data";
157+
}
158+
else
159+
{
160+
Debug.LogError(" Na bolta hai sala");
161+
}
162+
}
163+
else
164+
{
165+
_playerPrefsNotificationLabel = "Enter the PlayerPrefs Key";
166+
}
167+
}
168+
if(GUILayout.Button("Delete All",GUILayout.MinWidth(100),GUILayout.MaxWidth(150),GUILayout.MinHeight(25)))
169+
{
170+
bool option = EditorUtility.DisplayDialog( "Do you really want to Delete all the Data ???? ",
171+
"Gotta confirm it bro!",
172+
"I know what I'm doing.",
173+
"Noooooo!"
174+
);
175+
if(option)
176+
{
177+
PlayerPrefs.DeleteAll();
178+
_playerPrefsNotificationLabel = "All data deleted";
179+
}
180+
else
181+
{
182+
// Debug.LogError(" Na bolta hai sala");
183+
}
184+
}
185+
186+
GUILayout.EndHorizontal();
187+
GUILayout.Label(_playerPrefsNotificationLabel,normalLabel);
188+
GUILayout.EndVertical();
189+
190+
GUILayout.Space(20);
191+
GUILayout.Label(" No more losing your changes ",subtitleLabel);
192+
GUILayout.BeginVertical();
193+
194+
GUILayout.BeginHorizontal(styleHelpboxInner);
195+
isAutoSaveBeforePlay = GUILayout.Toggle(isAutoSaveBeforePlay,"Auto save scene before playing");
196+
if(GUILayout.Button("Save settings",GUILayout.MaxWidth(100),GUILayout.MaxHeight(30)))
197+
{
198+
EditorPrefs.SetBool("autoSaveOnPlay",isAutoSaveBeforePlay);
199+
}
200+
// isAutoSaveWithinTime = GUILayout.Toggle(isAutoSaveWithinTime,"Auto save within some time");
201+
GUILayout.EndHorizontal();
202+
203+
GUILayout.EndVertical();
204+
205+
206+
207+
GUILayout.EndVertical();
208+
}
209+
210+
static void SaveCurrentScene()
211+
{
212+
if (!EditorApplication.isPlaying && EditorApplication.isPlayingOrWillChangePlaymode && isAutoSaveBeforePlay)
213+
{
214+
Debug.Log("Saving scene on play...");
215+
EditorApplication.SaveScene();
216+
}
217+
}
218+
219+
220+
void InitStyles()
221+
{
222+
titleLabel = new GUIStyle();
223+
titleLabel.fontSize = 16;
224+
titleLabel.normal.textColor = Color.black;
225+
titleLabel.alignment = TextAnchor.UpperCenter;
226+
titleLabel.fixedHeight = 18;
227+
228+
normalLabel = new GUIStyle();
229+
normalLabel.fontSize = 12;
230+
normalLabel.normal.textColor = Color.black;
231+
normalLabel.fixedHeight = 14;
232+
normalLabel.alignment = TextAnchor.MiddleRight;
233+
234+
subtitleLabel = new GUIStyle();
235+
subtitleLabel.fontSize = 14;
236+
subtitleLabel.normal.textColor = Color.black;
237+
subtitleLabel.fixedHeight = 15;
238+
subtitleLabel.alignment = TextAnchor.MiddleLeft;
239+
240+
styleHelpboxInner = new GUIStyle("HelpBox");
241+
styleHelpboxInner.padding = new RectOffset(6, 6, 6, 6);
242+
}
243+
244+
245+
246+
static long totalSize = 0;
247+
//++++++++++++++++++++++++++++++++++++++++++++++++++++ GET ALL TEXTURE SIZE IN ASSETS FOLDER +++++++++++++++++++++++++++++++++++++++++++++
248+
249+
private static void GetCompressedFileSize()
250+
{
251+
Object[] AllObjects = Resources.FindObjectsOfTypeAll(typeof(Texture));
252+
foreach (Object currentObj in AllObjects)
253+
{
254+
string tempPath = AssetDatabase.GetAssetPath(currentObj);
255+
if (!string.IsNullOrEmpty(tempPath))
256+
{
257+
string guid = AssetDatabase.AssetPathToGUID(tempPath);
258+
string p = Path.GetFullPath(Application.dataPath + "../../Library/metadata/" + guid.Substring(0, 2) + "/" + guid);
259+
if (File.Exists(p))
260+
{
261+
var file = new FileInfo(p);
262+
totalSize += file.Length;
263+
}
264+
}
265+
}
266+
// Debug.LogError("Total Size "+GetFormattedSize(totalSize));
267+
}
268+
269+
static string GetFormattedSize(double tempSizeInBytes)
270+
{
271+
if(tempSizeInBytes>=1024)
272+
{
273+
return (tempSizeInBytes/1024).ToString("00") +" Kb";
274+
}
275+
if(tempSizeInBytes>=1048576)
276+
{
277+
return ((tempSizeInBytes/1024)/1024).ToString("00") + " Mb";
278+
}
279+
else
280+
{
281+
return tempSizeInBytes.ToString()+ "Bytes";
282+
}
283+
}
284+
285+
286+
287+
288+
289+
290+
291+
//++++++++++++++++++++++++++++++++++++++++++++++++++++ QUICK ACTIONS +++++++++++++++++++++++++++++++++++++++++++++
292+
[MenuItem("[Master_Tools]/Quick Actions/Delete Prefs %#x")]
293+
static void DeletePrefs()
294+
{
295+
if(EditorUtility.DisplayDialog("Delete All PlayerPrefs?", "Do you really want to delete all the PlayerPrefs?", "Yes", "No") == true)
296+
297+
{
298+
299+
PlayerPrefs.DeleteAll() ;
300+
}
301+
}
302+
303+
[MenuItem ("[Master_Tools]/Quick Actions/Disable All #d")]
304+
static void DisableEverything()
305+
{
306+
foreach(GameObject go in Selection.gameObjects)
307+
{
308+
if(go.name == "Camera")
309+
{
310+
for(int count = 0;count<go.transform.childCount;count++)
311+
{
312+
go.transform.GetChild(count).gameObject.SetActive(false);
313+
}
314+
}
315+
}
316+
}
317+
[MenuItem("[Master_Tools]/Quick Actions/Delete #x")]
318+
static void DeleteSelectedItem()
319+
{
320+
foreach(GameObject go in Selection.gameObjects)
321+
{
322+
DestroyImmediate(go);
323+
}
324+
}
325+
326+
[MenuItem("[Master_Tools]/Quick Actions/Disable or Enable _d")]
327+
static void DisableOrEnableObject()
328+
{
329+
foreach(GameObject go in Selection.gameObjects)
330+
{
331+
for(int count = 0;count<Selection.gameObjects.Length;count++)
332+
{
333+
go.SetActive(!go.activeInHierarchy);
334+
}
335+
}
336+
}
337+
338+
}
339+

0 commit comments

Comments
 (0)