topical media & game development

talk show tell print

lib-unity-tutorial-network-experiment-Assets-tutorial2-Tutorial-2B-Spawnscript.js / js



  /* 
  *  This file is part of the Unity networking tutorial by M2H (http://www.M2H.nl)
  *  The original author of this code Mike Hergaarden, even though some small parts 
  *  are copied from the Unity tutorials/manuals.
  *  Feel free to use this code for your own projects, drop me a line if you made something exciting! 
  */
  #pragma strict
  
  public var playerPrefab : Transform;
  
  function OnServerInitialized(){
          Spawnplayer();
  }
  
  function OnConnectedToServer(){
          Spawnplayer();
  }
  
  function Spawnplayer(){
          
          var myNewTrans : Transform = Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
  
  }
  
  function OnPlayerDisconnected(player: NetworkPlayer) {
          Debug.Log("Clean up after player " + player);
          Network.RemoveRPCs(player);
          Network.DestroyPlayerObjects(player);
  }
  
  function OnDisconnectedFromServer(info : NetworkDisconnection) {
          Debug.Log("Clean up a bit after server quit");
          Network.RemoveRPCs(Network.player);
          Network.DestroyPlayerObjects(Network.player);
          
          /* 
  	* Note that we only remove our own objects, but we cannot remove the other players 
  	* objects since we don't know what they are; we didn't keep track of them. 
  	* In a game you would usually reload the level or load the main menu level anyway ;).
  	* 
  	* In fact, we could use "Application.LoadLevel(Application.loadedLevel);" here instead to reset the scene.
  	*/
          Application.LoadLevel(Application.loadedLevel);
  }


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.