Make sure to update GameData to Version 3, so it includes new code added for choiceData Dictionary<string, string>
Dictionary: choiceData: SaveChoice.cs and ShowChoice.cs scripts
To demonstrate a simple example of using a C# Dictionary for storing and accessing user choice data, consider the following example.
SaveChoice Example uses UI-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.
Required components for Panel: Hide_Show_Panel.cs and Canvas-Group
Both panels should have Canvas-Group Components, and The Hide_Show_PanelScript attached. ( if you haven't created Hide_Show_Panel script, it will be downloaded with the package) The Hide_Show_Panel 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, if you download this package, it will overwrite your GameData and update it to GameData Version3
SaveChoice.cs
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.
Button onClick.AddListener( ) In addition to storing the SaveChoice key,value pair we can also configure these buttons to open panels, trigger dialog, to implement our conditional branching narrative structure. This will be covered in the section: Configure UI-Button Listeners
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.
The SaveChoice script can also function as a way to trigger opening DialogManager if there's an attached Conversation on the script.
usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.UI;publicclassSaveChoice:MonoBehaviour{ //set in inspector for each Button [SerializeField]ConversationList nextConversation; //if next conversation exists [SerializeField]DialogManager dialogManager; //if next conversation exists, set dialogManager [SerializeField]CanvasGroup nextPanelToOpen; //if not using dialogManager or nextConversatoin [SerializeField]publicList<KeyValuePair> choiceList; //a list of dictionary key-value pairs to be set when the button is clickedButton thisItem; // Start is called before the first frame updateprivatevoidStart() { thisItem =GetComponent<Button>();thisItem.onClick.AddListener(OnChoiceSelected); }publicvoidOnChoiceSelected() { //loop through list of keyValuePairs, Save key,value pair to GameData dictionary: choiceDataforeach (KeyValuePair keyValuePair in choiceList) { //if special gameState - type valueGameData.instanceRef.SaveChoice(keyValuePair.key,keyValuePair.value); //don't use improveState valueDebug.Log("Saved Key "+keyValuePair.key+" Saved Value "+GameData.instanceRef.GetChoice(keyValuePair.key)); //pull from stored value }thisItem.Select(); //show Button selected Image //Find Parent Panel and hideGameObject parentGameObject =this.transform.parent.gameObject; //find parentif (parentGameObject !=null) {CanvasGroup parentCG =parentGameObject.GetComponent<CanvasGroup>();if (parentCG !=null) {Utility.HideCG(parentCG); //hide the parent choice panel } } //either set dialogManager with nextConversationif (nextConversation !=null&& dialogManager !=null) //NextPanel will be dialogManager {dialogManager.TriggeredDialog(nextConversation); // }elseif (nextPanelToOpen !=null)//or show panel nextPanelToOpenindicated {Utility.ShowCG(nextPanelToOpen); //on to open next panel specified on parent } }}//end class
Optional: 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.
usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.UI;publicclassShowChoice:MonoBehaviour{publicstring key;publicText showText; //populate in inspectorpublicvoidUpdateDisplay() {string value =GameData.instanceRef.GetChoice(key);showText.text= value;Debug.Log("show choice "+ value); }}