Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fungus Game: Input Dialog implementation from blog post http://sercanaltun.com/blog/fungus-game-input-dialog-writing-custom-commands
Following gist is created for a blog post. If you want to read the details you can read the blog at:
http://sercanaltun.com/blog/fungus-game-input-dialog-writing-custom-commands
using UnityEngine;
using UnityEngine.UI;
using System;
using UnityEngine.EventSystems;
public class InputDialog : MonoBehaviour
{
// Currently active Input Dialog used to display Menu options
public static InputDialog activeInputDialog;
public InputField Input;
public Text Label;
public Button SubmitButton;
protected Action<string> Callback;
public static InputDialog GetInputDialog()
{
if (activeInputDialog == null)
{
// Use first Input Dialog found in the scene (if any)
InputDialog id = GameObject.FindObjectOfType<InputDialog>();
if (id != null)
{
activeInputDialog = id;
}
if (activeInputDialog == null)
{
// Auto spawn a menu dialog object from the prefab
GameObject prefab = Resources.Load<GameObject>("InputDialog");
if (prefab != null)
{
GameObject go = Instantiate(prefab) as GameObject;
go.SetActive(false);
go.name = "InputDialog";
activeInputDialog = go.GetComponent<InputDialog>();
}
}
}
return activeInputDialog;
}
public virtual void Awake()
{
if (Application.isPlaying)
{
Clear();
}
}
public virtual void OnEnable()
{
// The canvas may fail to update if the input dialog is enabled in the first game frame.
// To fix this we just need to force a canvas update when the object is enabled.
Canvas.ForceUpdateCanvases();
}
public virtual void Clear()
{
StopAllCoroutines();
if (Input != null)
{
Input.onEndEdit.RemoveAllListeners();
Input.text = "";
}
}
public virtual void InitializeInputField(string label, string placeholder, Action<string> callback )
{
// If inputfield is not set send empty input and continue
Callback = callback;
if (Input == null)
{
Callback("");
return;
}
else
{
EventSystem.current.SetSelectedGameObject(Input.gameObject);
Text placeholderText = Input.placeholder.GetComponent<Text>();
if (placeholderText!=null)
{
placeholderText.text = placeholder;
}
Input.onEndEdit.AddListener(OnEndEdit);
}
if (Label != null)
{
Label.text = label;
}
if (SubmitButton != null)
{
SubmitButton.onClick.AddListener(OnClickSubmit);
}
HideSayDialog();
}
public virtual void OnEndEdit(string inputValue)
{
if (inputValue != "")
{
Callback(inputValue);
Clear();
}
}
public virtual void OnClickSubmit()
{
OnEndEdit(Input.text);
}
public virtual void HideSayDialog()
{
SayDialog sayDialog = SayDialog.GetSayDialog();
if (sayDialog != null)
{
sayDialog.FadeOut();
}
}
}
using UnityEngine;
using Fungus;
[CommandInfo("Variable",
"Set Variable From Input",
"Sets a Boolean, Integer, Float or String variable from Input Dialog")]
[AddComponentMenu("")]
public class SetVariableFromInput : Command {
[Tooltip("The variable whos value will be set")]
[VariableProperty(typeof(BooleanVariable),
typeof(IntegerVariable),
typeof(FloatVariable),
typeof(StringVariable))]
public Variable Variable;
public string Label="";
public string Placeholder="";
public override void OnEnter()
{
InputDialog inputDialog = InputDialog.GetInputDialog();
if (inputDialog == null || Variable == null)
{
Continue();
return;
}
inputDialog.gameObject.SetActive(true);
inputDialog.InitializeInputField(Label,Placeholder, OnInputSubmit);
}
private void OnInputSubmit(string value)
{
if (Variable.GetType() == typeof(BooleanVariable))
{
BooleanVariable boolVar = (Variable as BooleanVariable);
if (value.ToLower() == "true" || value == "1")
{
boolVar.value = true;
}
else
{
boolVar.value = false;
}
}
else if (Variable.GetType() == typeof(IntegerVariable))
{
IntegerVariable intVar = (Variable as IntegerVariable);
int intResult;
if (int.TryParse(value, out intResult))
{
intVar.value = intResult;
}
intVar.value = 0;
}
else if (Variable.GetType() == typeof(FloatVariable))
{
FloatVariable floatVariable = (Variable as FloatVariable);
float floatResult;
if (float.TryParse(value, out floatResult))
{
floatVariable.value = floatResult;
}
floatVariable.value = 0f;
}
else if (Variable.GetType() == typeof(StringVariable))
{
StringVariable stringVar = (Variable as StringVariable);
stringVar.value = value;
}
InputDialog inputDialog = InputDialog.GetInputDialog();
inputDialog.gameObject.SetActive(false);
Continue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.