1. Create a window
1 // declare window position size 2 Private rect Pos = new rect (,); 3 4 // callback function 5 void wincallback (int id) 6 {7 if (GUI. button (New rect (,), "button in the window") {8 print ("button clicked in the window "); 9} 10} 11 12 Void ongui () 13 {14 Pos = GUI. window (0, POs, wincallback, "my window"); 15}
Effect;
Define a window that can be moved with the mouse:
Add in the callback function
1 void wincallback (int id) 2 {3 if (GUI. button (New rect (,), "button in window") {4 print ("button clicked in window"); 5} 6 7 GUI. dragwindow (); 8}
Note that you need to put this function at the end of the callback function;
If you use
GUI.DragWindow(new Rect(0,0,100,50));
It will limit the area that can be dragged (I tested it, but the effect is not very good );
Use the switch to control whether the display window is displayed or not:
1 private rect Pos = new rect (,); 2 3 // callback function 4 void wincallback (int id) 5 {6 if (GUI. button (New rect (,), "button in the window") {7 print ("button clicked in the window"); 8} 9 10 GUI. dragwindow (New rect (,); 11} 12 13 private bool showwin = true; 14 15 void ongui () 16 {17 showwin = GUI. toggle (New rect (,), showwin, "display window"); 18 19 if (showwin) 20 {21 Pos = GUI. window (0, POs, wincallback, "my window"); 22} 23}
Effect:
C # Writing a dynamic window of Unity basic Gui