Skip to content

Instantly share code, notes, and snippets.

@marcteys
Last active August 29, 2015 14:20
Show Gist options
  • Save marcteys/1ff0951c77ebc9c5a92b to your computer and use it in GitHub Desktop.
Save marcteys/1ff0951c77ebc9c5a92b to your computer and use it in GitHub Desktop.
Easy network unity
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
public bool setupServer = false;
void Start ()
{
#if UNITY_EDITOR
// setupServer = true;
#endif
if (setupServer) CreateServer();
else ConnectToServer();
}
void CreateServer ()
{
Network.InitializeServer(3, 25000, false);
}
void ConnectToServer()
{
if (PlayerPrefs.HasKey("ipAdress"))
{
Network.Connect(PlayerPrefs.GetString("ipAdress"), 25000);
}
else
{
Network.Connect("127.0.0.1", 25000);
}
}
void OnFailedToConnect(NetworkConnectionError error)
{
Debug.Log("Could not connect to server: " + error);
}
void OnConnectedToServer()
{
Debug.Log("Connected to server");
}
void OnServerInitialized()
{
Debug.Log("Server initialized");
}
void OnDisconnectedFromServer()
{
Debug.Log("Disconnected from server");
}
void OnPlayerConnected()
{
Debug.Log("Player connected");
}
void OnPlayerDisconnected()
{
Debug.Log("Player disconnected");
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class SaveIp : MonoBehaviour {
InputField inputF;
void Awake ()
{
inputF = this.GetComponent<InputField>();
if (PlayerPrefs.HasKey("ipAdress")) inputF.text = PlayerPrefs.GetString("ipAdress");
else inputF.text = "192.168.1.32";
}
public void SaveCurrentIp ()
{
PlayerPrefs.SetString("ipAdress", inputF.text);
PlayerPrefs.Save();
Debug.Log("save current IP");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment