Instantly share code, notes, and snippets.
Last active
February 23, 2019 09:17
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save ZeredaGames/b614c6b0eecb2666d5a7ebcfb3cb051c to your computer and use it in GitHub Desktop.
Made for someone new on Answers, all this is is a basic example of many little toolets and a pause menu was made while doing so. All was written with no helpers in a TXT doc. when run on unity to fix and arrors. Made to work with at least Unity 2018 3,3,f1 but should be usable by anything from Unity 5.5x-present as far as i am aware.
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
#region Licensing | |
/*Free to use all are basic functions, made in the course of 2 hour's, top's! Hope u all like it, should be pretty simple to use.*/ | |
#endregion | |
#region usings | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
#endregion | |
#region namespace | |
namespace ZeredaGamesEngine.Core.Controllers | |
{ | |
#region Sctipt 1 - ZeredaGamesBasicPauseMenu | |
public class BasicPauseMenu : MonoBehaviour | |
{ | |
#region class | |
// Basic functionality of a simple pause controller | |
// | |
#region Variables | |
[Header("ZGPauseMenuLevelManager:"), Space(1), Header("PUBLIC SETS:"), Space(1), Tooltip("Set a key code for enabling pause and disabling pause."), SerializeField] | |
private KeyCode PauseKey = KeyCode.Escape; | |
private static ZGPauseMenuLevelManager instance; | |
[Space(3), Header("SETTINGS:"), Space(1), Tooltip("A string using the TextArea attribute"), SerializeField] | |
private string SceneName = "~Select a 'SceneName'~"; | |
[Space, Tooltip("If True use int values *BuildIndexToLoad* to call scene otherwise use strings *SceneName* [Only in methods 'LoadNextLevel()' | 'LoadNextLevel(bool enable)', Otherwise this isn't called.]"), SerializeField, Space] | |
private bool useInt = true; | |
[Range(0, 10), Tooltip("Load a scene in Build Index by 'SceneName' *in array*."), SerializeField, Space(1)] | |
private int HomeScene = 0; | |
[Range(0, 10), Tooltip("Load a scene in Build Index by 'int id' *in array*."), SerializeField, Space(1)] | |
private int BuildIndexToLoad = 0; | |
private static bool paused = false; | |
#endregion | |
#region Unity Generic Methods | |
void Awake() | |
{ | |
//Was in OnEnable, but since you may not want that method or OnGUI, here it is in 'Awake' instead | |
AwakeSingleton(); | |
LoadData(); | |
} | |
void Update() | |
{ | |
if (Input.GetKeyDown(PauseKey)) | |
{ | |
PauseSwitch(); | |
} | |
} | |
#endregion | |
#region Custom Methods | |
public static void PauseSwitch() | |
{ | |
paused = !paused; | |
} | |
///<summary>Checks if paused and un-pause', checks if scene exists in build | |
///settings, loads Change "{} 's to view in code."scene if is, logs and exception | |
///with a new message explaining the error. </summary> | |
public void LoadScene(string sceneName) | |
{ | |
if (Equals(SceneManager.GetSceneByName(sceneName), sceneName)) | |
{ | |
if (paused) | |
OnUnpause(); | |
SaveData(); | |
SceneManager.LoadScene(sceneName); | |
} | |
else | |
Debug.LogException(new System.Exception("Scene does not exist in the build index")); | |
} | |
///<summary>Checks if paused and un-pause', checks if scene exists in build | |
///settings, loads Change "{} 's to view in code."scene if is, logs and exception | |
///with a new message explaining the error. </summary> | |
public void LoadScene(int buildIndexToLoad) | |
{ | |
if (Equals(SceneManager.GetSceneAt(BuildIndexToLoad).buildIndex, buildIndexToLoad)) | |
{ | |
if (paused) | |
OnUnpause(); | |
SaveData(); | |
SceneManager.LoadScene(buildIndexToLoad); | |
} | |
else | |
Debug.LogException(new System.Exception("Scene does not exist in the build index")); | |
} | |
/// <summary> | |
/// Loads the Next Scene | |
/// </summary> | |
public void LoadNextScene() | |
{ | |
if (paused) | |
OnUnpause(); | |
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); | |
} | |
/// <summary> | |
/// Loads the Next Scene by inspector "BuildIndexToLoad" Or "SceneName" | |
/// </summary> | |
/// <param name="UseInt"></param> | |
public void LoadNextScene(bool UseInt) | |
{ | |
if (UseInt) | |
{ | |
if (Equals(SceneManager.GetActiveScene().buildIndex, BuildIndexToLoad)) | |
{ | |
if (paused) | |
OnUnpause(); | |
SaveData(); | |
SceneManager.LoadScene(BuildIndexToLoad); | |
} | |
else | |
Debug.LogException(new System.Exception("Scene does not exist in the build index")); | |
} | |
else { | |
if (Equals(SceneManager.GetSceneByName(SceneName), SceneName)) | |
{ | |
if (paused) | |
OnUnpause(); | |
SaveData(); | |
SceneManager.LoadScene(BuildIndexToLoad); | |
} | |
else | |
Debug.LogException(new System.Exception("Scene does not exist in the build index")); | |
} | |
} | |
/// <summary> | |
/// Quit's the application Able To Call Ditrectly From Script | |
/// </summary> | |
public static void QuitApplication() | |
{ | |
Application.Quit(); | |
} | |
/// <summary> | |
/// Quit's the application Used as a button. | |
/// </summary> | |
public void OnQuitApplication() | |
{ | |
QuitApplication(); | |
} | |
/// <summary> | |
/// Last thing the script does. (Run's save sequance also) | |
/// </summary> | |
void OnDisable() | |
{ | |
SaveData(); | |
} | |
/// <summary> | |
/// Run's save sequance | |
/// </summary> | |
void SaveData() | |
{ | |
//SaveData.Save(Player.Id, Player.PlayerName); | |
} | |
/// <summary> | |
/// Run's load sequance | |
/// </summary> | |
void LoadData() | |
{ | |
//SaveData.Load(Player.Id, Player.PlayerName); | |
} | |
/// <summary> | |
/// Pauses the player | |
/// </summary> | |
void OnPause() | |
{ | |
if (paused) | |
{ | |
Time.timeScale = 0.0f; //Maybe some more data?? That's up to you. | |
AudioListener.volume = 0.0f; | |
Cursor.visible = false; | |
} | |
else | |
Debug.Log("Game Is Paused = " + paused); | |
} | |
/// <summary> | |
/// Un-pauses the player | |
/// </summary> | |
void OnUnpause() | |
{ | |
if (paused) | |
{ | |
Time.timeScale = 1.0f; //Maybe some more data?? That's up to you. | |
AudioListener.volume = 1.0f; | |
Cursor.visible = true; | |
} | |
else | |
Debug.Log("Game Is Paused = " + paused); | |
} | |
#endregion | |
#region Internal Singleton | |
public bool SetDontDestroyOnLoad = true; | |
public static ZGPauseMenuLevelManager Instance | |
{ | |
get | |
{ | |
if (instance == null) | |
{//in case not awake yet | |
instance = FindObjectOfType<ZGPauseMenuLevelManager>(); | |
} | |
return instance; | |
} | |
} | |
void AwakeSingleton() | |
{ | |
// if the singleton hasn't been initialized yet | |
if (SetDontDestroyOnLoad) | |
{ | |
if (instance != null && instance != this) | |
{ | |
Debug.LogError("Duplicate singleton " + this.gameObject + " created; destroying it now"); | |
Destroy(this.gameObject); | |
} | |
if (instance != this) | |
{ | |
instance = this; | |
DontDestroyOnLoad(this.gameObject); | |
} | |
} | |
else { | |
instance = this; | |
} | |
} | |
#endregion | |
#region OnGUI Extras | |
// Basic functionality of a simple pause Menu | |
// Extra GUI Menu HeHe this was actually pretty fun to make \/ \/ | |
#region GUIVariables | |
private static bool EnableOptions, DropDownMenu, DropDownMenu2; | |
[SerializeField, Header("GUI Content Info:"), Space, Tooltip("When enabled GUI Method calls. 'ToggleButtonGUIContent'")] | |
private bool UseGUIContent = false; | |
[SerializeField, Tooltip("When 'UseGUIContent' is enabled, changed GUI info on the toggle button in menu, reason is i wanted to add a tool tip to it. Works same idea for anywhere else, but would need a bunch of variables for each location so an array or preferrably a List of content is best.")] | |
private GUIContent ToggleButtonGUIContent; | |
[SerializeField, Space(1), Tooltip("Used to controll weather you would like to use a custom GUI style or not.")] | |
private bool UseGUIStyle = false; | |
[SerializeField, Space(1), Tooltip("Similar method applies here as the 'ToggleButtonGUIContent', when 'UseGUIStyle' is enabled, but this works accross many areas not just 1, i'm using it to control font,fontSize, If the text can scale and word wrap.")] | |
static GUIStyle YourGUIStyle = new GUIStyle(); | |
[SerializeField, Tooltip("How big is the font going to be. what looks good to you?.")] | |
private int FontSize = 17; | |
[SerializeField, Space(1), Tooltip("Select the font for all text in menu.")] | |
private Font SelectedFont; | |
[SerializeField, Space(1), Tooltip("Make the GUItext have word wrap.")] | |
private bool wordWrap = false; | |
[SerializeField, Space(1), Tooltip("Make the GUItext scale in height.")] | |
private bool expandHeight = false; | |
[SerializeField, Space(1), Tooltip("Make the GUItext scale in width.")] | |
private bool expandWidth = false; | |
[SerializeField, Space(1), Tooltip("Set a fixed height for all texts when using 'UseGUIStyle'.")] | |
private float fixedHeight; | |
[SerializeField, Space(1), Tooltip("Set a fixed width for all texts when using 'UseGUIStyle'.")] | |
private float fixedWidth; | |
#endregion | |
/// <summary> | |
/// First thing the script does. (Preferrably put LoadData(); here instead of "Start();") | |
/// </summary> | |
private void OnEnable() | |
{ | |
// Uncomment to use delete and from awake. | |
//AwakeSingleton(); | |
//LoadData(); | |
YourGUIStyle = new GUIStyle(); | |
YourGUIStyle.font = SelectedFont; | |
YourGUIStyle.fontSize = FontSize; | |
YourGUIStyle.stretchHeight = expandHeight; | |
YourGUIStyle.stretchWidth = expandWidth; | |
YourGUIStyle.wordWrap = wordWrap; | |
if (YourGUIStyle.stretchHeight) | |
{ | |
YourGUIStyle.fixedHeight = fixedHeight; | |
} | |
if (YourGUIStyle.stretchWidth) | |
{ | |
YourGUIStyle.fixedWidth = fixedWidth; | |
} | |
} | |
/// <summary> | |
/// Updated the Script GUI on the screen. | |
/// </summary> | |
void OnGUI() | |
{ | |
//if Paused | |
if (paused) | |
{ | |
//Make quit game button | |
if (UseGUIStyle) | |
{ | |
// Make a title in your menu | |
GUI.Box(new Rect(Screen.width / 2 - 540, Screen.height / 2.1f - 20, 270, 178), "Paused", YourGUIStyle); | |
// Load by int or string? | |
if (UseGUIContent) | |
useInt = GUILayout.Toggle(useInt, ToggleButtonGUIContent); | |
else | |
useInt = GUILayout.Toggle(useInt, "Use String or Int to call a scene."); | |
//Make a Main Menu | |
if (GUI.Button(new Rect(Screen.width / 2 - 540, Screen.height / 1.5f - 100, 270, 50), "Main menu", YourGUIStyle)) | |
{ | |
if (useInt) { SceneManager.LoadScene(HomeScene); } | |
else { SceneManager.LoadScene(SceneName); } | |
} | |
//Make an options button | |
if (GUI.Button(new Rect(Screen.width / 2 - 540, Screen.height / 1.5f - 50, 270, 50), "Options Menue", YourGUIStyle)) | |
{ | |
// Enables other options menu | |
if (DropDownMenu == false) | |
{ | |
DropDownMenu = true; | |
DropDownMenu2 = false; | |
} | |
else { | |
DropDownMenu = false; | |
DropDownMenu2 = false; | |
} | |
} | |
//Make a Quit Button | |
if (GUI.Button(new Rect(Screen.width / 2 - 540, Screen.height / 1.5f + 0, 270, 50), "Quit Game", YourGUIStyle)) | |
QuitApplication(); | |
} | |
else { | |
// Make a title in your menu | |
GUI.Box(new Rect(Screen.width / 2 - 540, Screen.height / 2.1f - 20, 270, 178), "Paused"); | |
// Load by int or string? | |
if (UseGUIContent) | |
useInt = GUILayout.Toggle(useInt, ToggleButtonGUIContent); | |
else | |
useInt = GUILayout.Toggle(useInt, "Use String or Int to call a scene."); | |
//Make a Main Menu | |
if (GUI.Button(new Rect(Screen.width / 2 - 540, Screen.height / 1.5f - 100, 270, 50), "Main menu")) | |
{ | |
if (useInt) { SceneManager.LoadScene(HomeScene); } | |
else { SceneManager.LoadScene(SceneName); } | |
} | |
//Make an options button | |
if (GUI.Button(new Rect(Screen.width / 2 - 540, Screen.height / 1.5f - 50, 270, 50), "Options Menue")) | |
{ | |
// Enables other options menu | |
if (DropDownMenu == false) | |
{ | |
DropDownMenu = true; | |
DropDownMenu2 = false; | |
} | |
else { | |
DropDownMenu = false; | |
DropDownMenu2 = false; | |
} | |
} | |
if (GUI.Button(new Rect(Screen.width / 2 - 540, Screen.height / 1.5f + 0, 270, 50), "Quit Game")) | |
QuitApplication(); | |
} | |
if (DropDownMenu) | |
{ | |
if (GUI.Button(new Rect(Screen.width / 2 - 315, Screen.height / 2 + 0, 230, 50), "Other Menu ")) | |
{ | |
if (!Equals(DropDownMenu2, false)) | |
{ | |
DropDownMenu = true; | |
DropDownMenu2 = true; | |
} | |
else { | |
DropDownMenu = false; | |
DropDownMenu2 = false; | |
} | |
} | |
if (GUI.Button(new Rect(Screen.width / 2 - 315, Screen.height / 2 + 50, 230, 50), "Options")) | |
{ | |
EnableOptions = !EnableOptions; | |
} | |
if (EnableOptions) | |
{ | |
// So Some GUI Options | |
} | |
} | |
if (DropDownMenu2) | |
{ | |
if (GUI.Button(new Rect(Screen.width / 2 - 315, Screen.height / 2 + 100, 230, 50), "Facebook")) | |
{ | |
GameObject go = GameObject.FindWithTag("Facebook"); | |
if (!Equals(go, null)) | |
gameObject.SetActive(true); | |
} | |
if (GUI.Button(new Rect(Screen.width / 2 - 315, Screen.height / 2 + 150, 230, 50), "Twitter")) | |
{ | |
GameObject go = GameObject.FindWithTag("Twitter"); | |
if (!Equals(go, null)) | |
gameObject.SetActive(true); | |
} | |
if (GUI.Button(new Rect(Screen.width / 2 - 315, Screen.height / 2 + 200, 230, 50), "Email")) | |
{ | |
GameObject go = GameObject.FindWithTag("Email"); | |
if (!Equals(go, null)) | |
gameObject.SetActive(true); | |
} | |
if (GUI.Button(new Rect(Screen.width / 2 - 315, Screen.height / 2 + 250, 230, 50), "Net Browser")) | |
{ | |
GameObject go = GameObject.FindWithTag("WebBrowser"); | |
if (!Equals(go, null)) | |
gameObject.SetActive(true); | |
} | |
} | |
} | |
} | |
#endregion | |
#endregion | |
} | |
#endregion | |
#region Script - 2 SaveData | |
public static class SaveData{ | |
public static List<string>PlayerNamesList = new List<string>(); | |
public static List<string>IdList = new List<string>(); | |
public static List<bool>ConnectedPlayers = new List<bool>(); | |
public static void Load(string name, int id){ | |
} | |
public static void Save(string name, int id){ | |
} | |
public bool DisplayWarning; | |
public static void OnEnable(){ | |
for(int i=1;i<IdList.Count:i++){ | |
if(i==Player.PlayerID){ | |
if(ConnectedPlayers[i]==true){ | |
DisplayWarning=true; | |
return; | |
}else{ | |
// Only storing there user name id and if they are connected. servers should handle the rest | |
Player.PlayerName=PlayerPrefs.GetString("UserName"+i); | |
Player.PlayerID==PlayerPrefs.GetString("UserID"+i); | |
Player.PlayerIndex=i | |
Player.Connected=true; | |
PlayerNamesList[i]=Player.PlayerName; | |
IdList[i]=Player.PlayerID; | |
ConnectedPlayers[i]=Player.Connected; | |
} | |
} | |
} | |
} | |
public static void OnDisable(){ | |
// Only storing there user name id and if they are connected. servers should handle the rest | |
PlayerPrefs.SetString("UserName"+PlayerName.PlayerIndex,Player.PlayerName); | |
PlayerPrefs.SetString("UserID"+PlayerName.PlayerIndex,Player.PlayerID); | |
PlayerName.Connected=false; | |
PlayerNamesList.Remove(Player.PlayerName); | |
IdList.Remove(Player.PlayerID); | |
ConnectedPlayers[PlayerName.PlayerIndex]=PlayerName.Connected; | |
} | |
} | |
public static class Player{ | |
public static string PlayerName | |
public static int PlayerIndex | |
public static string PlayerID; | |
public static bool IsConnected; | |
} | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment