// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus). // It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) using UnityEngine; using UnityEngine.UI; using System.Collections; using UnityEngine.EventSystems; using System.Linq; using MoonSharp.Interpreter; namespace Fungus { /// /// Presents multiple choice buttons to the players. /// public class MenuDialog : MonoBehaviour { [Tooltip("Automatically select the first interactable button when the menu is shown.")] [SerializeField] protected bool autoSelectFirstButton = false; protected Button[] cachedButtons; protected Slider cachedSlider; private int nextOptionIndex; #region Public members /// /// Currently active Menu Dialog used to display Menu options /// public static MenuDialog ActiveMenuDialog { get; set; } /// /// A cached list of button objects in the menu dialog. /// /// The cached buttons. public virtual Button[] CachedButtons { get { return cachedButtons; } } /// /// A cached slider object used for the timer in the menu dialog. /// /// The cached slider. public virtual Slider CachedSlider { get { return cachedSlider; } } /// /// Sets the active state of the Menu Dialog gameobject. /// public virtual void SetActive(bool state) { gameObject.SetActive(state); } /// /// Returns a menu dialog by searching for one in the scene or creating one if none exists. /// public static MenuDialog GetMenuDialog() { if (ActiveMenuDialog == null) { // Use first Menu Dialog found in the scene (if any) var md = GameObject.FindObjectOfType(); if (md != null) { ActiveMenuDialog = md; } if (ActiveMenuDialog == null) { // Auto spawn a menu dialog object from the prefab GameObject prefab = Resources.Load("Prefabs/MenuDialog"); if (prefab != null) { GameObject go = Instantiate(prefab) as GameObject; go.SetActive(false); go.name = "MenuDialog"; ActiveMenuDialog = go.GetComponent(); } } } return ActiveMenuDialog; } protected virtual void Awake() { Button[] optionButtons = GetComponentsInChildren