I_Jemin

Add demo script


// TCP로 통신할 때는 정의를 유효하게 하여 주십시오.
#define USE_TRANSPORT_TCP
using UnityEngine;
using System.Collections;
using System.Net;
public class LibrarySample : MonoBehaviour {
// 통신 모듈.
public GameObject transportTcpPrefab;
public GameObject transportUdpPrefab;
// 통신용 변수.
#if USE_TRANSPORT_TCP
TransportTCP m_transport = null;
#else
TransportUDP m_transport = null;
#endif
// 접속할 IP 주소.
private string m_strings = "";
// 접속할 포트 번호.
private const int m_port = 50765;
private const int m_mtu = 1400;
private bool isSelected = false;
// Use this for initialization
void Start ()
{
// Transport 클래스의 컴포넌트를 가져온다.
#if USE_TRANSPORT_TCP
GameObject obj = GameObject.Instantiate(transportTcpPrefab) as GameObject;
m_transport = obj.GetComponent<TransportTCP>();
#else
GameObject obj = GameObject.Instantiate(transportUdpPrefab) as GameObject;
m_transport = obj.GetComponent<TransportUDP>();
#endif
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
System.Net.IPAddress hostAddress = hostEntry.AddressList[0];
Debug.Log(hostEntry.HostName);
m_strings = hostAddress.ToString();
}
// Update is called once per frame
void Update ()
{
if (m_transport != null && m_transport.isConnected == true) {
byte[] buffer = new byte[m_mtu];
int recvSize = m_transport.Receive(ref buffer, buffer.Length);
if (recvSize > 0) {
string message = System.Text.Encoding.UTF8.GetString(buffer);
Debug.Log(message);
}
}
}
void OnGUI()
{
if (isSelected == false) {
OnGUISelectHost();
}
else {
if (m_transport.isServer == true) {
OnGUIServer();
}
else {
OnGUIClient();
}
}
}
void OnGUISelectHost()
{
#if USE_TRANSPORT_TCP
if (GUI.Button (new Rect (20,40, 150,20), "Launch server.")) {
#else
if (GUI.Button (new Rect (20,40, 150,20), "Launch Listener.")) {
#endif
m_transport.StartServer(m_port, 1);
isSelected = true;
}
// 클라이언트를 선택했을 때 접속할 서버 주소를 입력합니다.
m_strings = GUI.TextField(new Rect(20, 100, 200, 20), m_strings);
#if USE_TRANSPORT_TCP
if (GUI.Button (new Rect (20,70,150,20), "Connect to server")) {
#else
if (GUI.Button (new Rect (20,70,150,20), "Connect to terminal")) {
#endif
m_transport.Connect(m_strings, m_port);
isSelected = true;
m_strings = "";
}
}
void OnGUIServer()
{
#if USE_TRANSPORT_TCP
if (GUI.Button (new Rect (20,60, 150,20), "Stop server")) {
#else
if (GUI.Button (new Rect (20,60, 150,20), "Stop Listener")) {
#endif
m_transport.StopServer();
isSelected = false;
m_strings = "";
}
}
void OnGUIClient()
{
// 클라이언트를 선택했을 때 접속할 서버의 주소를 입력합니다.
if (GUI.Button (new Rect (20,70,150,20), "Send message")) {
byte[] buffer = System.Text.Encoding.UTF8.GetBytes("Hellow, this is client.");
m_transport.Send(buffer, buffer.Length);
}
if (GUI.Button (new Rect (20,100, 150,20), "Disconnect")) {
m_transport.Disconnect();
isSelected = false;
m_strings = "";
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: aa61c0cf4489b43888605561c5b33086
timeCreated: 1517159051
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -20,7 +20,7 @@ MonoBehaviour:
m_ShowMode: 4
m_Title:
m_RootView: {fileID: 6}
m_MinSize: {x: 950, y: 300}
m_MinSize: {x: 950, y: 521}
m_MaxSize: {x: 10000, y: 10000}
--- !u!114 &2
MonoBehaviour:
......@@ -40,8 +40,8 @@ MonoBehaviour:
y: 657
width: 567
height: 328
m_MinSize: {x: 102, y: 121}
m_MaxSize: {x: 4002, y: 4021}
m_MinSize: {x: 100, y: 100}
m_MaxSize: {x: 4000, y: 4000}
m_ActualView: {fileID: 15}
m_Panes:
- {fileID: 15}
......@@ -70,7 +70,7 @@ MonoBehaviour:
m_MinSize: {x: 277, y: 192}
m_MaxSize: {x: 4002, y: 8042}
vertical: 1
controlID: 147
controlID: 134
--- !u!114 &4
MonoBehaviour:
m_ObjectHideFlags: 52
......@@ -116,8 +116,8 @@ MonoBehaviour:
y: 0
width: 445
height: 985
m_MinSize: {x: 234, y: 492}
m_MaxSize: {x: 10004, y: 14042}
m_MinSize: {x: 234, y: 471}
m_MaxSize: {x: 10004, y: 14021}
vertical: 1
controlID: 94
--- !u!114 &6
......@@ -185,10 +185,10 @@ MonoBehaviour:
y: 30
width: 1920
height: 985
m_MinSize: {x: 713, y: 492}
m_MaxSize: {x: 18008, y: 14042}
m_MinSize: {x: 713, y: 471}
m_MaxSize: {x: 18008, y: 14021}
vertical: 0
controlID: 93
controlID: 26
--- !u!114 &9
MonoBehaviour:
m_ObjectHideFlags: 52
......@@ -251,8 +251,8 @@ MonoBehaviour:
y: 0
width: 445
height: 500
m_MinSize: {x: 204, y: 221}
m_MaxSize: {x: 4004, y: 4021}
m_MinSize: {x: 200, y: 200}
m_MaxSize: {x: 4000, y: 4000}
m_ActualView: {fileID: 16}
m_Panes:
- {fileID: 16}
......@@ -391,7 +391,7 @@ MonoBehaviour:
scrollPos: {x: 0, y: 0}
m_SelectedIDs:
m_LastClickedID: 0
m_ExpandedIDs: 92fbffff00000000
m_ExpandedIDs: 9efbffff00000000
m_RenameOverlay:
m_UserAcceptedRename: 0
m_Name:
......@@ -498,9 +498,9 @@ MonoBehaviour:
m_IsLocked: 0
m_FolderTreeState:
scrollPos: {x: 0, y: 0}
m_SelectedIDs: a0270000
m_LastClickedID: 10144
m_ExpandedIDs: 000000009227000000ca9a3bffffff7f
m_SelectedIDs: 6e270000
m_LastClickedID: 10094
m_ExpandedIDs: 000000006027000000ca9a3bffffff7f
m_RenameOverlay:
m_UserAcceptedRename: 0
m_Name:
......@@ -528,7 +528,7 @@ MonoBehaviour:
scrollPos: {x: 0, y: 0}
m_SelectedIDs:
m_LastClickedID: 0
m_ExpandedIDs: 0000000092270000
m_ExpandedIDs: 0000000060270000
m_RenameOverlay:
m_UserAcceptedRename: 0
m_Name:
......
f4d2a52a7ae35650c675960e6c91d6581973cd3e
c6a089c25c4e53a451f487b9c3cd774f1cfca7c4
......