-
-
Save kou-yeung/d9cada3653ed1b6acccdfc7a08e8564c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Clientは Unityを使います | |
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Net.Sockets; | |
using System.IO; | |
using System.Text; | |
using System; | |
public class Application : MonoBehaviour | |
{ | |
TcpClient client; | |
NetworkStream stream; | |
void Start () | |
{ | |
client = new TcpClient("127.0.0.1", 2001); | |
stream = client.GetStream(); | |
} | |
public void Poll() | |
{ | |
while (client.Available > 0) | |
{ | |
Command cmd; | |
if(TryParseCommand(out cmd)) | |
{ | |
commandExec(this, cmd); | |
} | |
} | |
} | |
bool TryParseCommand(out Command cmd) | |
{ | |
// stream -> Command : 今回は解説しない | |
return true; | |
} | |
void Update () | |
{ | |
Poll(); | |
} | |
// ボタンクリックイベント | |
public void OnClick() | |
{ | |
var bytes = Pack("Hello World!!!!"); | |
stream.Write(bytes, 0, bytes.Length); | |
stream.Flush(); | |
} | |
// Workaround : Jsonなどの形式にシリアライズ。(各自実装) | |
byte[] Pack(object obj) | |
{ | |
// object -> byte[] | |
return new byte[0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment