using UnityEngine; using System; using System.Collections; using System.Collections.Generic; namespace Fungus { /** * Permitted states for Dialogs. */ public enum DialogMode { /// Dialog has no pending content to display so is not shown. Idle, /// Dialog is currently writing out content. Writing, /// Dialog has finished writing out content and is waiting for player input. Waiting } /** * Interface for Dialog implementations. * This allows us to introduce new types of Dialog in future. */ public interface IDialog { /** * Returns the current state of the Dialog. */ DialogMode GetDialogMode(); /** * Display a line of story text. * If any options have previously been added using AddOption(), these will be displayed and the * user must choose an option to continue. The sayAction parameter is ignored * @param sayText The line of story text to be displayed. * @param sayAction Delegate method to call when the player taps to continue. */ void Say(string sayText, Action sayAction); /** * Clear the current list of options previously added using AddOption(). */ void ClearOptions(); /** * Add an option to the list of options to be displayed on the next call to Say(). * @param optionText Text to display on the button for the option. * @param optionAction Delegate method to call when the option button is pressed. */ void AddOption(string optionText, Action optionAction); /** * Sets a time limit for the player to choose between multiple options. * If the player fails to make a choice in time then the timeoutAction delegate method is called. * This setting only applies to the next Say() command and will be reset afterwards. * @timeoutDuration Length of time for the player to choose an option. * @timeoutAction Delegate method to call when player fails to choose an option. */ void SetTimeout(float timeoutDuration, Action timeoutAction); } [ExecuteInEditMode] public class Dialog : MonoBehaviour, IDialog { /** * Padding values for each side of the dialog. * Values are in normalized screen coords [0..1] */ [Serializable] public class Layout { /** * Pushes the left dialog edge away from the left side of the screen. */ public bool leftSpace = false; /** * Pushes the top dialog edge away from the top side of the screen. */ public bool topSpace = true; /** * Pushes the right dialog edge away from the right side of the screen. */ public bool rightSpace = false; /** * Pushes the bottom dialog edge away from the bottom side of the screen. */ public bool bottomSpace = false; /** * Minimum dimensions of the dialog. * The dialog may expand beyond these dimensions to fit content. * Coordinates are in normalized screen coordinates [0..1] */ public Vector2 size = new Vector2(1f, 0.25f); /** * Offsets the dialog relative to the top left corner of the screen. * Coordinates are in normalized screen coordinates [0..1] */ public Vector2 offset; } /** * Layout values used to control size and position of the dialog. */ public Layout layout; /** * Defines the dialog display properties of a game character. */ [System.Serializable] public class Character { /** * Side of screen to display character image. */ public enum ImageSide { /// Display image on the left side of the dialog. Left, /// Display image on the right side of the dialog. Right } /** * Indentifier for this character for use with the SetCharacter() method. */ public string characterID; /** * Name text to display for the character. * If empty then the name field is not displayed. */ public string name; /** * The color for the name text. * This always overrides any color value specified in the NameStyle property. */ public Color nameColor; /** * Image to display for the character. * If no image is specified then no character image will be displayed. */ public Texture2D image; /** * Side of dialog where character image will be displayed. */ public ImageSide imageSide; } /** * List of game characters that can be displayed using this dialog. */ public Character[] characters; /** * Active character to use when displaying dialog. */ public int activeCharacter; /** * Writing speed for say text in Characters Per Second. */ public int writingSpeed = 100; /** * Sound to play while text is being written in the dialog. */ public AudioClip writingSound; /** * Loop the writing sound as long as text is being written. */ public bool loopWritingSound = true; /** * Sound effect to play when the player taps to continue. */ public AudioClip continueSound; /** * Icon to display under the story text when waiting for player to tap to continue. */ public Texture2D continueIcon; /** * Number of buttons to display in the same row when showing multiple options */ public int buttonsPerRow = 2; /** * Minimum width of each button as a fraction of the screen width [0..1]. * This is useful to create a column of buttons with matching width. */ public float minButtonWidth = 0; /** * Padding offset to apply around the character image, in pixels. */ public RectOffset imagePadding; /** * Scale of character image, specified as a fraction of current screen height [0..1]. */ public float imageScale = 0.25f; /** * Animation frames for multiple choice time indicator */ public Texture2D[] timerAnimation; /** * Scale of timer image, specified as a fraction of current screen height [0..1]. */ public float timerScale = 0.2f; /** * Sound effect to play when time indicator advances. */ public AudioClip timerSound; /** * Style for dialog box background. */ public GUIStyle boxStyle; /** * Style for name text. */ public GUIStyle nameTextStyle; /** * Style for say text. */ public GUIStyle sayTextStyle; /** * Style for option buttons */ public GUIStyle buttonStyle; DialogMode dialogMode; public DialogMode GetDialogMode() { return dialogMode; } class Option { public string optionText; public Action optionAction; public Option(string _optionText, Action _optionAction) { optionText = _optionText; optionAction = _optionAction; } } List