Skip to content

Instantly share code, notes, and snippets.

@duynguye
Created April 24, 2019 02:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duynguye/2ae6680068c44e574390d7e9874d4f83 to your computer and use it in GitHub Desktop.
Save duynguye/2ae6680068c44e574390d7e9874d4f83 to your computer and use it in GitHub Desktop.
using UnityEngine;
using Unity.Networking.Transport;
using Unity.Networking.Transport.Utilities;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using NetworkConnection = Unity.Networking.Transport.NetworkConnection;
public class ClientBehavior : MonoBehaviour
{
public UdpNetworkDriver m_Driver;
public NetworkPipeline m_Pipeline;
public NetworkConnection m_Connection;
public NetworkEndPoint m_Endpoint;
public bool Done;
// Start is called before the first frame update
void Start()
{
m_Driver = new UdpNetworkDriver(new SimulatorUtility.Parameters { MaxPacketSize = 256, MaxPacketCount = 30, PacketDelayMs = 100 });
m_Pipeline = m_Driver.CreatePipeline(typeof(UnreliableSequencedPipelineStage), typeof(SimulatorPipelineStage));
m_Connection = default;
m_Endpoint = new NetworkEndPoint();
m_Endpoint = NetworkEndPoint.Parse("127.0.0.1", 9000);
m_Connection = m_Driver.Connect(m_Endpoint);
}
public void OnDestroy()
{
m_Driver.Dispose();
}
// Update is called once per frame
void Update()
{
m_Driver.ScheduleUpdate().Complete();
if (!m_Connection.IsCreated)
{
if (!Done)
{
Debug.Log("Something went wrong during connection");
}
return;
}
DataStreamReader stream;
NetworkEvent.Type cmd;
while ((cmd = m_Connection.PopEvent(m_Driver, out stream)) != NetworkEvent.Type.Empty)
{
if (cmd == NetworkEvent.Type.Connect)
{
Debug.Log("We are now connected to the server.");
var value = 1;
using (var writer = new DataStreamWriter(4, Allocator.Temp))
{
writer.Write(value);
m_Connection.Send(m_Driver, writer);
}
}
else if (cmd == NetworkEvent.Type.Data)
{
var readerCtx = default(DataStreamReader.Context);
uint value = stream.ReadUInt(ref readerCtx);
Debug.Log("Got the value = " + value + " back from the server.");
Done = true;
m_Connection.Disconnect(m_Driver);
m_Connection = default;
}
else if (cmd == NetworkEvent.Type.Disconnect)
{
Debug.Log("Client got disconnected from the server.");
m_Connection = default;
}
}
}
}
@RickyJMJ-zz
Copy link

The documents on Unity github is still outdated, so this helped me out a lot. Thanks.

@Jacob-Rose
Copy link

This is more up to date than official Unity GitHub. Much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment