- Home /
Similar networking options in unity?
Hi there,
I have a question regarding Unity networking... At the moment I am trying to decide if it's best to move from GameMaker Studio to Unity for our 3d multiplayer RPG. We have put a lot of work in to our game already but we are kind of tired to work with a bad 3d engine like GameMaker and have to reinvent the wheel for some basic 3d visuals all the time...
The biggest problem we think we should have when porting from GameMaker to Unity is the networking aspect (a side from our little experience in Unityscripting). Our game at the moment works with simple buffers and sockets. Here is an example of our server code:
while(socket_read_message(socket, global.buffer)) {
a = readbyte(); //Message ID
switch(a) {
case 1:
point_x = readshort();
point_y = readshort();
//Move player to location and send the new location to other clients...
break;
}
case 2:
//Other message...
}
}
}
This is a snippet from the server message receiving part... A client sending a message will look like:
clearbuffer()
writebyte(1) //Message ID
writeshort(point_x)
writeshort(point_y)
sendmessage(global.socket)
So after showing some of this GameMaker code... What would be the best approach to do this in Unity? We don't want auto syncing and stuff because it's a point and click game, so it only has to send a movement update when a player clicks and moves to a new location.
Answer by FLASHDENMARK · Mar 21, 2014 at 08:43 PM
You can create your own network back-end if you want. Or you may use existing ones. Unity provides one out of the box, but personally I would only use it until you become familiar with Unity and networking in general(from the sound of it you got networking covered).
You may also use Photon Unity Networking - a third party framework for Unity. There are others as well but Photon Unity Networking(PUN) is the easiest solution I have worked with. It is fast, reliable free, very easy to implement(works pretty much as an extension to Unity).
There are a few default ways of sending data either UDP-like and TCP-like. You can send data as easy as one line of code, supporting several data types and receivers:
//Sends a simple message to all other clients
photonView.RPC("Network_Message", PhotonTargets.Others, "I am sending others a message");
And receive network data easily:
[RPC]
void Network_Message (string msg){
Debug.Log("Message received " + msg);
}
Networking in Unity can be very simple and using PUN sending and receiving data is simple as the above examples.
I do not know GameMaker but I doubt when you have settled and "imported" your game into Unity you will feel any regrets. Whether it be networking or not Unity is simple. I will link to PUN so you can read about it and see how simple it is( you can also download a simple project from the Assets Store that very simply show different ways of networking).
Answer by Kiloblargh · Mar 21, 2014 at 08:51 PM
I am using a very similar approach in Unity; I have an indefinite number of controllable units that do not have their own Network Views. Instead, there is one game controller object with one network view per player, I'm keeping track of all moving units, turning their positions into an array of shorts, first serializing the length of the array, then using that to reconstruct the original array on the receiving side.
Read the documentation on OnSerializeNetworkView and BitStream; your existing code should directly port rather well. (Without actually doing anything with sockets.)
Also, you can send a byte[] as an RPC parameter; which is not in the documentation.