Skip to content

Instantly share code, notes, and snippets.

@Jimbologo
Created April 10, 2019 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jimbologo/af6c24c5dca3cb8ae26499eb94161735 to your computer and use it in GitHub Desktop.
Save Jimbologo/af6c24c5dca3cb8ae26499eb94161735 to your computer and use it in GitHub Desktop.
// James Gratrix, 01/03/2019
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Realtime;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using L6.UI.Manager;
namespace Photon.Pun {
//Controls Initial Connection to Photon & Room listings
public class PhotonServerSetup : MonoBehaviourPunCallbacks {
public PreGameLobby PreGameLobbyScript;
[SerializeField]
private bool debugMode = false;
[Header("UI Components")]
[SerializeField]
private InputField createRoomInputField;
[SerializeField]
private GameObject roomListScrollContainer;
[SerializeField]
private GameObject roomListButtonPrefab;
[SerializeField]
private GameObject roomListingPanel;
[SerializeField]
private GameObject photonDisconnectedPanel;
[Space(5)]
[Header("Photon Settings")]
[SerializeField]
const string versionName = "1.0";
[Space(5)]
[Header("Scene Settings")]
[SerializeField]
private GameObject miniGameManager;
private GameSelectorManager gameSelectorManager;
private void Start() {
gameSelectorManager = FindObjectOfType<GameSelectorManager>();
ConnectToPhotonMaster();
}
//Connects to photon master
private void ConnectToPhotonMaster()
{
PhotonNetwork.GameVersion = versionName;
PhotonNetwork.ConnectUsingSettings();
if (debugMode) {
Debug.Log("Connecting to Photon...");
}
}
//Called once connected to photon master
public override void OnConnectedToMaster()
{
PhotonNetwork.JoinLobby();
if (debugMode) {
Debug.Log("We are connected to master");
}
base.OnConnectedToMaster();
}
//called when joined to master & lobby
public override void OnJoinedLobby() {
roomListingPanel.SetActive(true);
if (debugMode) {
Debug.Log("We are connected to master");
}
base.OnJoinedLobby();
}
//called when disconnected from photon
public override void OnDisconnected(DisconnectCause cause) {
roomListingPanel.SetActive(false);
photonDisconnectedPanel.SetActive(true);
if (debugMode) {
Debug.Log("Disconnected from Photon Server Due to: " + cause.ToString());
}
base.OnDisconnected(cause);
}
//Called when we create a new room button
public void OnClickCreateRoom() {
if (createRoomInputField.text.Length >= 1) {
if (debugMode) {
Debug.Log("Created Room with name: " + createRoomInputField.text);
}
PhotonNetwork.CreateRoom(createRoomInputField.text, new Realtime.RoomOptions() { MaxPlayers = 4 }, null);
} else {
if (debugMode) {
Debug.Log("Room name too short! ");
}
}
}
//Called when we click on a room listing
public void OnClickJoinRoom(string a_sRoomName) {
PhotonNetwork.JoinRoom(a_sRoomName);
}
//Called when connected to a room
public override void OnJoinedRoom() {
gameSelectorManager.ShowNetReadyUPPanel();
if (debugMode) {
Debug.Log("Connected to the room!");
}
InvokeRepeating("UpdatePlayerList", 0.1f, 2.0f);
UpdatePlayerList();
}
public override void OnPlayerEnteredRoom(Player newPlayer) {
if (debugMode) {
Debug.Log("A Player Connected to the room!");
}
UpdatePlayerList();
base.OnPlayerEnteredRoom(newPlayer);
}
public override void OnPlayerLeftRoom(Player newPlayer) {
if (debugMode) {
Debug.Log("a Player Left the room!");
}
base.OnPlayerEnteredRoom(newPlayer);
}
//called when we failed to create a room
public override void OnCreateRoomFailed(short returnCode, string message) {
if (debugMode) {
Debug.Log("Failed to create room! Error: " + message);
}
base.OnCreateRoomFailed(returnCode, message);
}
//called when we failed to join a room
public override void OnJoinRoomFailed(short returnCode, string message) {
if (debugMode) {
Debug.Log("Failed to join room! Error: " + message);
}
base.OnJoinRoomFailed(returnCode, message);
}
//Creates the room list
public void LoadRoomList() {
if (!roomListScrollContainer) {
if (debugMode) {
Debug.Log("No room list container found, exiting ");
}
return;
}
}
//Loads/refreshes the room listings
public override void OnRoomListUpdate(List<RoomInfo> roomList) {
if (debugMode) {
Debug.Log("Loading room list... ");
}
int roomCount = roomList.Count;
for (int i = 0; i < roomCount; ++i) {
GameObject newRoomButton = Instantiate(roomListButtonPrefab);
newRoomButton.transform.SetParent(roomListScrollContainer.transform);
RoomListButton newRoom = newRoomButton.GetComponent<RoomListButton>();
if (!newRoom) {
if (debugMode) {
Debug.Log("Room not Found! Exiting");
}
return;
}
newRoom.photonServers = this;
newRoom.SetRoomName(roomList[i].Name);
newRoom.SetRoomPlayerCount(roomList[i].PlayerCount);
}
base.OnRoomListUpdate(roomList);
}
private void UpdatePlayerList()
{
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Photon.Pun {
public class RoomListButton : MonoBehaviour {
public string roomName = "";
public PhotonServerSetup photonServers;
[SerializeField]
private Text roomNameTextUI;
[SerializeField]
private Text roomPlayerCountTextUI;
//Sets the local room name text
public void SetRoomName(string a_roomName) {
roomName = a_roomName;
roomNameTextUI.text = "Room: " + roomName;
}
//sets the local room player count text
public void SetRoomPlayerCount(int a_roomPlayerCount) {
roomPlayerCountTextUI.text = "Players: " + a_roomPlayerCount;
}
//called when we click button, joins room with room name
public void JoinRoom() {
photonServers.OnClickJoinRoom(roomName);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SettingsController : MonoBehaviour
{
[Space(5)]
[Header("General Panels")]
[SerializeField]
private GameObject graphicSettingsPanel;
[SerializeField]
private GameObject audioSettingsPanel;
[SerializeField]
private GameObject creditsPanel;
[Space(5)]
[Header("Resolution Settings")]
[SerializeField]
private Dropdown resolutionDropdown;
private List<Dropdown.OptionData> resolutionsList= new List<Dropdown.OptionData>();
private bool m_fullscreenToggle = false;
private void Start() {
Screen.fullScreen = m_fullscreenToggle;
UpdateResolutions();
resolutionDropdown.onValueChanged.AddListener(delegate {
DropdownValueChanged(resolutionDropdown);
}
);
}
///
/// GENERAL SETTINGS
///
//Called when the user clicks on the graphics button
public void onClickShowGraphicSettings() {
graphicSettingsPanel.SetActive(true);
audioSettingsPanel.SetActive(false);
creditsPanel.SetActive(false);
}
//Called when the user clicks on the audio button
public void onClickShowAudioSettings() {
graphicSettingsPanel.SetActive(false);
audioSettingsPanel.SetActive(true);
creditsPanel.SetActive(false);
}
//Called when the user clicks on the credits button
public void onClickShowCredits() {
graphicSettingsPanel.SetActive(false);
audioSettingsPanel.SetActive(false);
creditsPanel.SetActive(true);
}
///
/// GRAPHIC SETTINGS
///
//Called when the user clicks on one of levels of quality for quality
public void onClickGeneralQuality(int a_level)
{
QualitySettings.SetQualityLevel(a_level);
}
//Called when the user clicks on one of levels of quality for vsync
public void onClickVSync(int a_level) {
QualitySettings.vSyncCount = a_level;
}
//Called when the user clicks on one of levels of quality for anti-aliasing
public void onClickAntiAliasing(int a_level) {
QualitySettings.antiAliasing = a_level;
}
//Called when the user clicks on one of levels of quality for lod
public void onClickLODLevel(int a_level) {
QualitySettings.lodBias = (float)a_level;
}
//Called when the user clicks on one of levels of quality for shadows
public void onClickShadows(int a_level) {
QualitySettings.shadows = (ShadowQuality)a_level;
}
//Called when the user clicks on one of levels of quality for fullscreen
public void onClickToggleFullScreen() {
m_fullscreenToggle = !m_fullscreenToggle;
}
//called when we wish to update the list of posible resolutions
private void UpdateResolutions() {
resolutionsList.Clear();
Resolution[] resolutions = Screen.resolutions;
for (int i = 0; i < resolutions.Length; ++i) {
Dropdown.OptionData newData = new Dropdown.OptionData();
newData.text = resolutions[i].width + "x" + resolutions[i].height;
resolutionsList.Add(newData);
}
resolutionDropdown.ClearOptions();
resolutionDropdown.AddOptions(resolutionsList);
}
//Called as a delegate when we select a new resolution from the dropdown
public void DropdownValueChanged(Dropdown a_thisDropDown) {
string resolution = a_thisDropDown.options[a_thisDropDown.value].text;
string[] newRes = resolution.Split('x');
Screen.SetResolution(int.Parse(newRes[0]), int.Parse(newRes[1]), m_fullscreenToggle);
}
///
/// AUDIO SETTINGS
///
//Called when the user uses the audio volume slider
public void onValChangedVolume(float a_value) {
AudioListener.volume = a_value;
}
///
/// CREDITS
///
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment