Dialogs cover all windows, popups and messages that you might need to display in your game. Game Framework dialog functionality provides a standard framework for creating and managing dialogs including customisation, animation and transitioning. Several standard system dialogs are provided for things like general messaging, game over and settings.
The below shows some instances of dialogs in action:
The most general purpose dialog is GeneralMessage (FlipWebApps\GameFramework\Resources\Default\Dialog\GeneralMessage) which can display general information popups with optional text, image, buttons and custom content
The DialogInstance component forms the basis of the dialog system and one must be associated with every dialog. DialogInstance will typically be placed on the root gameobject of your dialog. You can trigger the DialogInstance’s Show() method from code or some other event listener to display the dialog, optionally passing in things like a title, display text, display image and which dialog buttons to show (if any).
You can close a dialog by clicking on a displayed button or calling one of the DialogInstance DoneXX() methods. This will result in the dialog closing, the DialogInstance DialogResult property being set with a code indicating the button clicked and any callback being triggered before the dialog is transitioned out / closed.
Internally DialogInstance makes some assumptions about the naming of gameobjects within your dialog instance in order to do replacement – see the Show() method in DialogInstance.cs for more details.
DialogManager can be used to dynamically create dialogs and also optionally display them through the returned DialogInstance reference. To display a GeneralMessage dialog add a DialogManager component to your scene and then from code call one of the following functions with parameters for fields like the text to be displayed etc.:
DialogManager also has methods for creating more advanced dialogs that can load prefabs and content from resource folders. These create methods return a DialogInstance reference that again can be used to interact with the created dialog.
There are a number of dialog prefabs that you can use located under Resources/Default/Dialog and Prefabs/UI . If you have the extras bundle then themed dialogs are under Themes/[Theme]/Prefabs/Dialog.
Showing and Swapping
As discussed above, dialogs can be created and optionally displayed through DialogManager. You can also use a reference to DialogInstance to show dialogs, either those created by DialogManager, or by dragging a dialog prefab into your scene and getting a reference to it’s DialogInstance component.
You can also show a dialog a single time which is useful for giving instructions when you first run your game. This can be done through the DialogManager.Instance.ShowOnce method or using the ShowDialogOnce component.
See the dialogs tutorial for a demo and walkthrough of the above.
If you have the Beautiful Transitions asset installed then dialogs will automatically make use of any added transitions to transition in / out dialogs when they are shown / closed.
If you have multiple dialogs added to your scene then you can swap between then using the DialogInstance SwapTo() method on the displayed dialog passing a reference to the DialogInstance that you want to swap to. Again if you have the Beautiful Transitions asset installed this will run any transitions at the same time. The components OnMouseClickOrTapSwapDialogInstance and OnButtonClickSwapDialogInstance also provide the same functionality, automatically reacting to mouse / tap input or button a button click.
The default setting window allows you to control the music and audio volumes and also restore purchases if you have in app purchase enabled. You can also easily extend the settings window to add your own configuration properties.
The getting started tutorial shows how to include the settings window in your game, however the process basically involves dragging one of the settings window prefabs into your scene and then either calling Settings.Instance.Show() from code or adding the OnButtonClickShowSettings component to a UI Button gameobject.
To customise the settings window you should create a copy of one of the settings prefabs and modify as needed. Settings assumes the following gameobject name / component combinations are present:
- “MusicSlider” (UI.Slider) – a slider for controlling the music level
- “SfxSlider” (UI.Slider) – a slider for controlling the sound effect level
- “LanguageDropdown” (UI.Dropdown) – a dropdown for setting the language from those listed in GameManager.
To control any additional options you should create a subclass of the Settings class and override the Show() method to setup any properties in the new settings window and / or the DoneCallback() method to update values when done (be sure to call through to the base Settings methods when done). You should then replace the existing Settings component on your new prefab with this new class.
The pause window can be used in game to both pause the game and giving the user various options for how they want to continue.
The getting started tutorial shows how to include the pause window in your game, however the process basically involves dragging one of the pause window prefabs into your scene and then calling either LevelManager.Instance.PauseLevel() directly from code or adding the OnButtonClickPauseLevel component to a UI Button gameobject. You can test whether the game is paused through the LevelManager.Instance.IsLevelPaused property and react accordingly such as disabling user input.
There are various options that you can configure in the PauseWindow script that is attached to the Pause Window prefab including what options to show.
Both of the OnButtonClickPauseLevel component and the PauseWindow script allow you to work with Unity’s TimeScale. If you set the Time Scale value in the OnButtonClickPauseLevel component to a value other than 1 to pause things like physics and the like then be sure to set it back to 1 when you are done, either manually or through the Always Reset Time Scale Pause Window option. Failure to do so might result in other parts of your game appearing to freeze.
If you have any thoughts or suggestions on improvements or additions to the dialog features then please let us know!