Advertisement
kasru

Networking

Mar 11th, 2013
7,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //****** Donations are greatly appreciated.  ******
  2. //****** You can donate directly to Jesse through paypal at  https://www.paypal.me/JEtzler   ******
  3.  
  4. var playerPrefab : GameObject;
  5. var spawnObject : Transform;
  6.  
  7. var gameName : String = "level1";
  8. private var refreshing : boolean = false;
  9. private var hostData : HostData[];
  10.  
  11. function OnGUI () {
  12.  
  13. if(!Network.isClient && !Network.isServer) {
  14.  
  15.  if (GUI.Button(Rect(Screen.width/2,Screen.height/2,100,20),"Start Server")) {
  16.         startServer();
  17. }
  18.  
  19.  if (GUI.Button(Rect(Screen.width/2,Screen.height/2 + 30,100,20),"Refresh Hosts")) {
  20.         Debug.Log("Refresh");
  21.         refreshHostList();
  22. }
  23.     if(hostData) {
  24.  
  25.     for(var i:int = 0; i<hostData.length; i++) {
  26.    
  27.     if(GUI.Button(Rect(Screen.width/2,Screen.height/2 + 60,100,20),hostData[i].gameName)) {
  28.         Network.Connect(hostData[i]);
  29.    
  30.    
  31.     }
  32.    
  33.    
  34. }
  35.  
  36. }
  37.  
  38.  
  39. }
  40.  
  41.  
  42. }
  43.  
  44.  
  45. function Update () {
  46.  
  47.     if(refreshing) {
  48.         if(MasterServer.PollHostList().Length > 0) {       
  49.         refreshing = false;
  50.         Debug.Log(MasterServer.PollHostList().Length);
  51.         hostData = MasterServer.PollHostList();
  52.        
  53.        
  54.         }
  55.    
  56.    
  57.     }
  58.  
  59.  
  60.  
  61. }
  62.  
  63.  
  64. function startServer () {
  65.  
  66.     Network.InitializeServer(32,25001, !Network.HavePublicAddress);
  67.     MasterServer.RegisterHost(gameName, "Tutorial Game", " this is a tutorial");
  68.    
  69. }
  70.    
  71.    
  72.    
  73. function OnServerInitialized () {
  74.  
  75.     Debug.Log("server initialized");
  76.     spawnPlayer();
  77.    
  78.    
  79. }
  80.  
  81. function OnConnectedToServer () {
  82.  
  83.     spawnPlayer();
  84.  
  85.  
  86. }
  87.  
  88. function spawnPlayer () {
  89.  
  90.     Network.Instantiate(playerPrefab, spawnObject.position, Quaternion.identity, 0);
  91.  
  92. }
  93.  
  94. function OnMasterServerEvent(mse:MasterServerEvent) {
  95.  
  96.     if(mse == MasterServerEvent.RegistrationSucceeded) {
  97.     Debug.Log("Registered Server");
  98.    
  99.     }
  100.    
  101. }
  102.    
  103.    
  104. function refreshHostList () {
  105.  
  106.     MasterServer.RequestHostList(gameName);
  107.     refreshing = true;
  108.  
  109.    
  110.    
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement