Advertisement
Guest User

Untitled

a guest
Apr 19th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ConnectScript : MonoBehaviour {
  5.     //variable declaraties
  6.     string remoteIP = "127.0.0.1";
  7.     int Port =  25000;
  8.     Rect windowRect = new Rect(20,20,300,600);
  9.     int playerSelection = 0;
  10.     string[] PlayerStrings = { "2 Players", "3 Players", "4 Players"};
  11.     bool serverstarted = false;
  12.     public static bool gamestarted = false;
  13.     public Transform PlayerPrefab;
  14.     Transform player;
  15.     int number = 1;
  16.    
  17.    
  18.    
  19.     void OnGUI()
  20.     {
  21.         if(gamestarted)
  22.             GUI.Label (new Rect(20,35,260,20),"Game Started");
  23.         else
  24.         {
  25.             if(Network.peerType == NetworkPeerType.Disconnected)
  26.             {
  27.                 windowRect = GUI.Window (0,windowRect,WindowFunction,"Connection");
  28.             }
  29.             else
  30.             {
  31.                 if(serverstarted)
  32.                     GUI.Label (new Rect(20,35,260,20), "Server started at:" + Network.player.ipAddress);
  33.             }
  34.         }
  35.     }
  36.         void WindowFunction(int WindowID)
  37.     {
  38.         GUI.Label(new Rect(20, 35, 260, 20), "Number of Players:");
  39.         playerSelection = GUI.SelectionGrid(new Rect(20, 60, 260, 60),
  40.                 playerSelection, PlayerStrings, 1) ;
  41.        
  42.         GUI.Label(new Rect(20, 130, 80, 20), "RemoteIP:");
  43.         remoteIP = GUI.TextField(new Rect(120, 150, 160, 20), remoteIP);
  44.         GUI.Label(new Rect(20, 180, 80, 20), "Port:");
  45.         Port = int.Parse(GUI.TextField(new Rect(120, 180, 160, 20), Port.ToString()));
  46.        
  47.         //START SERVER START SERVER START SERVER
  48.         if (GUI.Button(new Rect(100, 210, 140, 20), "Start Server"))
  49.         {
  50.             Network.InitializeServer(32, Port, false);
  51.             player =(Transform)Network.Instantiate (PlayerPrefab, Vector3.zero, Quaternion.identity,0);
  52.             SetPlayerNumber(1);
  53.         }
  54.         if (GUI.Button(new Rect(100, 230, 140, 20), "Connect to Server"))
  55.         {
  56.             Network.Connect(remoteIP, Port);
  57.         }
  58.     }
  59.  
  60.     void OnServerInitialized()
  61.     {
  62.         serverstarted = true;
  63.     }
  64.  
  65.     [RPC]
  66.     void startGame()
  67.     {
  68.         gamestarted = true;
  69.     }
  70.    
  71.     [RPC]
  72.     void SetPlayerNumber(int thisNumber)
  73.     {      
  74.         player.GetComponent<PlayerScript>().Setlocalplayer(thisNumber, true);
  75.         networkView.RPC("SetOtherPlayerNumber", RPCMode.OthersBuffered, thisNumber,
  76.             player.networkView.viewID);
  77.     }
  78.  
  79.     [RPC]
  80.     void SetOtherPlayerNumber(int thisNumber, NetworkViewID ID)
  81.     {
  82.         NetworkView view = NetworkView.Find(ID);
  83.         view.transform.GetComponent<PlayerScript>().Setlocalplayer(thisNumber,
  84.             false);
  85.     }
  86.  
  87.    
  88.    
  89.    
  90.    
  91.  
  92.     void OnPlayerConnected(NetworkPlayer networkplayer)
  93.     {
  94.         networkView.RPC("startGame", RPCMode.All);
  95.         number++;
  96.         networkView.RPC("SetPlayerNumber", networkplayer, number);
  97.        
  98.     }
  99.    
  100.     void OnConnectedToServer()
  101. {
  102.          player = (Transform)Network.Instantiate(PlayerPrefab, new Vector3(0, 3, 0),
  103.          Quaternion.identity, 0);
  104.  }
  105.  
  106.  
  107.     // Use this for initialization
  108.     void Start () {
  109.    
  110.     }
  111.    
  112.     // Update is called once per frame
  113.     void Update () {
  114.    
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement