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

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » FREE Project Downloads & Recources » Scripts » Networking in Unity

Networking in Unity

View previous topic View next topic Go down  Message [Page 1 of 1]

1 Networking in Unity on Fri Aug 22, 2014 1:20 am

Ninjaboy follower


Member [Lv. 3]
Member [Lv. 3]
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();
         }
      }
   }
}


View user profile

2 Re: Networking in Unity on Fri Aug 22, 2014 3:08 am

developer23


Moderator
Moderator
Nice

View user profile http://masterdevelopers.altervista.org/

3 Re: Networking in Unity on Thu Sep 11, 2014 11:30 pm

BryceCain25


SUPER MODERATOR
SUPER MODERATOR
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!

View user profile

4 Re: Networking in Unity on Sat May 09, 2015 6:39 am

macktoxic


Member [Lv. 2]
Member [Lv. 2]
Is this can work for Multiplayer games using Host IP?

View user profile

5 Re: Networking in Unity on Fri May 15, 2015 6:39 am

mynametiger


{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
u can use this

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

aswell

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

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