2
2
3
3
namespace UnityWebSocket . Demo
4
4
{
5
- public class UnityWebSocket : MonoBehaviour
5
+ public class UnityWebSocket : MonoBehaviour
6
6
{
7
7
public string address = "ws://127.0.0.1:8080" ;
8
8
public string sendText = "Hello World!" ;
@@ -23,33 +23,44 @@ private void OnGUI()
23
23
var scale = UnityEngine . Screen . width / 800f ;
24
24
GUI . matrix = Matrix4x4 . TRS ( new Vector3 ( 0 , 0 , 0 ) , Quaternion . identity , new Vector3 ( scale , scale , 1 ) ) ;
25
25
var width = GUILayout . Width ( UnityEngine . Screen . width / scale - 10 ) ;
26
-
26
+ var w = UnityEngine . Screen . width / scale - 10 ;
27
27
WebSocketState state = socket == null ? WebSocketState . Closed : socket . ReadyState ;
28
28
29
+ GUIStyle style = new GUIStyle ( GUI . skin . label ) ;
30
+ style . fontSize = 26 ;
31
+
32
+ GUILayout . BeginHorizontal ( ) ;
33
+ GUILayout . Label ( "" , GUILayout . Width ( UnityEngine . Screen . width / scale - 100 ) , GUILayout . Height ( 250 ) ) ;
34
+ GUILayout . EndHorizontal ( ) ;
35
+
29
36
GUILayout . BeginHorizontal ( ) ;
30
- GUILayout . Label ( "SDK Version: " + Settings . VERSION , GUILayout . Width ( UnityEngine . Screen . width / scale - 100 ) ,
31
- GUILayout . Height ( 250 ) ) ;
37
+ GUILayout . Label ( "SDK Version: " + Settings . VERSION , style , GUILayout . Width ( UnityEngine . Screen . width / scale - 300 ) ) ;
32
38
GUI . color = green ;
33
- GUILayout . Label ( $ "FPS: { fps : F2} ", GUILayout . Width ( 80 ) ) ;
39
+ GUILayout . Label ( $ "FPS: { fps : F2} ", style , GUILayout . Width ( 300 ) ) ;
34
40
GUI . color = Color . white ;
35
41
GUILayout . EndHorizontal ( ) ;
36
42
37
43
GUILayout . BeginHorizontal ( ) ;
38
- GUILayout . Label ( "State: " , GUILayout . Width ( 36 ) ) ;
44
+ GUILayout . Label ( "State: " , style , GUILayout . Width ( 70 ) ) ;
39
45
GUI . color = WebSocketState . Closed == state ? red : WebSocketState . Open == state ? green : wait ;
40
- GUILayout . Label ( $ "{ state } ", GUILayout . Width ( 120 ) ) ;
46
+ GUILayout . Label ( $ "{ state } ", style , GUILayout . Width ( 300 ) ) ;
41
47
GUI . color = Color . white ;
42
48
GUILayout . EndHorizontal ( ) ;
43
49
44
50
GUI . enabled = state == WebSocketState . Closed ;
45
- GUILayout . Label ( "Address: " , width ) ;
46
- address = GUILayout . TextField ( address , width ) ;
51
+ GUILayout . Label ( "Address: " , style , width ) ;
52
+ style . normal . background = MakeTex ( 2 , 2 , Color . grey ) ;
53
+ address = GUILayout . TextField ( address , style , width , GUILayout . Height ( 50 ) ) ;
47
54
48
55
GUILayout . BeginHorizontal ( ) ;
56
+ style . alignment = TextAnchor . MiddleCenter ;
57
+ style . normal . background = MakeTex ( 2 , 2 , Color . grey ) ;
58
+
49
59
GUI . enabled = state == WebSocketState . Closed ;
50
- if ( GUILayout . Button ( state == WebSocketState . Connecting ? "Connecting..." : "Connect" ) )
60
+ if ( GUILayout . Button ( state == WebSocketState . Connecting ? "Connecting..." : "Connect" , style , GUILayout . Width ( w / 2 ) , GUILayout . Height ( 45 ) ) )
51
61
{
52
- if ( WebSocketState . Connecting != 0 ) {
62
+ if ( WebSocketState . Connecting != 0 )
63
+ {
53
64
AddLog ( string . Format ( "something is wrong" ) ) ;
54
65
}
55
66
socket = new WebSocket ( address ) ;
@@ -62,31 +73,43 @@ private void OnGUI()
62
73
}
63
74
64
75
GUI . enabled = state == WebSocketState . Open ;
65
- if ( GUILayout . Button ( state == WebSocketState . Closing ? "Closing..." : "Close" ) )
76
+ if ( GUILayout . Button ( state == WebSocketState . Closing ? "Closing..." : "Close" , style , GUILayout . Width ( w / 2 ) , GUILayout . Height ( 45 ) ) )
66
77
{
67
78
AddLog ( string . Format ( "Closing..." ) ) ;
68
79
socket . CloseAsync ( ) ;
69
80
}
70
81
GUILayout . EndHorizontal ( ) ;
71
82
72
- GUILayout . Label ( "Text: " ) ;
73
- sendText = GUILayout . TextArea ( sendText , GUILayout . MinHeight ( 50 ) , width ) ;
83
+ GUILayout . BeginHorizontal ( ) ;
84
+ GUILayout . Label ( "" , GUILayout . Width ( UnityEngine . Screen . width ) , GUILayout . Height ( 20 ) ) ;
85
+ GUILayout . EndHorizontal ( ) ;
86
+
87
+ style . alignment = TextAnchor . UpperLeft ;
88
+ style . normal . background = null ;
89
+ GUI . color = Color . white ;
90
+ GUILayout . Label ( "Text: " , style ) ;
91
+ style . normal . background = MakeTex ( 2 , 2 , Color . grey ) ;
92
+ sendText = GUILayout . TextArea ( sendText , style , GUILayout . MinHeight ( 100 ) , width ) ;
74
93
75
94
GUILayout . BeginHorizontal ( ) ;
76
- if ( GUILayout . Button ( "Send" ) && ! string . IsNullOrEmpty ( sendText ) )
95
+ style . alignment = TextAnchor . MiddleCenter ;
96
+ if ( GUILayout . Button ( "Send" , style , GUILayout . Width ( w / 2 ) , GUILayout . Height ( 45 ) ) && ! string . IsNullOrEmpty ( sendText ) )
77
97
{
78
98
socket . SendAsync ( sendText ) ;
79
99
AddLog ( string . Format ( "Send: {0}" , sendText ) ) ;
80
100
sendCount += 1 ;
81
101
}
82
- if ( GUILayout . Button ( "Send Bytes" ) && ! string . IsNullOrEmpty ( sendText ) )
102
+ if ( GUILayout . Button ( "Send Bytes" , style , GUILayout . Width ( w / 2 ) , GUILayout . Height ( 45 ) ) && ! string . IsNullOrEmpty ( sendText ) )
83
103
{
84
104
var bytes = System . Text . Encoding . UTF8 . GetBytes ( sendText ) ;
85
105
socket . SendAsync ( bytes ) ;
86
106
AddLog ( string . Format ( "Send Bytes ({1}): {0}" , sendText , bytes . Length ) ) ;
87
107
sendCount += 1 ;
88
108
}
89
- if ( GUILayout . Button ( "Send x100" ) && ! string . IsNullOrEmpty ( sendText ) )
109
+ GUILayout . EndHorizontal ( ) ;
110
+
111
+ GUILayout . BeginHorizontal ( ) ;
112
+ if ( GUILayout . Button ( "Send x100" , style , GUILayout . Width ( w / 2 ) , GUILayout . Height ( 45 ) ) && ! string . IsNullOrEmpty ( sendText ) )
90
113
{
91
114
for ( int i = 0 ; i < 100 ; i ++ )
92
115
{
@@ -96,7 +119,7 @@ private void OnGUI()
96
119
sendCount += 1 ;
97
120
}
98
121
}
99
- if ( GUILayout . Button ( "Send Bytes x100" ) && ! string . IsNullOrEmpty ( sendText ) )
122
+ if ( GUILayout . Button ( "Send Bytes x100" , style , GUILayout . Width ( w / 2 ) , GUILayout . Height ( 45 ) ) && ! string . IsNullOrEmpty ( sendText ) )
100
123
{
101
124
for ( int i = 0 ; i < 100 ; i ++ )
102
125
{
@@ -111,21 +134,25 @@ private void OnGUI()
111
134
GUILayout . EndHorizontal ( ) ;
112
135
113
136
GUI . enabled = true ;
114
- GUILayout . BeginHorizontal ( ) ;
115
- logMessage = GUILayout . Toggle ( logMessage , "Log Message" ) ;
116
- GUILayout . Label ( string . Format ( "Send Count: {0}" , sendCount ) ) ;
117
- GUILayout . Label ( string . Format ( "Receive Count: {0}" , receiveCount ) ) ;
118
- GUILayout . EndHorizontal ( ) ;
137
+ // GUILayout.BeginHorizontal();
138
+ // style.normal.background = null;
139
+ // logMessage = GUILayout.Toggle(logMessage, "Log Message", style);
140
+ // GUILayout.Label(string.Format("Send Count: {0}", sendCount), style);
141
+ // GUILayout.Label(string.Format("Receive Count: {0}", receiveCount), style);
142
+ // GUILayout.EndHorizontal();
143
+
144
+ style . normal . background = MakeTex ( 2 , 2 , Color . grey ) ;
119
145
120
- if ( GUILayout . Button ( "Clear" ) )
146
+ if ( GUILayout . Button ( "Clear" , style , GUILayout . Width ( w ) , GUILayout . Height ( 45 ) ) )
121
147
{
122
148
log = "" ;
123
149
receiveCount = 0 ;
124
150
sendCount = 0 ;
125
151
}
126
-
152
+ style . alignment = TextAnchor . UpperLeft ;
127
153
scrollPos = GUILayout . BeginScrollView ( scrollPos , GUILayout . MaxHeight ( UnityEngine . Screen . height / scale - 270 ) , width ) ;
128
- GUILayout . Label ( log ) ;
154
+ style . normal . background = null ;
155
+ GUILayout . Label ( log , style ) ;
129
156
GUILayout . EndScrollView ( ) ;
130
157
}
131
158
@@ -141,6 +168,20 @@ private void AddLog(string str)
141
168
scrollPos . y = int . MaxValue ;
142
169
}
143
170
171
+ // 创建一个纹理
172
+ private Texture2D MakeTex ( int width , int height , Color col )
173
+ {
174
+ Color [ ] pix = new Color [ width * height ] ;
175
+ for ( int i = 0 ; i < pix . Length ; i ++ )
176
+ pix [ i ] = col ;
177
+
178
+ Texture2D result = new Texture2D ( width , height ) ;
179
+ result . SetPixels ( pix ) ;
180
+ result . Apply ( ) ;
181
+
182
+ return result ;
183
+ }
184
+
144
185
private void Socket_OnOpen ( object sender , OpenEventArgs e )
145
186
{
146
187
AddLog ( string . Format ( "Connected: {0}" , address ) ) ;
0 commit comments