Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink
Share
Go down
Member [Lv. 3]
Member [Lv. 3]
Posts : 37
Points : 1667
Rep : 19
Join date : 2014-06-14
View user profile

Networking in Unity

on Fri Aug 22, 2014 1:20 am
Message reputation : 100% (1 vote)
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();
         }
      }
   }
}


avatar
Moderator
Moderator
Posts : 329
Points : 2202
Rep : 43
Join date : 2014-03-12
Age : 18
Location : Italy
View user profilehttp://masterdevelopers.altervista.org/

Re: Networking in Unity

on Fri Aug 22, 2014 3:08 am
Nice
avatar
SUPER MODERATOR
SUPER MODERATOR
Posts : 279
Points : 2167
Rep : 58
Join date : 2013-12-16
Age : 17
Location : Boise Idaho United States
View user profile

Re: Networking in Unity

on Thu Sep 11, 2014 11:30 pm
Ninjaboy follower 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!
Member [Lv. 2]
Member [Lv. 2]
Posts : 5
Points : 1204
Rep : 0
Join date : 2015-05-09
View user profile

Re: Networking in Unity

on Sat May 09, 2015 6:39 am
Is this can work for Multiplayer games using Host IP?
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 66
Points : 1731
Rep : 16
Join date : 2015-03-27
Age : 15
Location : India
View user profile

Re: Networking in Unity

on Fri May 15, 2015 6:39 am
u can use this

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

aswell
Back to top
Permissions in this forum:
You cannot reply to topics in this forum