forked from haneytron/simplsockets
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUnity3D.cs
117 lines (98 loc) · 3.64 KB
/
Unity3D.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using SimplMessage;
using System.Net;
using System.Threading.Tasks;
using System.Threading;
namespace SimplSocketsClient
{
public class Unity3D
{
private ClientMonoBehaviour _clientMonoBehaviour;
private GameObjectMonoBehaviour _gameObjectMonoBehaviour;
public void Start()
{
CreateClient();
CreateServer();
}
/// Simulates calling of the Unity Monobehaviour classes
async void CreateClient()
{
// Create the Unity objects
_clientMonoBehaviour = new ClientMonoBehaviour();
_gameObjectMonoBehaviour = new GameObjectMonoBehaviour();
// Simulate Unity start call
_clientMonoBehaviour.Start();
_gameObjectMonoBehaviour.Start();
// Simulate Unity update call loop
await Task.Run(() =>
{
while (true)
{
Thread.Sleep(16); // 60 FPS
_clientMonoBehaviour.Update();
_gameObjectMonoBehaviour.Update();
}
});
}
void CreateServer()
{
// Create the server
var server = new SimplMessageServer();
// Start listening for client connections on loopback end point
server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));
// Wait until a new client has connected
var connectedClient = server.WaitForNewClient();
// Create an object to send
var objectToSend = new ClassA() { VarInt = 2, VarDouble = 2.5 };
// Send it with an implicit descriptor (which is the class name)
Console.WriteLine($"server sending received message: {objectToSend.VarDouble}, {objectToSend.VarInt} with implicit descriptor {typeof(ClassA).Name}");
server.Send(objectToSend, connectedClient);
}
}
// Define a class to be send
public class ClassA
{
public int VarInt;
public double VarDouble;
}
/// Example of a class that in unity would typically derive from MonoBehaviour,
/// This class implements a global client
public class ClientMonoBehaviour
{
private SimplMessageClient _client;
public void Start()
{
// Get the singleton instance of SimplMessageClient
_client = SimplMessageClient.Instance;
// Make the client connect automatically
_client.AutoConnect();
}
public void Update()
{
// Go through all messages that are queued and process them during the Update loopfunction
_client.UpdateCallbacks();
}
}
/// Example of a class that in unity would typically derive from MonoBehaviour
/// This object is typically connected to a GameObject
public class GameObjectMonoBehaviour
{
private SimplMessageClient _client;
public void Start()
{
// Get the singleton instance of SimplMessageClient
_client = SimplMessageClient.Instance;
// This callback is triggered when a the UpdateCallbacks() function is being run
_client.AddUpdateCallBack<ClassA>((receivedMessage) =>
{
// Get data from received message cast to ClassA
var receivedObject = receivedMessage.GetContent<ClassA>();
Console.WriteLine($"Client received message: {receivedObject.VarDouble}, {receivedObject.VarInt}");
});
}
public void Update()
{
// Do something a game object does
}
}
}