Advertisement
Guest User

Untitled

a guest
Mar 5th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class NetworkManager : MonoBehaviour {
  5.  
  6. public GameObject playerPrefab;
  7. public Transform[] spawnPoints;
  8. float buttonWidth;
  9. float buttonHeight;
  10. float buttonY;
  11. float buttonX;
  12.  
  13. void Start () {
  14. buttonX = Screen.width * 0.01f;
  15. buttonY = Screen.width * 0.01f;
  16. buttonWidth = Screen.width * 0.5f;
  17. buttonHeight = Screen.width * 0.05f;
  18. }
  19.  
  20. void StartServer () {
  21. Network.InitializeServer(10, 20505, !Network.HavePublicAddress());
  22. }
  23.  
  24. void Connect () {
  25. Network.Connect("127.0.0.1", 20505);
  26. }
  27.  
  28. void OnServerInitialized () {
  29. SpawnPlayer ();
  30. }
  31.  
  32. void OnConnectedToServer () {
  33. SpawnPlayer ();
  34. }
  35.  
  36. void SpawnPlayer () {
  37. int randomSpawn = Random.Range(0, spawnPoints.Length);
  38. Network.Instantiate (playerPrefab, spawnPoints[randomSpawn].position, spawnPoints[randomSpawn].rotation, 0);
  39. gameObject.SetActive(false);
  40. }
  41.  
  42. void OnGUI () {
  43. if (GUI.Button(new Rect(buttonX, buttonY, buttonWidth, buttonHeight), "Start Server")){
  44. StartServer ();
  45. }
  46. if (GUI.Button(new Rect(buttonX, buttonY * 1.5f + buttonHeight, buttonWidth, buttonHeight), "Connect to server")){
  47. Connect ();
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement