Last active
April 10, 2017 22:20
-
-
Save altunsercan/0e867eedd53737119f9f3cb10ced4edb to your computer and use it in GitHub Desktop.
Fungus Game: Input Dialog implementation from blog post http://sercanaltun.com/blog/fungus-game-input-dialog-writing-custom-commands
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
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 |
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
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(); | |
} | |
} | |
} | |
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
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