Created
July 17, 2020 20:40
-
-
Save HolyFot/f7d058b1af46f82386d11f2e8f5beb01 to your computer and use it in GitHub Desktop.
Re-usable YesNo Window C# Unity
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
//Example | |
public YesNoWindow yesNoWindow; | |
public void ExitGame() | |
{ | |
if (yesNoWindow != null) | |
{ | |
yesNoWindow.onYesClickCall += ExitGame2; | |
yesNoWindow.ShowYesNo("Are you sure you wish to quit the game?"); | |
} | |
} | |
private void ExitGame2() | |
{ | |
#if UNITY_EDITOR | |
UnityEditor.EditorApplication.isPlaying = false; | |
#else | |
Application.Quit(); | |
#endif | |
} |
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
//Made by: HolyFot | |
//License: CC0 - https://creativecommons.org/share-your-work/public-domain/cc0/ | |
//Easy re-usable Yes/No menu using a delegate call back. | |
using System; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using UnityEngine.EventSystems; | |
using UnityEngine.Events; | |
public class YesNoWindow : MonoBehaviour | |
{ | |
public Text msg; | |
public Button YesBtn; | |
public Button NoBtn; | |
public GameObject window; | |
public delegate void EventYesClicked(); | |
public event EventYesClicked onYesClickCall; | |
void Awake() | |
{ | |
if (NoBtn != null) | |
NoBtn.onClick.AddListener(delegate { HideWindow(); }); | |
if (YesBtn != null) | |
YesBtn.onClick.AddListener(delegate { YesClick(); }); | |
HideWindow(); | |
} | |
public void ShowYesNo(string message) | |
{ | |
msg.text = message; | |
window.SetActive(true); | |
} | |
public void YesClick() | |
{ | |
if (onYesClickCall != null) | |
{ | |
onYesClickCall.Invoke(); | |
} | |
HideWindow(); | |
} | |
public void HideWindow() | |
{ | |
window.SetActive(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment