Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class NetworkManager : MonoBehaviour {
- public GameObject playerPrefab;
- public Transform[] spawnPoints;
- float buttonWidth;
- float buttonHeight;
- float buttonY;
- float buttonX;
- void Start () {
- buttonX = Screen.width * 0.01f;
- buttonY = Screen.width * 0.01f;
- buttonWidth = Screen.width * 0.5f;
- buttonHeight = Screen.width * 0.05f;
- }
- void StartServer () {
- Network.InitializeServer(10, 20505, !Network.HavePublicAddress());
- }
- void Connect () {
- Network.Connect("127.0.0.1", 20505);
- }
- void OnServerInitialized () {
- SpawnPlayer ();
- }
- void OnConnectedToServer () {
- SpawnPlayer ();
- }
- void SpawnPlayer () {
- int randomSpawn = Random.Range(0, spawnPoints.Length);
- Network.Instantiate (playerPrefab, spawnPoints[randomSpawn].position, spawnPoints[randomSpawn].rotation, 0);
- gameObject.SetActive(false);
- }
- void OnGUI () {
- if (GUI.Button(new Rect(buttonX, buttonY, buttonWidth, buttonHeight), "Start Server")){
- StartServer ();
- }
- if (GUI.Button(new Rect(buttonX, buttonY * 1.5f + buttonHeight, buttonWidth, buttonHeight), "Connect to server")){
- Connect ();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement