I’ve never used Photon before, but if I also wanted to add a singleplayer mode would I just use
PhotonNetwork.OfflineMode = true; or would I use something else?
Yes That’s what OfflineMode is for. The point is to be able to use the same code for a networked game and a single player game.
Do you think this code will work? This is for the main menu. (The offline is called on the Singleplayer function and the Connect and going online is called on MultiplayerMenu.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using Photon.Pun;
using Photon.Realtime;
[System.Serializable]
public class ProfileData
{
public string username;
public int level;
public int xp;
public ProfileData()
{
this.username = "PLAYER";
this.level = 1;
this.xp = 0;
}
public ProfileData(string u, int l, int x)
{
this.username = u;
this.level = l;
this.xp = x;
}
object[] ConvertToObjectArr()
{
object[] ret = new object[3];
return ret;
}
}
[System.Serializable]
public class MapData
{
public string name;
public int scene;
}
public class MenuManager : MonoBehaviourPunCallbacks
{
public GameObject mainMenuHolder;
public GameObject multiplayerMenu;
public GameObject createRoomMenu;
public GameObject settingsMenuHolder;
public GameObject exitGameHolder;
public Text mapText;
public MapData[] maps;
private int currentMap = 0;
public Text modeText;
public InputField usernameField;
public InputField roomNameField;
public static ProfileData myProfile = new ProfileData();
public GameObject roomButton;
private List<RoomInfo> roomList;
public Slider maxPlayersSlider;
public Text maxPlayersValue;
public Text usernameText;
public void ApplyUsername()
{
if (string.IsNullOrEmpty(usernameField.text))
myProfile.username = "PLAYER" + Random.Range(100, 1000);
else
myProfile.username = usernameField.text;
usernameText.text = myProfile.username;
}
private void VerifyUsername()
{
if (string.IsNullOrEmpty(usernameField.text))
myProfile.username = "PLAYER" + Random.Range(100, 1000);
else
myProfile.username = usernameField.text;
usernameText.text = myProfile.username;
}
void Start()
{
MainMenu();
PauseMenu.paused = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
PhotonNetwork.AutomaticallySyncScene = true;
PhotonNetwork.OfflineMode = true;
myProfile = Data.LoadProfile();
usernameField.text = myProfile.username;
usernameText.text = myProfile.username;
}
public void Singleplayer()
{
PhotonNetwork.OfflineMode = true;
Debug.Log(PhotonNetwork.OfflineMode);
if (PhotonNetwork.OfflineMode)
Debug.Log("You're offline!");
else
Debug.Log("You are not offline! | " + PhotonNetwork.OfflineMode);
}
public override void OnConnectedToMaster()
{
Debug.Log("Connected!");
PhotonNetwork.JoinLobby();
base.OnConnectedToMaster();
}
public override void OnJoinedRoom()
{
StartGame();
base.OnJoinedRoom();
}
public override void OnJoinRandomFailed(short returnCode, string message)
{
Create();
base.OnJoinRandomFailed(returnCode, message);
}
public void Connect()
{
Debug.Log("Connecting to server...");
PhotonNetwork.GameVersion = "0.1.0";
PhotonNetwork.ConnectUsingSettings();
}
public void Create()
{
RoomOptions options = new RoomOptions();
options.MaxPlayers = (byte)maxPlayersSlider.value;
options.CustomRoomPropertiesForLobby = new string[] { "map", "mode" };
ExitGames.Client.Photon.Hashtable properties = new ExitGames.Client.Photon.Hashtable();
properties.Add("map", currentMap);
properties.Add("mode", (int)GameSettings.gameMode);
options.CustomRoomProperties = properties;
PhotonNetwork.CreateRoom(roomNameField.text, options);
}
public void ChangeMap()
{
currentMap++;
if (currentMap >= maps.Length)
currentMap = 0;
mapText.text = "MAP: " + maps[currentMap].name.ToUpper();
}
public void ChangeMode()
{
int newMode = (int)GameSettings.gameMode + 1;
if (newMode >= System.Enum.GetValues(typeof(GameMode)).Length)
newMode = 0;
GameSettings.gameMode = (GameMode)newMode;
modeText.text = "MODE: " + System.Enum.GetName(typeof(GameMode), newMode);
}
public void ChangeMaxPlayerSlider(float value)
{
maxPlayersValue.text = Mathf.RoundToInt(value).ToString();
}
private void ClearRoomList()
{
Transform content = multiplayerMenu.transform.Find("Scroll View/Viewport/Content");
foreach (Transform a in content)
Destroy(a.gameObject);
}
public override void OnRoomListUpdate(List<RoomInfo> list)
{
roomList = list;
ClearRoomList();
//Debug.Log("Loaded Rooms @ " + Time.time);
Transform content = multiplayerMenu.transform.Find("Scroll View/Viewport/Content");
foreach(RoomInfo a in roomList)
{
GameObject newRoomButton = Instantiate(roomButton, content) as GameObject;
newRoomButton.transform.Find("Name").GetComponent<Text>().text = a.Name;
newRoomButton.transform.Find("Players").GetComponent<Text>().text = a.PlayerCount + " / " + a.MaxPlayers;
if (a.CustomProperties.ContainsKey("map"))
newRoomButton.transform.Find("Map/Name").GetComponent<Text>().text = maps[(int)a.CustomProperties["map"]].name;
else
newRoomButton.transform.Find("Map/Name").GetComponent<Text>().text = "-----";
newRoomButton.GetComponent<Button>().onClick.AddListener(delegate { JoinRoom(newRoomButton.transform); });
}
base.OnRoomListUpdate(roomList);
}
public void JoinRoom(Transform button)
{
string roomName = button.Find("Name").GetComponent<Text>().text;
VerifyUsername();
RoomInfo roomInfo = null;
Transform buttonParent = button.parent;
for (int i = 0; i < buttonParent.childCount; i++)
{
if (buttonParent.GetChild(i).Equals(button))
{
roomInfo = roomList[i];
break;
}
}
if (roomInfo != null)
{
LoadGameSettings(roomInfo);
PhotonNetwork.JoinRoom(roomName);
}
}
public void LoadGameSettings(RoomInfo roomInfo)
{
GameSettings.gameMode = (GameMode)roomInfo.CustomProperties["mode"];
Debug.Log(System.Enum.GetName(typeof(GameMode), GameSettings.gameMode));
}
public void Join()
{
PhotonNetwork.JoinRandomRoom();
}
public void StartGame()
{
VerifyUsername();
if (PhotonNetwork.CurrentRoom.PlayerCount == 1)
{
Data.SaveProfile(myProfile);
PhotonNetwork.LoadLevel(maps[currentMap].scene);
}
}
public void MainMenu()
{
mainMenuHolder.SetActive(true);
multiplayerMenu.SetActive(false);
createRoomMenu.SetActive(false);
settingsMenuHolder.SetActive(false);
exitGameHolder.SetActive(false);
PhotonNetwork.Disconnect();
PhotonNetwork.OfflineMode = true;
}
public void MultiplayerMenu()
{
mainMenuHolder.SetActive(false);
multiplayerMenu.SetActive(true);
createRoomMenu.SetActive(false);
settingsMenuHolder.SetActive(false);
exitGameHolder.SetActive(false);
PhotonNetwork.OfflineMode = false;
Connect();
Debug.Log(PhotonNetwork.OfflineMode);
}
public void CreateRoomMenu()
{
mainMenuHolder.SetActive(false);
multiplayerMenu.SetActive(false);
createRoomMenu.SetActive(true);
settingsMenuHolder.SetActive(false);
exitGameHolder.SetActive(false);
roomNameField.text = "";
currentMap = 0;
mapText.text = "MAP: " + maps[currentMap].name.ToUpper();
GameSettings.gameMode = (GameMode)0;
modeText.text = "MODE: " + System.Enum.GetName(typeof(GameMode), (GameMode)0);
maxPlayersSlider.value = maxPlayersSlider.maxValue;
maxPlayersValue.text = Mathf.RoundToInt(maxPlayersSlider.value).ToString();
}
public void SettingsMenu()
{
mainMenuHolder.SetActive(false);
multiplayerMenu.SetActive(false);
settingsMenuHolder.SetActive(true);
exitGameHolder.SetActive(false);
}
public void ExitMenu()
{
mainMenuHolder.SetActive(false);
multiplayerMenu.SetActive(false);
settingsMenuHolder.SetActive(false);
exitGameHolder.SetActive(true);
}
public void QuitGame()
{
Debug.Log("Quitting the game...");
Application.Quit();
}
public void OnHover()
{
FindObjectOfType<AudioManager>().Play("Hover");
}
public void OnClick()
{
FindObjectOfType<AudioManager>().Play("Click");
}
}
anyone able to help me out?