Guest User

Untitled

a guest
Aug 10th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. // This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
  2. // It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
  3.  
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6.  
  7. namespace Fungus
  8. {
  9. /// <summary>
  10. /// Supported modes for clicking through a Say Dialog.
  11. /// </summary>
  12. public enum ClickMode
  13. {
  14. /// <summary> Clicking disabled. </summary>
  15. Disabled,
  16. /// <summary> Click anywhere on screen to advance. </summary>
  17. ClickAnywhere,
  18. /// <summary> Click anywhere on Say Dialog to advance. </summary>
  19. ClickOnDialog,
  20. /// <summary> Click on continue button to advance. </summary>
  21. ClickOnButton
  22. }
  23.  
  24. /// <summary>
  25. /// Input handler for say dialogs.
  26. /// </summary>
  27. public class DialogInput : MonoBehaviour
  28. {
  29. [Tooltip("Click to advance story")]
  30. [SerializeField] public ClickMode clickMode;
  31.  
  32. [Tooltip("Delay between consecutive clicks. Useful to prevent accidentally clicking through story.")]
  33. [SerializeField] protected float nextClickDelay = 0f;
  34.  
  35. [Tooltip("Allow holding Cancel to fast forward text")]
  36. [SerializeField] protected bool cancelEnabled = true;
  37.  
  38. [Tooltip("Ignore input if a Menu dialog is currently active")]
  39. [SerializeField] protected bool ignoreMenuClicks = true;
  40.  
  41. protected bool dialogClickedFlag;
  42.  
  43. protected bool nextLineInputFlag;
  44.  
  45. protected float ignoreClickTimer;
  46.  
  47. protected StandaloneInputModule currentStandaloneInputModule;
  48.  
  49. protected Writer writer;
  50.  
  51. protected virtual void Awake()
  52. {
  53. writer = GetComponent<Writer>();
  54.  
  55. CheckEventSystem();
  56. }
  57.  
  58. // There must be an Event System in the scene for Say and Menu input to work.
  59. // This method will automatically instantiate one if none exists.
  60. protected virtual void CheckEventSystem()
  61. {
  62. EventSystem eventSystem = GameObject.FindObjectOfType<EventSystem>();
  63. if (eventSystem == null)
  64. {
  65. // Auto spawn an Event System from the prefab
  66. GameObject prefab = Resources.Load<GameObject>("Prefabs/EventSystem");
  67. if (prefab != null)
  68. {
  69. GameObject go = Instantiate(prefab) as GameObject;
  70. go.name = "EventSystem";
  71. }
  72. }
  73. }
  74.  
  75. protected virtual void Update()
  76. {
  77. if (EventSystem.current == null)
  78. {
  79. return;
  80. }
  81.  
  82. if (currentStandaloneInputModule == null)
  83. {
  84. currentStandaloneInputModule = EventSystem.current.GetComponent<StandaloneInputModule>();
  85. }
  86.  
  87. if (writer != null && writer.IsWriting)
  88. {
  89. if (Input.GetButtonDown(currentStandaloneInputModule.submitButton) ||
  90. (cancelEnabled && Input.GetButton(currentStandaloneInputModule.cancelButton)))
  91. {
  92. SetNextLineFlag();
  93. }
  94. }
  95.  
  96. switch (clickMode)
  97. {
  98. case ClickMode.Disabled:
  99. break;
  100. case ClickMode.ClickAnywhere:
  101. if (Input.GetMouseButtonDown(0))
  102. {
  103. SetNextLineFlag();
  104. }
  105. break;
  106. case ClickMode.ClickOnDialog:
  107. if (dialogClickedFlag)
  108. {
  109. SetNextLineFlag();
  110. dialogClickedFlag = false;
  111. }
  112. break;
  113. }
  114.  
  115. if (ignoreClickTimer > 0f)
  116. {
  117. ignoreClickTimer = Mathf.Max (ignoreClickTimer - Time.deltaTime, 0f);
  118. }
  119.  
  120. if (ignoreMenuClicks)
  121. {
  122. // Ignore input events if a Menu is being displayed
  123. if (MenuDialog.ActiveMenuDialog != null &&
  124. MenuDialog.ActiveMenuDialog.IsActive() &&
  125. MenuDialog.ActiveMenuDialog.DisplayedOptionsCount > 0)
  126. {
  127. dialogClickedFlag = false;
  128. nextLineInputFlag = false;
  129. }
  130. }
  131.  
  132. // Tell any listeners to move to the next line
  133. if (nextLineInputFlag)
  134. {
  135. var inputListeners = gameObject.GetComponentsInChildren<IDialogInputListener>();
  136. for (int i = 0; i < inputListeners.Length; i++)
  137. {
  138. var inputListener = inputListeners[i];
  139. inputListener.OnNextLineEvent();
  140. }
  141. nextLineInputFlag = false;
  142. }
  143. }
  144.  
  145. #region Public members
  146.  
  147. /// <summary>
  148. /// Trigger next line input event from script.
  149. /// </summary>
  150. public virtual void SetNextLineFlag()
  151. {
  152. nextLineInputFlag = true;
  153. }
  154.  
  155. /// <summary>
  156. /// Set the dialog clicked flag (usually from an Event Trigger component in the dialog UI).
  157. /// </summary>
  158. public virtual void SetDialogClickedFlag()
  159. {
  160. // Ignore repeat clicks for a short time to prevent accidentally clicking through the character dialogue
  161. if (ignoreClickTimer > 0f)
  162. {
  163. return;
  164. }
  165. ignoreClickTimer = nextClickDelay;
  166.  
  167. // Only applies in Click On Dialog mode
  168. if (clickMode == ClickMode.ClickOnDialog)
  169. {
  170. dialogClickedFlag = true;
  171. }
  172. }
  173.  
  174. /// <summary>
  175. /// Sets the button clicked flag.
  176. /// </summary>
  177. public virtual void SetButtonClickedFlag()
  178. {
  179. // Only applies if clicking is not disabled
  180. if (clickMode != ClickMode.Disabled)
  181. {
  182. SetNextLineFlag();
  183. }
  184. }
  185.  
  186. #endregion
  187. }
  188. }
  189.  
Add Comment
Please, Sign In to add comment