Created
April 13, 2017 08:01
-
-
Save will-hart/da11d309e214c4b8abf2aa9fc79e388e to your computer and use it in GitHub Desktop.
Single Player Fallback (Forge Networking)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Goliath.Networking | |
{ | |
#region Dependencies | |
using BeardedManStudios.Forge.Networking; | |
using BeardedManStudios.Forge.Networking.Unity; | |
// other usings | |
using UnityEngine; | |
#endregion | |
public class SinglePlayerFallback : MonoBehaviour | |
{ | |
[SerializeField] | |
public GameObject _networkManager = null; | |
private void Start() | |
{ | |
if (NetworkManager.Instance == null) | |
{ | |
Debug.LogWarning("No network instance detected - reverting to single player"); | |
UDPServer server = new UDPServer(0); | |
server.Connect(); | |
server.StopAcceptingConnections(); | |
var mgr = Instantiate(_networkManager).GetComponent<NetworkManager>(); | |
mgr.Initialize(server); | |
NetworkObject.Flush(server); | |
// spawn required systems and delete existing ones which aren't required | |
NetworkManager.Instance.InstantiatePlayerSlots(); | |
// do other setup logic that is normally done by the lobby scene | |
} | |
// do stuff here that I want done in any scene, i.e. instantiate my "GameManager", "Player" or whatever | |
// tidy up | |
Destroy(gameObject); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment