- Home /
Add menus to FPS Game
Hello,
I recently tried to create a FPS Multiplayer game and I got everything working. However, I don't know how to add nice menus.
The only menus I have right now is one to choose singleplayer or multiplayer and one where you choose your team. But now I want to add a main menu, settings menu, a menu to show rooms on server(multiplayer) and the ability to join a certain room or join a random room, a menu to create rooms, a menu to choose map with and a pause menu. In the settings menu I want the ability to change volume, resolution, toggle fullscreen, and mouse sensitivity.
I'm new to both unity and c# and this is my first game so I don't know how I should create that.
Also, I don't know which scripts you guys need to help me but this script is the one with the two menus I currently have.
using UnityEngine;
using System.Collections.Generic;
public class NetworkManager : MonoBehaviour {
public GameObject standbyCamera;
SpawnSpot[] spawnSpots;
public bool offlineMode = false;
bool connecting = false;
List<string> chatMessages;
int maxChatMessages = 5;
public float respawnTimer = 0;
bool hasPickedTeam = false;
int teamID = 0;
// Use this for initialization
void Start () {
spawnSpots = GameObject.FindObjectsOfType<SpawnSpot>();
PhotonNetwork.player.name = PlayerPrefs.GetString("Username", "Awesome Dude");
chatMessages = new List<string>();
}
void OnDestroy() {
PlayerPrefs.SetString("Username", PhotonNetwork.player.name);
}
public void AddChatMessage(string m) {
GetComponent<PhotonView>().RPC("AddChatMessage_RPC", PhotonTargets.AllBuffered, m);
}
[RPC]
void AddChatMessage_RPC(string m) {
while (chatMessages.Count >= maxChatMessages) {
chatMessages.RemoveAt(0);
}
chatMessages.Add(m);
}
void Connect() {
PhotonNetwork.ConnectUsingSettings("v004");
}
void OnGUI() {
GUILayout.Label ( PhotonNetwork.connectionStateDetailed.ToString() );
if (PhotonNetwork.connected == false && connecting == false && LaunchGame == true) {
// We have not yet connected, so ask the player for online vs offline mode
GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();
GUILayout.BeginHorizontal();
GUILayout.Label("Username: ");
PhotonNetwork.player.name = GUILayout.TextField(PhotonNetwork.player.name);
GUILayout.EndHorizontal();
if (GUILayout.Button("Singleplayer")) {
connecting = true;
PhotonNetwork.offlineMode = true;
OnJoinedLobby();
}
if (GUILayout.Button("Multiplayer")) {
connecting = true;
Connect();
}
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
if (PhotonNetwork.connected == true && connecting == false) {
if (hasPickedTeam) {
// We are fully connected, make sure to display the chat box
GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();
foreach (string msg in chatMessages) {
GUILayout.Label(msg);
}
GUILayout.EndVertical();
GUILayout.EndArea();
} else {
// Player has not yet selected a team
GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Green Team")) {
SpawnMyPlayer(2);
}
if (GUILayout.Button("Red Team")) {
SpawnMyPlayer(1);
}
if (GUILayout.Button("Random")) {
SpawnMyPlayer(Random.Range(1, 3)); // 1 or 2
}
if (GUILayout.Button("Renegade")) {
SpawnMyPlayer(0);
}
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
}
}
void OnJoinedLobby() {
Debug.Log ("OnJoinedLobby");
PhotonNetwork.JoinRandomRoom();
}
void OnPhotonRandomJoinFailed() {
Debug.Log ("We couldn't enter any room");
PhotonNetwork.CreateRoom(null);
}
void OnJoinedRoom() {
Debug.Log ("Joined");
connecting = false;
//SpawnMyPlayer();
}
void SpawnMyPlayer(int teamID) {
this.teamID = teamID;
hasPickedTeam = true;
Screen.lockCursor = true;
AddChatMessage("Spawning player: " + PhotonNetwork.player.name);
if (spawnSpots == null) {
Debug.LogError("No spawnspots?!?!?!?!?");
return;
}
SpawnSpot mySpawnSpot = spawnSpots [Random.Range(0, spawnSpots.Length)];
GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("PlayerController", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
standbyCamera.SetActive(false);
//((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent("PlayerMovement")).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent("PlayerShooting")).enabled = true;
myPlayerGO.GetComponent<PhotonView>().RPC("SetTeamID", PhotonTargets.AllBuffered, teamID);
myPlayerGO.transform.FindChild("Main Camera").gameObject.SetActive(true);
}
void Update() {
if (respawnTimer > 0) {
respawnTimer -= Time.deltaTime;
if (respawnTimer <= 0) {
// Respawn player
SpawnMyPlayer(teamID);
}
}
}
}
I have this menu code as well, but it isn't used by the game yet because I don't know how to implement it.
using UnityEngine;
using System.Collections;
public class Menu : MonoBehaviour
{
public GUISkin guiSkin;
public Texture2D background, LOGO;
public bool DragWindow = false;
public string levelToLoadWhenClickedPlay = "";
public string[] AboutTextLines = new string[0];
private string clicked = "", MessageDisplayOnAbout = "About \n ";
private Rect WindowRect = new Rect((Screen.width / 2) - 100, Screen.height / 2, 200, 200);
private float volume = 1.0f;
private void Start()
{
for (int x = 0; x < AboutTextLines.Length;x++ )
{
MessageDisplayOnAbout += AboutTextLines[x] + " \n ";
}
MessageDisplayOnAbout += "Press Esc To Go Back";
}
private void OnGUI()
{
if (background != null)
GUI.DrawTexture(new Rect(0,0,Screen.width , Screen.height),background);
if (LOGO != null && clicked != "about")
GUI.DrawTexture(new Rect((Screen.width / 2) - 100, 30, 200, 200), LOGO);
GUI.skin = guiSkin;
if (clicked == "")
{
WindowRect = GUI.Window(0, WindowRect, menuFunc, "Main Menu");
}
else if (clicked == "options")
{
WindowRect = GUI.Window(1, WindowRect, optionsFunc, "Options");
}
else if (clicked == "about")
{
GUI.Box(new Rect (0,0,Screen.width,Screen.height), MessageDisplayOnAbout);
}else if (clicked == "resolution")
{
GUILayout.BeginVertical();
for (int x = 0; x < Screen.resolutions.Length;x++ )
{
if (GUILayout.Button(Screen.resolutions[x].width + "X" + Screen.resolutions[x].height))
{
Screen.SetResolution(Screen.resolutions[x].width,Screen.resolutions[x].height,true);
}
}
GUILayout.EndVertical();
GUILayout.BeginHorizontal();
if (GUILayout.Button("Back"))
{
clicked = "options";
}
GUILayout.EndHorizontal();
}
}
private void optionsFunc(int id)
{
if (GUILayout.Button("Resolution"))
{
clicked = "resolution";
}
GUILayout.Box("Volume");
volume = GUILayout.HorizontalSlider(volume ,0.0f,1.0f);
AudioListener.volume = volume;
if (GUILayout.Button("Back"))
{
clicked = "";
}
if (DragWindow)
GUI.DragWindow(new Rect (0,0,Screen.width,Screen.height));
}
private void menuFunc(int id)
{
//buttons
if (GUILayout.Button("Play Game"))
{
//play game is clicked
Application.LoadLevel(levelToLoadWhenClickedPlay);
}
if (GUILayout.Button("Options"))
{
clicked = "options";
}
if (GUILayout.Button("About"))
{
clicked = "about";
}
if (GUILayout.Button("Quit Game"))
{
Application.Quit();
}
if (DragWindow)
GUI.DragWindow(new Rect(0, 0, Screen.width, Screen.height));
}
private void Update()
{
if (clicked == "about" && Input.GetKey (KeyCode.Escape))
clicked = "";
}
}
This game is based on this tutorial series: https://www.youtube.com/playlist?list=PLbghT7MmckI7BDIGqNl_TgizCpJiXy0n9
If you need something else to help me, just let me know.
Thanks in advance,
TS1997
Wouldn't the same concept of you're menus you've already done work?
You're best bet is to either;
Youtube and google (they are your best friends lol)
learn how to use 3d objects as a menu, much easier if you ask me, all you have to worry about then is $$anonymous$$ouse Click Events, if you click this button then do this.
Hope that helps, I know I wasn't much on you're topic persae but I've learned quickly that building a Network game for your first game is a daunting task that isn't to be taken lightly.
I know Youtube has a 3d $$anonymous$$ain $$anonymous$$enu, however they aren't using Clickable 3d objects, at least the ones I've seen. THey are all the EWWW GUI's.
But yeah, do some more research and I'm sure you can find something on what you are looking for!
Thanks for the answer @N1warhead. I was thinking about using the concept I have and I've done some research and I kind of know how to do buttons etc but I don't know how to implement them in the code I have. Could you possibly show me by editing the code? To show me for example, in this if block you have this menu and down here you have this one? Thanks in advance!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Runs fine in editor, crash on switch platform to iOS. 0 Answers
How to Move an object after it was reproduced by spawn 0 Answers
Mathf.pingpong 4 Answers