This script provides a way to have a Collider2D that is configured as a trigger, so that it can have a Conversation ScriptableObject triggered to be executed by the DialogManager. See version 2 below, it also includes logic to use key-value pair data to determine if a dialog should be replayed when revisiting a scene. The DialogManager version2 has logic that sets conversations as completed by default so they won't be replayed when returning to a scene.
DialogTrigger.cs (Important: see version 2 below)
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;/// <summary>/// This class provides a logic for the player to trigger dialog using a 2D collider/// This script will be added to a gameObject with a collider2D with isTrigger true////// THis script requires that the dialogManager instance which will execute the dialog/// must be populated in the inspector panel/// as well as the ConversationList scriptableObject that will be displayed when triggered////// This can be used on multiple objects within a scene, where each instance will have it's own/// conversation to be exectued. /// </summary>publicclassDialogTrigger:MonoBehaviour{publicConversationList convList; //populate in the inspectorpublicDialogManager dialogManager; //Populate in the InspectorprivatevoidOnTriggerEnter2D(Collider2D collision) {if (collision.CompareTag("Player")) {Debug.Log("Collided with DialogTrigger");if (dialogManager !=null) //populated in the inspector {dialogManager.TriggeredDialog(convList); //pass the convList as a parameter into the DialogManagerDebug.Log("Trigger dialog"); }else {Debug.Log("Couldn't find dialogManager to trigger"); } } }} //end class
DialogTrigger version2
In this version, a series of values associated with a given key, will determine conditions when the conversation will be replayed in a scene. This requires that the key-value pair: key: ConvList.name value: "completed" be removed from the choiceData dictionary so that the DialogManager will replay the script.
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;/// <summary>/// This class provides a logic for the player to trigger dialog using a 2D collider/// This script will be added to a gameObject with a collider2D with isTrigger true////// THis script requires that the dialogManager instance which will execute the dialog/// must be populated in the inspector panel/// as well as the ConversationList scriptableObject that will be displayed when triggered////// This can be used on multiple objects within a scene, where each instance will have it's own/// conversation to be exectued. /// </summary>publicclassDialogTrigger:MonoBehaviour{publicConversationList convList; //populate in the inspectorpublicDialogManager dialogManager; //Populate in the Inspector [SerializeField]string key; //value required in order to be visible [SerializeField]string[] active_values; //set values that determine visibility within each sceneprivatevoidStart() { //what NPC state is the condition that requires this to be replayed //example: ZombieState == "hungry"string curValue =GameData.instanceRef.GetChoice(key); bool activate =false; //add logic to see if conversation should be replayed - replay, make conversation active if this state is truefor ( int i=0; i<active_values.Length; i++) {if( curValue ==active_values[i]) { activate =true;Debug.Log("reactivated conversation "+convList.name ); } }if(activate) //activate by removing the key from the dictionary to remove value 'completed' {GameData.instanceRef.RemoveChoice(convList.name); //key to remove is ConvList.name } }privatevoidOnTriggerEnter2D(Collider2D collision) {if (collision.CompareTag("Player")) {Debug.Log("Collided with DialogTrigger");if (dialogManager !=null) //populated in the inspector {dialogManager.TriggeredDialog(convList); //pass the convList as a parameter into the DialogManagerDebug.Log("Trigger dialog");Destroy(this.gameObject); //destroy trigger gameObject so that it doesn't get retriggered in the scene }else {Debug.Log("Couldn't find dialogManager to trigger"); } } }} //end class