1
+ using System . Collections ;
2
+ using System . Collections . Generic ;
3
+ using UnityEngine ;
4
+ using UnityEditor ;
5
+ using UXF ;
6
+
7
+ namespace UXFTools
8
+ {
9
+
10
+ [ InitializeOnLoad ]
11
+ public class UXFSessionDisplay : EditorWindow
12
+ {
13
+ static Vector2 scrollPos ;
14
+ static Session session ;
15
+ static Dictionary < string , object > settingsDict ;
16
+ static bool parsed ;
17
+
18
+
19
+ [ MenuItem ( "UXF/Show session debugger" ) ]
20
+ static void Init ( )
21
+ {
22
+ FetchReferences ( ) ;
23
+ var window = ( UXFSessionDisplay ) EditorWindow . GetWindow ( typeof ( UXFSessionDisplay ) ) ;
24
+ window . minSize = new Vector2 ( 300 , 500 ) ;
25
+ window . titleContent = new GUIContent ( "UXF Session Debugger" ) ;
26
+ window . Show ( ) ;
27
+ }
28
+
29
+ static UXFSessionDisplay ( )
30
+ {
31
+ EditorApplication . playModeStateChanged += OnPlayModeStateChanged ;
32
+ }
33
+
34
+ static void OnPlayModeStateChanged ( PlayModeStateChange state )
35
+ {
36
+ if ( state == PlayModeStateChange . EnteredPlayMode )
37
+ {
38
+ FetchReferences ( ) ;
39
+ }
40
+ }
41
+
42
+ static void FetchReferences ( )
43
+ {
44
+ session = FindObjectOfType < Session > ( ) ;
45
+ if ( ! session )
46
+ {
47
+ settingsDict = null ;
48
+ return ;
49
+ }
50
+ if ( session . settings == null )
51
+ {
52
+ settingsDict = null ;
53
+ return ;
54
+ }
55
+
56
+ settingsDict = new Dictionary < string , object > ( ) ;
57
+ // log each session setting
58
+ foreach ( string key in session . settings . Keys )
59
+ settingsDict . Add ( key , session . settings [ key ] . ToString ( ) ) ;
60
+
61
+ List < Dictionary < string , object > > blockList = new List < Dictionary < string , object > > ( ) ;
62
+ foreach ( var block in session . blocks )
63
+ {
64
+ Dictionary < string , object > blockDict = new Dictionary < string , object > ( ) ;
65
+ // log each block setting
66
+ foreach ( string key in block . settings . Keys )
67
+ blockDict . Add ( key , block . settings [ key ] . ToString ( ) ) ;
68
+
69
+ List < Dictionary < string , object > > trialList = new List < Dictionary < string , object > > ( ) ;
70
+ // log each trial
71
+ foreach ( var trial in block . trials )
72
+ {
73
+ Dictionary < string , object > trialDict = new Dictionary < string , object > ( ) ;
74
+ // log each trial setting
75
+ foreach ( string key in trial . settings . Keys )
76
+ trialDict . Add ( key , trial . settings [ key ] . ToString ( ) ) ;
77
+
78
+ // add trial to block
79
+ trialList . Add ( trialDict ) ;
80
+ }
81
+ blockDict . Add ( "_____trials" , trialList ) ;
82
+ blockList . Add ( blockDict ) ;
83
+ }
84
+ settingsDict . Add ( "_____blocks" , blockList ) ;
85
+ }
86
+
87
+ public void OnGUI ( )
88
+ {
89
+ EditorGUILayout . Space ( ) ;
90
+ GUIStyle labelStyle = new GUIStyle ( ) ;
91
+ labelStyle . wordWrap = true ;
92
+ labelStyle . margin = new RectOffset ( 6 , 6 , 0 , 0 ) ;
93
+
94
+ GUILayout . Label ( "Press the button below after generating your blocks and trials to list all blocks and trials with their associated setting in the session. This helps you make sure settings are being applied to the correct trials. " , labelStyle ) ;
95
+ if ( GUILayout . Button ( "Fetch session info" ) )
96
+ {
97
+ FetchReferences ( ) ;
98
+ }
99
+
100
+ if ( settingsDict != null )
101
+ {
102
+ scrollPos = EditorGUILayout . BeginScrollView ( scrollPos ) ;
103
+ ParseSettings ( ) ;
104
+ EditorGUILayout . EndScrollView ( ) ;
105
+ return ;
106
+ }
107
+ else if ( ! session )
108
+ {
109
+ EditorGUILayout . HelpBox ( "Did not find a UXF.Session component in the scene! Press button above to try again." , UnityEditor . MessageType . Error ) ;
110
+ return ;
111
+ }
112
+ else if ( ! session . hasInitialised )
113
+ {
114
+ EditorGUILayout . HelpBox ( "Session has not yet started! This debugging tool can be used only when the session has started. Press button above to try again." , UnityEditor . MessageType . Warning ) ;
115
+ return ;
116
+ }
117
+ GUILayout . Label ( "Something went wrong." , labelStyle ) ;
118
+ return ;
119
+
120
+ }
121
+
122
+
123
+ static void ParseSettings ( )
124
+ {
125
+ EditorGUILayout . HelpBox ( "Remember, Settings requests cascade upwards: That means accessing a settings in a trial will first look inside the trial, if it is then not found, will look inside the block, then the session." , UnityEditor . MessageType . Info ) ;
126
+
127
+ GUILayout . Label ( "Session .settings" , EditorStyles . boldLabel ) ;
128
+
129
+ // add more info...
130
+ EditorGUILayout . BeginVertical ( "box" ) ;
131
+
132
+ // log each session setting
133
+ GUIKeyValuePairColumns ( settingsDict ) ;
134
+ EditorGUILayout . Space ( ) ;
135
+
136
+ EditorGUILayout . BeginVertical ( "box" ) ;
137
+
138
+ var blockList = ( List < Dictionary < string , object > > ) settingsDict [ "_____blocks" ] ;
139
+
140
+ int b = 0 ;
141
+ foreach ( var block in blockList )
142
+ {
143
+ GUILayout . Label ( string . Format ( "Block {0} .settings" , ++ b ) , EditorStyles . boldLabel ) ;
144
+ EditorGUILayout . BeginVertical ( "box" ) ;
145
+
146
+ GUIKeyValuePairColumns ( block ) ;
147
+ EditorGUILayout . Space ( ) ;
148
+
149
+ var trialList = ( List < Dictionary < string , object > > ) block [ "_____trials" ] ;
150
+
151
+ int t = 0 ;
152
+ foreach ( var trial in trialList )
153
+ {
154
+ GUILayout . Label ( string . Format ( "Trial {0} .settings" , ++ t ) , EditorStyles . boldLabel ) ;
155
+ EditorGUILayout . BeginVertical ( "box" ) ;
156
+ GUIKeyValuePairColumns ( trial ) ;
157
+
158
+ EditorGUILayout . EndVertical ( ) ;
159
+ }
160
+
161
+
162
+ EditorGUILayout . EndVertical ( ) ;
163
+ }
164
+
165
+ EditorGUILayout . EndVertical ( ) ;
166
+ EditorGUILayout . EndVertical ( ) ;
167
+
168
+ }
169
+
170
+ static void GUIKeyValuePairColumns ( Dictionary < string , object > dict )
171
+ {
172
+ if ( dict . Count == 0 )
173
+ {
174
+ EditorGUILayout . LabelField ( "None" , EditorStyles . miniLabel ) ;
175
+ return ;
176
+ }
177
+
178
+ foreach ( KeyValuePair < string , object > pair in dict )
179
+ {
180
+ string k = pair . Key ;
181
+ if ( k . StartsWith ( "_____" ) ) continue ;
182
+
183
+ GUILayout . BeginHorizontal ( ) ;
184
+ string v = Truncate ( pair . Value . ToString ( ) , 100 ) ;
185
+ EditorGUILayout . LabelField ( string . Format ( "[\" {0}\" ]: {1}" , k , v ) ) ;
186
+ GUILayout . EndHorizontal ( ) ;
187
+ }
188
+
189
+ }
190
+
191
+
192
+ static string Truncate ( string value , int maxChars )
193
+ {
194
+ return value . Length <= maxChars ? value : value . Substring ( 0 , maxChars ) + "..." ;
195
+ }
196
+ }
197
+
198
+ }
0 commit comments