You will need to create a simple Prefab that you can use in multiple times within one scene and within multiple scenes to allow the player to make choices during gameplay to determine subsequent game conditions.
CanvasGroup component - REMEMBER TO ADD CANVAS GROUP
Vertical Layout Group component
Each UI-Button will require an instance of the SaveChoice Script(see below).
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. 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.
KeyValuePair.cs - Data Class created for Dictionary Data
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 value Debug.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