/// <summary> /// Returns true if this is the singleton, false if we had to destroy it. /// </summary> private bool EnforceSingleton() { if (single && single != this) { Destroy(this); return(false); } single = this; return(true); }
private void SpawnLocalPlayer() { // we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate if (MasterNetAdapter.NetLib == NetworkLibrary.PUN) { Transform tr = NSTSamplePlayerSpawn.GetRandomSpawnPoint(); Vector3 pos = (tr) ? tr.position : Vector3.zero; Quaternion rot = (tr) ? tr.rotation : Quaternion.identity; MasterNetAdapter.Spawn(playerPrefab, pos, rot, null); } }
public void Update() { if (Input.GetKeyDown(spawnPlayerKey)) { SpawnLocalPlayer(); } else if (Input.GetKeyDown(unspawnPlayerKey)) { MasterNetAdapter.UnSpawn(localPlayer); } }
public override void OnInspectorGUI() { serializedObject.Update(); MasterNetAdapter _target = (MasterNetAdapter)target; NetAdapterTools.EnsureSceneNetLibDependencies(); base.OnInspectorGUI(); EditorGUILayout.HelpBox("This is the UNET adapter. To work with Photon PUN, switch the Network Library.", MessageType.None); NetLibrarySettings.Single.DrawGui(true, false); }
/// <summary> /// Start the connection process. /// - If already connected, we attempt joining a random room /// - if not yet connected, Connect this application instance to Photon Cloud Network /// </summary> public void Connect() { // we check if we are connected or not, we join if we are , else we initiate the connection to the server. if (MasterNetAdapter.PUN_Connected) { MasterNetAdapter.PUN_JoinRandomRoom(); } else { MasterNetAdapter.PUN_ConnectUsingSettings(_gameVersion); } }
/// <summary> /// Deep find and add of adapters to all NSTs objects in assets. Deferred actions that need to happen after a compile following library change. /// </summary> public static void TryToAddDependenciesEverywhere() { if (DependenciesHaveBeenAddedEverywhere) { return; } DebugX.Log("Adding NST Entities in all Assets"); MasterNetAdapter.AddNstEntityComponentsEverywhere(); // Now that prefabs in assets have been altered, make sure any scene objects revert to those prefabs RevertPrefabsInSceneWithComponentType <NetworkSyncTransform>(); DependenciesHaveBeenAddedEverywhere = true; }
// This should only be run very rarely. Forced to false after a library change. //public static bool dependenciesNeedToBeCheckedEverywhere; /// <summary> /// Deep find and add of adapters to all NSTs objects in assets. Deferred actions that need to happen after a compile following library change. /// </summary> public static void TryToAddDependenciesEverywhere() { if (!NetLibrarySettings.Single.dependenciesNeedToBeCheckedEverywhere) { return; } Debug.LogWarning("Added NST Entities in all Assets in entire project. Sorry if this took a while, but changing network libraries is a very brute force operation."); MasterNetAdapter.AddNstEntityComponentsEverywhere(); // Now that prefabs in assets have been altered, make sure any scene objects revert to those prefabs RevertPrefabsInSceneWithComponentType <NetworkSyncTransform>(); NetLibrarySettings.Single.dependenciesNeedToBeCheckedEverywhere = false; AssetDatabase.Refresh(); EditorUtility.SetDirty(NetLibrarySettings.single); AssetDatabase.SaveAssets(); }
private void SpawnLocalPlayer() { // we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate if (MasterNetAdapter.NetLib == NetworkLibrary.PUN || NSTNetAdapter.NetLibrary == NetworkLibrary.PUN2) { if (playerPrefab) { Transform tr = NSTSamplePlayerSpawn.GetRandomSpawnPoint(); Vector3 pos = (tr) ? tr.position : Vector3.zero; Quaternion rot = (tr) ? tr.rotation : Quaternion.identity; localPlayer = MasterNetAdapter.Spawn(playerPrefab, pos, rot, null); } else { Debug.LogError("No PlayerPrefab defined in " + this.GetType().Name); } } }
// Create the master bitstream and poll for updates private static void PollForUpdates(int updateCounter, bool isOfftick) { UdpBitStream bitstream = new UdpBitStream(NSTMaster.bitstreamByteArray); UdpBitStream outstream = new UdpBitStream(); bitstream.WriteInt(updateCounter, 6); bool updatesFound = PollAllForNSTUpdates(ref bitstream, isOfftick ? NSTTools.allNstsWithOfftick : NSTTools.allNsts, updateCounter, isOfftick); if (!isOfftick) { MasterRTT.Send(ref bitstream, updateCounter); } /// Additional Data after NST Updates added to stream here // Send every tick, or if anything offtick was found if (updatesFound || !isOfftick) { MasterNetAdapter.SendUpdate(ref bitstream, ref outstream); } }
//#if UNITY_EDITOR // private bool needsEditorModePostAwakeCheck; //#endif protected override void Awake() { base.Awake(); mna = GetComponent <MasterNetAdapter>(); if (mna == null) { mna = gameObject.AddComponent <MasterNetAdapter>(); } //#if UNITY_EDITOR // // Don't run awake if this is not runtime. // if (!Application.isPlaying) // { // EnsureNSTMasterConforms(); // needsEditorModePostAwakeCheck = true; // return; // } //#endif DontDestroyOnLoad(gameObject); }
public void OnConnect(ServerClient svrclnt) { MasterNetAdapter.PUN_JoinRandomRoom(); }
/// <summary> /// Finds all instances of the network adapters in loaded scenes and the assetdatabse. Used prior to a network library change in order to not /// create broken scripts where the old adapters were. /// </summary> public static void PurgeLibraryReferences() { PurgeTypeFromEverywhere <NSTNetAdapter>(); PurgeTypeFromEverywhere <MasterNetAdapter>(); MasterNetAdapter.PurgeLibSpecificComponents(); }
void OnDisable() { spawns.Remove(this); MasterNetAdapter.UNET_UnRegisterStartPosition(transform); }
void OnEnable() { spawns.Add(this); MasterNetAdapter.UNET_RegisterStartPosition(transform); }
protected virtual void OnDisable() { MasterNetAdapter.UnregisterCallbackInterfaces(this); }
/// <summary> /// We use interfaces to get messages from the network adapters, so we need to register this MB to make its interfaces /// known to the MasterNetAdapter. /// </summary> protected void OnEnable() { MasterNetAdapter.RegisterCallbackInterfaces(this); }
public void OnJoinRoomFailed() { DebugX.Log("Launcher:OnPhotonRandomJoinFailed() was called by PUN. No random room available, so we create one.\nCalling: PhotonNetwork.CreateRoom(null, new RoomOptions() {maxPlayers = 4}, null);"); MasterNetAdapter.PUN_CreateRoom(null, 8); }