Skip to content

Instantly share code, notes, and snippets.

@HolyFot
Created July 17, 2020 20:45
Embed
What would you like to do?
//Example
public void AddFriend()
{
//Show Input Window
if (inputWindow != null)
{
inputWindow.onOkClickCall += AddFriend2;
inputWindow.ShowInputWindow("Username to add as friend:");
}
}
public void AddFriend2()
{
//Do something with inputWindow.result
}
//Made by: HolyFot
//License: CC0 - https://creativecommons.org/share-your-work/public-domain/cc0/
//Easy re-usable Input menu using a delegate call back.
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
public class InputWindow : MonoBehaviour
{
public Text msg;
public GameObject window;
public Button OkBtn;
public Button CancelBtn;
public InputField input1;
public string result;
public delegate void EventInputEntered();
public event EventInputEntered onOkClickCall;
void Awake()
{
if (CancelBtn != null)
CancelBtn.onClick.AddListener(delegate { HideWindow(); });
if (OkBtn != null)
OkBtn.onClick.AddListener(delegate { OkClick(); });
HideWindow();
}
public void ShowInputWindow(string message)
{
msg.text = message;
window.SetActive(true);
//Set Focus
input1.ActivateInputField();
input1.Select();
}
public void OkClick()
{
result = input1.text;
if (onOkClickCall != null)
{
onOkClickCall.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