I_Jemin

Add demo script

1 +
2 +// TCP로 통신할 때는 정의를 유효하게 하여 주십시오.
3 +#define USE_TRANSPORT_TCP
4 +
5 +using UnityEngine;
6 +using System.Collections;
7 +using System.Net;
8 +
9 +
10 +public class LibrarySample : MonoBehaviour {
11 +
12 + // 통신 모듈.
13 + public GameObject transportTcpPrefab;
14 + public GameObject transportUdpPrefab;
15 +
16 + // 통신용 변수.
17 +#if USE_TRANSPORT_TCP
18 + TransportTCP m_transport = null;
19 +#else
20 + TransportUDP m_transport = null;
21 +#endif
22 +
23 + // 접속할 IP 주소.
24 + private string m_strings = "";
25 +
26 + // 접속할 포트 번호.
27 + private const int m_port = 50765;
28 +
29 + private const int m_mtu = 1400;
30 +
31 + private bool isSelected = false;
32 +
33 +
34 + // Use this for initialization
35 + void Start ()
36 + {
37 + // Transport 클래스의 컴포넌트를 가져온다.
38 +#if USE_TRANSPORT_TCP
39 + GameObject obj = GameObject.Instantiate(transportTcpPrefab) as GameObject;
40 + m_transport = obj.GetComponent<TransportTCP>();
41 +#else
42 + GameObject obj = GameObject.Instantiate(transportUdpPrefab) as GameObject;
43 + m_transport = obj.GetComponent<TransportUDP>();
44 +#endif
45 +
46 + IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
47 + System.Net.IPAddress hostAddress = hostEntry.AddressList[0];
48 + Debug.Log(hostEntry.HostName);
49 + m_strings = hostAddress.ToString();
50 + }
51 +
52 + // Update is called once per frame
53 + void Update ()
54 + {
55 + if (m_transport != null && m_transport.isConnected == true) {
56 + byte[] buffer = new byte[m_mtu];
57 + int recvSize = m_transport.Receive(ref buffer, buffer.Length);
58 + if (recvSize > 0) {
59 + string message = System.Text.Encoding.UTF8.GetString(buffer);
60 + Debug.Log(message);
61 + }
62 + }
63 + }
64 +
65 + void OnGUI()
66 + {
67 + if (isSelected == false) {
68 + OnGUISelectHost();
69 + }
70 + else {
71 + if (m_transport.isServer == true) {
72 + OnGUIServer();
73 + }
74 + else {
75 + OnGUIClient();
76 + }
77 + }
78 + }
79 +
80 + void OnGUISelectHost()
81 + {
82 +#if USE_TRANSPORT_TCP
83 + if (GUI.Button (new Rect (20,40, 150,20), "Launch server.")) {
84 +#else
85 + if (GUI.Button (new Rect (20,40, 150,20), "Launch Listener.")) {
86 +#endif
87 + m_transport.StartServer(m_port, 1);
88 + isSelected = true;
89 + }
90 +
91 + // 클라이언트를 선택했을 때 접속할 서버 주소를 입력합니다.
92 + m_strings = GUI.TextField(new Rect(20, 100, 200, 20), m_strings);
93 +#if USE_TRANSPORT_TCP
94 + if (GUI.Button (new Rect (20,70,150,20), "Connect to server")) {
95 +#else
96 + if (GUI.Button (new Rect (20,70,150,20), "Connect to terminal")) {
97 +#endif
98 + m_transport.Connect(m_strings, m_port);
99 + isSelected = true;
100 + m_strings = "";
101 + }
102 + }
103 +
104 + void OnGUIServer()
105 + {
106 +#if USE_TRANSPORT_TCP
107 + if (GUI.Button (new Rect (20,60, 150,20), "Stop server")) {
108 +#else
109 + if (GUI.Button (new Rect (20,60, 150,20), "Stop Listener")) {
110 +#endif
111 + m_transport.StopServer();
112 + isSelected = false;
113 + m_strings = "";
114 + }
115 + }
116 +
117 +
118 + void OnGUIClient()
119 + {
120 + // 클라이언트를 선택했을 때 접속할 서버의 주소를 입력합니다.
121 + if (GUI.Button (new Rect (20,70,150,20), "Send message")) {
122 + byte[] buffer = System.Text.Encoding.UTF8.GetBytes("Hellow, this is client.");
123 + m_transport.Send(buffer, buffer.Length);
124 + }
125 +
126 + if (GUI.Button (new Rect (20,100, 150,20), "Disconnect")) {
127 + m_transport.Disconnect();
128 + isSelected = false;
129 + m_strings = "";
130 + }
131 + }
132 +
133 +}
...\ No newline at end of file ...\ No newline at end of file
1 +fileFormatVersion: 2
2 +guid: aa61c0cf4489b43888605561c5b33086
3 +timeCreated: 1517159051
4 +licenseType: Pro
5 +MonoImporter:
6 + externalObjects: {}
7 + serializedVersion: 2
8 + defaultReferences: []
9 + executionOrder: 0
10 + icon: {instanceID: 0}
11 + userData:
12 + assetBundleName:
13 + assetBundleVariant:
...@@ -20,7 +20,7 @@ MonoBehaviour: ...@@ -20,7 +20,7 @@ MonoBehaviour:
20 m_ShowMode: 4 20 m_ShowMode: 4
21 m_Title: 21 m_Title:
22 m_RootView: {fileID: 6} 22 m_RootView: {fileID: 6}
23 - m_MinSize: {x: 950, y: 300} 23 + m_MinSize: {x: 950, y: 521}
24 m_MaxSize: {x: 10000, y: 10000} 24 m_MaxSize: {x: 10000, y: 10000}
25 --- !u!114 &2 25 --- !u!114 &2
26 MonoBehaviour: 26 MonoBehaviour:
...@@ -40,8 +40,8 @@ MonoBehaviour: ...@@ -40,8 +40,8 @@ MonoBehaviour:
40 y: 657 40 y: 657
41 width: 567 41 width: 567
42 height: 328 42 height: 328
43 - m_MinSize: {x: 102, y: 121} 43 + m_MinSize: {x: 100, y: 100}
44 - m_MaxSize: {x: 4002, y: 4021} 44 + m_MaxSize: {x: 4000, y: 4000}
45 m_ActualView: {fileID: 15} 45 m_ActualView: {fileID: 15}
46 m_Panes: 46 m_Panes:
47 - {fileID: 15} 47 - {fileID: 15}
...@@ -70,7 +70,7 @@ MonoBehaviour: ...@@ -70,7 +70,7 @@ MonoBehaviour:
70 m_MinSize: {x: 277, y: 192} 70 m_MinSize: {x: 277, y: 192}
71 m_MaxSize: {x: 4002, y: 8042} 71 m_MaxSize: {x: 4002, y: 8042}
72 vertical: 1 72 vertical: 1
73 - controlID: 147 73 + controlID: 134
74 --- !u!114 &4 74 --- !u!114 &4
75 MonoBehaviour: 75 MonoBehaviour:
76 m_ObjectHideFlags: 52 76 m_ObjectHideFlags: 52
...@@ -116,8 +116,8 @@ MonoBehaviour: ...@@ -116,8 +116,8 @@ MonoBehaviour:
116 y: 0 116 y: 0
117 width: 445 117 width: 445
118 height: 985 118 height: 985
119 - m_MinSize: {x: 234, y: 492} 119 + m_MinSize: {x: 234, y: 471}
120 - m_MaxSize: {x: 10004, y: 14042} 120 + m_MaxSize: {x: 10004, y: 14021}
121 vertical: 1 121 vertical: 1
122 controlID: 94 122 controlID: 94
123 --- !u!114 &6 123 --- !u!114 &6
...@@ -185,10 +185,10 @@ MonoBehaviour: ...@@ -185,10 +185,10 @@ MonoBehaviour:
185 y: 30 185 y: 30
186 width: 1920 186 width: 1920
187 height: 985 187 height: 985
188 - m_MinSize: {x: 713, y: 492} 188 + m_MinSize: {x: 713, y: 471}
189 - m_MaxSize: {x: 18008, y: 14042} 189 + m_MaxSize: {x: 18008, y: 14021}
190 vertical: 0 190 vertical: 0
191 - controlID: 93 191 + controlID: 26
192 --- !u!114 &9 192 --- !u!114 &9
193 MonoBehaviour: 193 MonoBehaviour:
194 m_ObjectHideFlags: 52 194 m_ObjectHideFlags: 52
...@@ -251,8 +251,8 @@ MonoBehaviour: ...@@ -251,8 +251,8 @@ MonoBehaviour:
251 y: 0 251 y: 0
252 width: 445 252 width: 445
253 height: 500 253 height: 500
254 - m_MinSize: {x: 204, y: 221} 254 + m_MinSize: {x: 200, y: 200}
255 - m_MaxSize: {x: 4004, y: 4021} 255 + m_MaxSize: {x: 4000, y: 4000}
256 m_ActualView: {fileID: 16} 256 m_ActualView: {fileID: 16}
257 m_Panes: 257 m_Panes:
258 - {fileID: 16} 258 - {fileID: 16}
...@@ -391,7 +391,7 @@ MonoBehaviour: ...@@ -391,7 +391,7 @@ MonoBehaviour:
391 scrollPos: {x: 0, y: 0} 391 scrollPos: {x: 0, y: 0}
392 m_SelectedIDs: 392 m_SelectedIDs:
393 m_LastClickedID: 0 393 m_LastClickedID: 0
394 - m_ExpandedIDs: 92fbffff00000000 394 + m_ExpandedIDs: 9efbffff00000000
395 m_RenameOverlay: 395 m_RenameOverlay:
396 m_UserAcceptedRename: 0 396 m_UserAcceptedRename: 0
397 m_Name: 397 m_Name:
...@@ -498,9 +498,9 @@ MonoBehaviour: ...@@ -498,9 +498,9 @@ MonoBehaviour:
498 m_IsLocked: 0 498 m_IsLocked: 0
499 m_FolderTreeState: 499 m_FolderTreeState:
500 scrollPos: {x: 0, y: 0} 500 scrollPos: {x: 0, y: 0}
501 - m_SelectedIDs: a0270000 501 + m_SelectedIDs: 6e270000
502 - m_LastClickedID: 10144 502 + m_LastClickedID: 10094
503 - m_ExpandedIDs: 000000009227000000ca9a3bffffff7f 503 + m_ExpandedIDs: 000000006027000000ca9a3bffffff7f
504 m_RenameOverlay: 504 m_RenameOverlay:
505 m_UserAcceptedRename: 0 505 m_UserAcceptedRename: 0
506 m_Name: 506 m_Name:
...@@ -528,7 +528,7 @@ MonoBehaviour: ...@@ -528,7 +528,7 @@ MonoBehaviour:
528 scrollPos: {x: 0, y: 0} 528 scrollPos: {x: 0, y: 0}
529 m_SelectedIDs: 529 m_SelectedIDs:
530 m_LastClickedID: 0 530 m_LastClickedID: 0
531 - m_ExpandedIDs: 0000000092270000 531 + m_ExpandedIDs: 0000000060270000
532 m_RenameOverlay: 532 m_RenameOverlay:
533 m_UserAcceptedRename: 0 533 m_UserAcceptedRename: 0
534 m_Name: 534 m_Name:
......
1 -f4d2a52a7ae35650c675960e6c91d6581973cd3e 1 +c6a089c25c4e53a451f487b9c3cd774f1cfca7c4
......