User-Choices - Example
To demonstrate a simple example of using a C# Dictionary for storing and accessing user choice data, consider the following example.
ChoicePackage Version 1 uses Images for Buttons, with a Horizontal Layout Group Unity Package with Prefab Panels, Scripts, Images: Box.com link: ChoicePackage
ChoicePackage_V2 uses default buttons and uses 1 panel to contain all gameObjects. This version uses a Vertical Layout Group for the Panel that holds the Buttons This may be easier to use and modify. Unity Package with Prefab Panels, Scripts: Box.Com Link: ChoicePackage_V2
Directions for creating ChoicePackage Version 1 In BeginScene, You will create 2 UI-Panels, one holds 3 buttons, one has text to display the selected choice. In the image below we can see that for this example, we'll work with 2 panels, one to allow selecting a choice, and the other panel to display the choice. The ChoicePanel has 3 child buttons to allow the user to make a choice that isn't directly associated with changing scenes. This example uses images for the buttons, that's not necessary.

Both panels should have Canvas-Group Components, and The Hide_Show_Panel Script attached. ( if you haven't created Hide_Show_Panel script before, do it now) This script gives options in the inspector for any panel with a CanvasGroup that allows for easy configuration of having a panel opened or closed by a button, when this script is added to a panel, it will be hidden on start by default.
Scripts: Both examples use 2 new scripts: SaveChoice.cs, and ShowChoice.cs and includes changes to GameData.cs version 1. The modified GameData.cs code is included at the bottom of this page.


The general idea is that we'd like a simple script that can be attached to any Button so that clicking the Button will update the database for the key,value pair associated with the Button.
ShowChoice Panel with ShowChoice.cs
Displays the Dictionary value for the specified key

The inspector below shows that the ShowChoice Script allows the Dictionary key to be specified. This panel is hidden at start, and opens when a choiceButton has been selected.

This script can be attached to a Panel that has a CanvasGroup and a child Text element. If you add the Hide_Show_Panel.cs script to the ShowChoice panel, then it'll be hidden when the scene starts.
SaveChoice.cs - Attach to each child Button

Set the Key, Value pair for the SaveChoice Component
The script below is attached to a Button, and has code to be executed when the Button is clicked, so first we have to find the Button Component in Start, this is a bit of a work-around. When one of the Buttons is clicked, it updated the Dictionary in GameData. Then it finds it's parent and gets the CanvasGroup of the parent and hides the entire panel. Then it checks to see if the parent panel has an attached Hide_Show_Panel script with a nextPanelToOpen. It opens that panel and checks to see if there is a ShowChoice script on that Panel, if so, it executes: UpdateDisplay( ). See code for that script below.
GameData v2; Code added for choiceData Dictionary<string, string>
Last updated
Was this helpful?