1
+ using System . Collections . Generic ;
2
+ using Unity . UIWidgets . editor ;
3
+ using Unity . UIWidgets . foundation ;
4
+ using Unity . UIWidgets . material ;
5
+ using Unity . UIWidgets . painting ;
6
+ using Unity . UIWidgets . rendering ;
7
+ using Unity . UIWidgets . service ;
8
+ using Unity . UIWidgets . ui ;
9
+ using Unity . UIWidgets . widgets ;
10
+ using UnityEditor ;
11
+ using UnityEngine ;
12
+ using Color = Unity . UIWidgets . ui . Color ;
13
+ using Transform = UnityEngine . Transform ;
14
+
15
+ namespace UIWidgetsSample . DragNDrop {
16
+ public class CustomInspectorSample : UIWidgetsEditorWindow {
17
+ [ MenuItem ( "UIWidgetsTests/Drag&Drop/Custom Inspector" ) ]
18
+ public static void ShowEditorWindow ( ) {
19
+ var window = GetWindow < CustomInspectorSample > ( ) ;
20
+ window . titleContent . text = "Custom Inspector Sample" ;
21
+ }
22
+
23
+ protected override void OnEnable ( ) {
24
+ FontManager . instance . addFont ( Resources . Load < Font > ( "MaterialIcons-Regular" ) , "Material Icons" ) ;
25
+ FontManager . instance . addFont ( Resources . Load < Font > ( "GalleryIcons" ) , "GalleryIcons" ) ;
26
+
27
+ base . OnEnable ( ) ;
28
+ }
29
+
30
+ protected override Widget createWidget ( ) {
31
+ Debug . Log ( "[ WIDGET RECREATED ]" ) ;
32
+ return new MaterialApp (
33
+ home : new CustomInspectorSampleWidget ( ) ,
34
+ darkTheme : new ThemeData ( primaryColor : Colors . black26 )
35
+ ) ;
36
+ }
37
+ }
38
+
39
+ public class CustomInspectorSampleWidget : StatefulWidget {
40
+ public CustomInspectorSampleWidget ( Key key = null ) : base ( key ) {
41
+ }
42
+
43
+ public override State createState ( ) {
44
+ return new CustomInspectorSampleWidgetState ( ) ;
45
+ }
46
+ }
47
+
48
+ public class CustomInspectorSampleWidgetState : State < CustomInspectorSampleWidget > {
49
+ GameObject objectRef ;
50
+ Transform transformRef ;
51
+
52
+ TextEditingController textController = new TextEditingController ( ) ;
53
+
54
+ public override void initState ( ) {
55
+ this . textController . addListener ( ( ) => {
56
+ var text = this . textController . text . ToLower ( ) ;
57
+ this . textController . value = this . textController . value . copyWith (
58
+ text : text ,
59
+ selection : new TextSelection ( baseOffset : text . Length , extentOffset : text . Length ) ,
60
+ composing : TextRange . empty
61
+ ) ;
62
+ } ) ;
63
+ base . initState ( ) ;
64
+ }
65
+
66
+ enum ETransfrom {
67
+ Position ,
68
+ Rotation ,
69
+ Scale
70
+ }
71
+
72
+ int oldCursorPosition = 0 ;
73
+
74
+ Widget getCardRow ( ETransfrom type , bool hasRef ) {
75
+ var xValue = hasRef
76
+ ? type == ETransfrom . Position
77
+ ? this . transformRef . position . x . ToString ( )
78
+ : type == ETransfrom . Rotation
79
+ ? this . transformRef . localEulerAngles . x . ToString ( )
80
+ : this . transformRef . localScale . x . ToString ( )
81
+ : "" ;
82
+
83
+ var xValueController = TextEditingController . fromValue (
84
+ new TextEditingValue ( xValue , TextSelection . collapsed ( this . oldCursorPosition ) )
85
+ ) ;
86
+
87
+ var yValue = hasRef
88
+ ? type == ETransfrom . Position
89
+ ? this . transformRef . position . y . ToString ( )
90
+ : type == ETransfrom . Scale
91
+ ? this . transformRef . localEulerAngles . y . ToString ( )
92
+ : this . transformRef . localScale . y . ToString ( )
93
+ : "" ;
94
+
95
+ var yValueController = TextEditingController . fromValue (
96
+ new TextEditingValue ( yValue , TextSelection . collapsed ( this . oldCursorPosition ) )
97
+ ) ;
98
+
99
+ var zValue = hasRef
100
+ ? type == ETransfrom . Position
101
+ ? this . transformRef . position . z . ToString ( )
102
+ : type == ETransfrom . Rotation
103
+ ? this . transformRef . localEulerAngles . z . ToString ( )
104
+ : this . transformRef . localScale . z . ToString ( )
105
+ : "" ;
106
+
107
+ var zValueController = TextEditingController . fromValue (
108
+ new TextEditingValue ( zValue , TextSelection . collapsed ( this . oldCursorPosition ) )
109
+ ) ;
110
+
111
+ return new Column (
112
+ children : new List < Widget > {
113
+ new Container (
114
+ padding : EdgeInsets . symmetric ( vertical : 8f ) ,
115
+ child : new Align (
116
+ alignment : Alignment . centerLeft ,
117
+ child : new Text (
118
+ type == ETransfrom . Position ? "Position" :
119
+ type == ETransfrom . Rotation ? "Rotation" : "Scale" ,
120
+ style : new TextStyle ( fontSize : 16.0f )
121
+ )
122
+ )
123
+ ) ,
124
+ new Row (
125
+ children : new List < Widget > {
126
+ new Flexible (
127
+ flex : 8 ,
128
+ child : new Container (
129
+ decoration : new BoxDecoration (
130
+ color : new Color ( 0xfff5f5f5 ) ) ,
131
+ child : new TextField (
132
+ decoration : new InputDecoration (
133
+ border : new UnderlineInputBorder ( ) ,
134
+ contentPadding :
135
+ EdgeInsets . symmetric (
136
+ horizontal : 10f , vertical : 5f ) ,
137
+ labelText : "X"
138
+ ) ,
139
+ controller : xValueController ,
140
+ onChanged : hasRef
141
+ ? ( str ) => {
142
+ this . setState ( ( ) => {
143
+ float result = 0 ;
144
+ float . TryParse ( str , out result ) ;
145
+ this . oldCursorPosition = xValueController . selection . startPos . offset ;
146
+
147
+ switch ( type ) {
148
+ case ETransfrom . Position :
149
+ var newPos = this . transformRef . position ;
150
+ newPos . x = result ;
151
+ this . transformRef . position = newPos ;
152
+ break ;
153
+ case ETransfrom . Rotation :
154
+ var newRot = this . transformRef . localEulerAngles ;
155
+ newRot . x = result ;
156
+ this . transformRef . localEulerAngles = newRot ;
157
+ break ;
158
+ case ETransfrom . Scale :
159
+ var newScale = this . transformRef . localScale ;
160
+ newScale . x = result ;
161
+ this . transformRef . localScale = newScale ;
162
+ break ;
163
+ }
164
+ } ) ;
165
+ }
166
+ : ( ValueChanged < string > ) null
167
+ )
168
+ ) ) ,
169
+ new Flexible (
170
+ child : new Container ( )
171
+ ) ,
172
+ new Flexible (
173
+ flex : 8 ,
174
+ child : new Container (
175
+ decoration : new BoxDecoration (
176
+ color : new Color ( 0xfff5f5f5 ) ) ,
177
+ child : new TextField (
178
+ decoration : new InputDecoration (
179
+ border : new UnderlineInputBorder ( ) ,
180
+ contentPadding :
181
+ EdgeInsets . symmetric (
182
+ horizontal : 10f , vertical : 5f ) ,
183
+ labelText : "Y"
184
+ ) ,
185
+ controller : yValueController ,
186
+ onChanged : hasRef
187
+ ? ( str ) => {
188
+ this . setState ( ( ) => {
189
+ float result = 0 ;
190
+ float . TryParse ( str , out result ) ;
191
+ this . oldCursorPosition = yValueController . selection . startPos . offset ;
192
+
193
+ switch ( type ) {
194
+ case ETransfrom . Position :
195
+ var newPos = this . transformRef . position ;
196
+ newPos . y = result ;
197
+ this . transformRef . position = newPos ;
198
+ break ;
199
+ case ETransfrom . Rotation :
200
+ var newRot = this . transformRef . localEulerAngles ;
201
+ newRot . y = result ;
202
+ this . transformRef . localEulerAngles = newRot ;
203
+ break ;
204
+ case ETransfrom . Scale :
205
+ var newScale = this . transformRef . localScale ;
206
+ newScale . y = result ;
207
+ this . transformRef . localScale = newScale ;
208
+ break ;
209
+ }
210
+ } ) ;
211
+ }
212
+ : ( ValueChanged < string > ) null
213
+ )
214
+ ) ) ,
215
+ new Flexible (
216
+ child : new Container ( )
217
+ ) ,
218
+ new Flexible (
219
+ flex : 8 ,
220
+ child : new Container (
221
+ decoration : new BoxDecoration (
222
+ color : new Color ( 0xfff5f5f5 ) ) ,
223
+ child : new TextField (
224
+ decoration : new InputDecoration (
225
+ border : new UnderlineInputBorder ( ) ,
226
+ contentPadding :
227
+ EdgeInsets . symmetric (
228
+ horizontal : 10f , vertical : 5f ) ,
229
+ labelText : "Z"
230
+ ) ,
231
+ controller : zValueController ,
232
+ onChanged : hasRef
233
+ ? ( str ) => {
234
+ this . setState ( ( ) => {
235
+ float result = 0 ;
236
+ float . TryParse ( str , out result ) ;
237
+ this . oldCursorPosition = zValueController . selection . startPos . offset ;
238
+ Debug . Log ( result ) ;
239
+
240
+ switch ( type ) {
241
+ case ETransfrom . Position :
242
+ var newPos = this . transformRef . position ;
243
+ newPos . z = result ;
244
+ this . transformRef . position = newPos ;
245
+ break ;
246
+ case ETransfrom . Rotation :
247
+ var newRot = this . transformRef . localEulerAngles ;
248
+ newRot . z = result ;
249
+ this . transformRef . localEulerAngles = newRot ;
250
+ break ;
251
+ case ETransfrom . Scale :
252
+ var newScale = this . transformRef . localScale ;
253
+ newScale . z = result ;
254
+ this . transformRef . localScale = newScale ;
255
+ break ;
256
+ }
257
+ } ) ;
258
+ }
259
+ : ( ValueChanged < string > ) null
260
+ )
261
+ ) )
262
+ }
263
+ )
264
+ }
265
+ ) ;
266
+ }
267
+
268
+ public override Widget build ( BuildContext context ) {
269
+ return new Theme (
270
+ data : new ThemeData (
271
+ appBarTheme : new AppBarTheme (
272
+ color : Colors . purple
273
+ ) ,
274
+ cardTheme : new CardTheme (
275
+ color : Colors . white ,
276
+ elevation : 2.0f
277
+ )
278
+ ) ,
279
+ child : new Scaffold (
280
+ appBar : new AppBar ( title : new Text ( "Custom Inspector" ) ) ,
281
+ body : new ListView (
282
+ children : new List < Widget > {
283
+ new Card (
284
+ clipBehavior : Clip . antiAlias ,
285
+ margin : EdgeInsets . all ( 20.0f ) ,
286
+ shape : new RoundedRectangleBorder (
287
+ borderRadius : BorderRadius . circular ( 20.0f )
288
+ ) ,
289
+ child : new Container (
290
+ padding : EdgeInsets . symmetric ( vertical : 20f , horizontal : 10f ) ,
291
+ child : new Column (
292
+ mainAxisSize : MainAxisSize . min ,
293
+ children : new List < Widget > {
294
+ new UnityObjectDetector (
295
+ onRelease : ( details ) => {
296
+ this . setState ( ( ) => {
297
+ this . objectRef = details . objectReferences [ 0 ] as GameObject ;
298
+ if ( this . objectRef ) {
299
+ this . transformRef = this . objectRef . transform ;
300
+ }
301
+ } ) ;
302
+ } ,
303
+ child : new ListTile (
304
+ title : new Text (
305
+ this . objectRef == null ? "Object Name" : this . objectRef . name ,
306
+ style : new TextStyle ( fontSize : 28.0f ) ) ,
307
+ subtitle : new Text ( "Drag an object here" ,
308
+ style : new TextStyle ( fontSize : 16.0f ) ) ,
309
+ contentPadding : EdgeInsets . symmetric ( horizontal : 10f )
310
+ )
311
+ ) ,
312
+ new Card (
313
+ clipBehavior : Clip . antiAlias ,
314
+ shape : new RoundedRectangleBorder (
315
+ borderRadius : BorderRadius . circular ( 20.0f )
316
+ ) ,
317
+ child : new Container (
318
+ padding : EdgeInsets . symmetric ( horizontal : 10.0f ) ,
319
+ child : new Column (
320
+ mainAxisSize : MainAxisSize . min ,
321
+ children : new List < Widget > {
322
+ new Container (
323
+ padding : EdgeInsets . only ( top : 20f ) ,
324
+ child : new Align (
325
+ alignment : Alignment . centerLeft ,
326
+ child : new Text ( "Transform" ,
327
+ style : new TextStyle ( fontSize : 20.0f ) )
328
+ )
329
+ ) ,
330
+ this . getCardRow ( ETransfrom . Position ,
331
+ this . objectRef != null ) ,
332
+ this . getCardRow ( ETransfrom . Rotation ,
333
+ this . objectRef != null ) ,
334
+ this . getCardRow ( ETransfrom . Scale , this . objectRef != null ) ,
335
+ new Container ( padding : EdgeInsets . only ( bottom : 20f ) )
336
+ }
337
+ )
338
+ )
339
+ ) ,
340
+ }
341
+ )
342
+ )
343
+ )
344
+ }
345
+ )
346
+ )
347
+ ) ;
348
+ }
349
+ }
350
+ }
0 commit comments