Created
November 14, 2019 00:48
-
-
Save EddieAbbondanzio/81eb3675f8cb8b51acbdc8888f9ab1a3 to your computer and use it in GitHub Desktop.
Unity - How to Create a Basic Server / Client With LiteNetLib
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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using LiteNetLib; | |
public class Client : MonoBehaviour { | |
NetManager netManager; | |
EventBasedNetListener netListener; | |
// Start is called before the first frame update | |
void Start() { | |
netListener = new EventBasedNetListener(); | |
netListener.PeerConnectedEvent += (server) => { | |
Debug.LogError($"Connected to server: {server}"); | |
}; | |
netManager = new NetManager(netListener); | |
netManager.Start(); // Don't forget to call .Start()! | |
netManager.Connect("localhost", 9050); | |
} | |
// Update is called once per frame | |
void Update() { | |
netManager.PollEvents(); | |
} | |
} |
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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using LiteNetLib; | |
public class Server : MonoBehaviour { | |
NetManager netManager; | |
NetListener netListener; | |
// Start is called before the first frame update | |
void Start() { | |
netListener = new EventBasedNetListener(); | |
netListener.ConnectionRequestEvent += (request) => { | |
request.Accept(); | |
}; | |
netListener.PeerConnectedEvent += (client) => { | |
Debug.LogError($"Client connected: {client}"); | |
}; | |
netManager = new NetManager(netListener); | |
} | |
// Update is called once per frame | |
void Update() { | |
netManager.PollEvents(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
need to add next line: