FragMate CommunityLog in

FragMate - Software and Game Developer Community, including Resources, Forums, Marketplace & More.

Share

descriptionNetworking in Unity

more_horiz
So, I just wanted to give a brainstorm to everyone about the basics of networking in Unity. This is just LAN you can experiment with this in your computer. I have made two scripts by myself. This first one spawns the player and again this is very limited you can do so much with it this is just the basics.

Note- Name it as "PlayerSpawner", you can attach it to an empty game object, add network view game component to your player.

Code:


using UnityEngine;
using System.Collections;

public class PlayerSpawner : MonoBehaviour {

   public Transform player;

   void OnServerInitialized(){
      SpawnPlayer ();
   }
   void OnConnectedToServer(){
      SpawnPlayer ();
      }
   void SpawnPlayer(){
      Network.Instantiate (player, transform.position, transform.rotation, 0);
   }
   void OnPlayerDisconnected(NetworkPlayer player){
      Network.RemoveRPCs (player);
      Network.DestroyPlayerObjects (player);
   }
   void OnDisconnectedFromServer(NetworkDisconnection info){
      Network.RemoveRPCs (Network.player);
      Network.DestroyPlayerObjects (Network.player);
      Application.LoadLevel (Application.loadedLevel);
   }
}


And this second one is the front-end of your networking you can attach to your Main Camera.
Note - Name it as "Networking", note that the IP adddress is your computer's IP and again you can experiment with this and do so much.

Code:


using UnityEngine;
using System.Collections;

public class Networking : MonoBehaviour {

   string ServerIP = "127.0.0.1";
   int ServerPort = 30000;

   void OnGUI(){
      if (Network.peerType == NetworkPeerType.Disconnected) {
         if(GUILayout.Button("Connect")){
            Network.Connect(ServerIP, ServerPort);
         }
         if(GUILayout.Button("New Server")){
            Network.InitializeServer(32, ServerPort, false);
         }
            }
      else
      {
         if(GUILayout.Button("Disconnect")){
            Network.Disconnect();
         }
      }
   }
}


descriptionRe: Networking in Unity

more_horiz
Nice

_________________
Game Developer
YouTube Channel: [You must be registered and logged in to see this link.]
Website: [You must be registered and logged in to see this link.]

descriptionRe: Networking in Unity

more_horiz
[You must be registered and logged in to see this link.] wrote:
So, I just wanted to give a brainstorm to everyone about the basics of networking in Unity. This is just LAN you can experiment with this in your computer. I have made two scripts by myself. This first one spawns the player and again this is very limited you can do so much with it this is just the basics.

Note- Name it as "PlayerSpawner", you can attach it to an empty game object, add network view game component to your player.

Code:


using UnityEngine;
using System.Collections;

public class PlayerSpawner : MonoBehaviour {

   public Transform player;

   void OnServerInitialized(){
      SpawnPlayer ();
   }
   void OnConnectedToServer(){
      SpawnPlayer ();
      }
   void SpawnPlayer(){
      Network.Instantiate (player, transform.position, transform.rotation, 0);
   }
   void OnPlayerDisconnected(NetworkPlayer player){
      Network.RemoveRPCs (player);
      Network.DestroyPlayerObjects (player);
   }
   void OnDisconnectedFromServer(NetworkDisconnection info){
      Network.RemoveRPCs (Network.player);
      Network.DestroyPlayerObjects (Network.player);
      Application.LoadLevel (Application.loadedLevel);
   }
}


And this second one is the front-end of your networking you can attach to your Main Camera.
Note - Name it as "Networking", note that the IP adddress is your computer's IP and again you can experiment with this and do so much.

Code:


using UnityEngine;
using System.Collections;

public class Networking : MonoBehaviour {

   string ServerIP = "127.0.0.1";
   int ServerPort = 30000;

   void OnGUI(){
      if (Network.peerType == NetworkPeerType.Disconnected) {
         if(GUILayout.Button("Connect")){
            Network.Connect(ServerIP, ServerPort);
         }
         if(GUILayout.Button("New Server")){
            Network.InitializeServer(32, ServerPort, false);
         }
            }
      else
      {
         if(GUILayout.Button("Disconnect")){
            Network.Disconnect();
         }
      }
   }
}




Simple but neat, you may wan't to implement some more UI stuff.
Working with the Master Server can be better looking when you have a better UI
And remember, you will need to portforward any sort of port the server is started on.
This is a neat script, thank you for sharing!

_________________
-BryceCain25

descriptionRe: Networking in Unity

more_horiz
Is this can work for Multiplayer games using Host IP?

descriptionRe: Networking in Unity

more_horiz
u can use this

[You must be registered and logged in to see this link.]

aswell

_________________
Permissions in this forum:
You cannot reply to topics in this forum