Skip to content

Instantly share code, notes, and snippets.

@HolyFot
Created July 17, 2020 20:40
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 HolyFot/f7d058b1af46f82386d11f2e8f5beb01 to your computer and use it in GitHub Desktop.
Save HolyFot/f7d058b1af46f82386d11f2e8f5beb01 to your computer and use it in GitHub Desktop.
Re-usable YesNo Window C# Unity
//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
}
//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