Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Dialogue Inputs

Discussion in 'Editor & General Support' started by Altanin, Aug 19, 2020.

  1. Altanin

    Altanin

    Joined:
    Jul 21, 2020
    Posts:
    15
    Hi all just a few questions.
    I am using a conversation manager asset and all is working well.
    Just had a few tweaks I wanted to make in the code for a few different things to happen.

    In the code its showing that when pressing E the conversation will begin and F presses the continue text button. I want to change this however so it would be like..

    E starts conversation, then if conversation already started, E will press the continue button INSTEAD of starting a new conversation. (I only added the F part to my script because I couldn't do this)

    and I also wanted the conversation to go away if my main character walks away.

    How would I go about scripting this?
    Thanks in advance







    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using DialogueEditor;
    5.  
    6. public class BirdCharacter : MonoBehaviour
    7. {
    8.     public NPCConversation myConversation;
    9.  
    10.     private void OnMouseOver()
    11.  
    12.  
    13.     {
    14.         if (Input.GetKeyDown(KeyCode.E))
    15.         {
    16.             ConversationManager.Instance.StartConversation(myConversation);
    17.  
    18.         }
    19.         else if (Input.GetKeyDown(KeyCode.F))
    20.             ConversationManager.Instance.PressSelectedOption();
    21.  
    22.  
    23.     }
    24. }
    25.  
    26.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    The appropriate place to script this would certainly be inside ConversationManager, and not inside "BirdCharacter".

    Create a function in ConversationManager called "StartOrContinueConversation" that does what you want. Without seeing more about how ConversationManager is implemented, I can't say much more than that, other than you need ConversationManager to internally keep track of the state of a conversation, such as whether or not it has been started already.
     
  3. Altanin

    Altanin

    Joined:
    Jul 21, 2020
    Posts:
    15

    This is the ConversationManager script.
    I just figured it would be a function in the first script I provided because that's where I was able to edit the ability to change key functions in the first place
     
  4. Altanin

    Altanin

    Joined:
    Jul 21, 2020
    Posts:
    15
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.Events;
    4. using UnityEngine.UI;
    5.  
    6. namespace DialogueEditor
    7. {
    8.     public class ConversationManager : MonoBehaviour
    9.     {
    10.         private const float TRANSITION_TIME = 0.2f; // Transition time for fades
    11.  
    12.         public static ConversationManager Instance { get; private set; }
    13.  
    14.         public delegate void ConversationStartEvent();
    15.         public delegate void ConversationEndEvent();
    16.  
    17.         public static ConversationStartEvent OnConversationStarted;
    18.         public static ConversationEndEvent OnConversationEnded;
    19.  
    20.         private enum eState
    21.         {
    22.             TransitioningDialogueBoxOn,
    23.             ScrollingText,
    24.             TransitioningOptionsOn,
    25.             Idle,
    26.             TransitioningOptionsOff,
    27.             TransitioningDialogueOff,
    28.             Off,
    29.             NONE,
    30.         }
    31.  
    32.         // User-Facing options
    33.         // Drawn by custom inspector
    34.         public bool ScrollText;
    35.         public float ScrollSpeed = 1;
    36.         public Sprite BackgroundImage;
    37.         public bool BackgroundImageSliced;
    38.         public Sprite OptionImage;
    39.         public bool OptionImageSliced;
    40.         public bool AllowMouseInteraction;
    41.  
    42.         // Non-User facing
    43.         // Not exposed via custom inspector
    44.         //
    45.         // Base panels
    46.         public RectTransform DialoguePanel;
    47.         public RectTransform OptionsPanel;
    48.         // Dialogue UI
    49.         public Image DialogueBackground;
    50.         public Image NpcIcon;
    51.         public TMPro.TextMeshProUGUI NameText;
    52.         public TMPro.TextMeshProUGUI DialogueText;
    53.         // Components
    54.         public AudioSource AudioPlayer;
    55.         // Prefabs
    56.         public UIConversationButton ButtonPrefab;
    57.         // Default values
    58.         public Sprite BlankSprite;
    59.  
    60.         // Getter properties
    61.         public bool IsConversationActive
    62.         {
    63.             get
    64.             {
    65.                 return m_state != eState.NONE && m_state != eState.Off;
    66.             }
    67.         }
    68.  
    69.         // Private
    70.         private float m_elapsedScrollTime;
    71.         private int m_scrollIndex;
    72.         public int m_targetScrollTextCount;
    73.         private eState m_state;
    74.         private float m_stateTime;
    75.         private Conversation m_conversation;
    76.         private List<UIConversationButton> m_uiOptions;
    77.  
    78.         private SpeechNode m_pendingDialogue;
    79.         private OptionNode m_selectedOption;
    80.         private SpeechNode m_currentSpeech;
    81.  
    82.         // Selection options
    83.         private int m_currentSelectedIndex;
    84.  
    85.  
    86.         //--------------------------------------
    87.         // Awake, Start, Destroy
    88.         //--------------------------------------
    89.  
    90.         private void Awake()
    91.         {
    92.             // Destroy myself if I am not the singleton
    93.             if (Instance != null && Instance != this)
    94.             {
    95.                 GameObject.Destroy(this.gameObject);
    96.             }
    97.             Instance = this;
    98.  
    99.             m_uiOptions = new List<UIConversationButton>();
    100.         }
    101.  
    102.         private void Start()
    103.         {
    104.             NpcIcon.sprite = BlankSprite;
    105.             DialogueText.text = "";
    106.             TurnOffUI();
    107.         }
    108.  
    109.         private void OnDestroy()
    110.         {
    111.             Instance = null;
    112.         }
    113.  
    114.  
    115.  
    116.  
    117.         //--------------------------------------
    118.         // Update
    119.         //--------------------------------------
    120.  
    121.         private void Update()
    122.         {
    123.             switch (m_state)
    124.             {
    125.                 case eState.TransitioningDialogueBoxOn:
    126.                     {
    127.                         m_stateTime += Time.deltaTime;
    128.                         float t = m_stateTime / TRANSITION_TIME;
    129.  
    130.                         if (t > 1)
    131.                         {
    132.                             DoSpeech(m_pendingDialogue);
    133.                             return;
    134.                         }
    135.  
    136.                         SetColorAlpha(DialogueBackground, t);
    137.                         SetColorAlpha(NpcIcon, t);
    138.                         SetColorAlpha(NameText, t);
    139.                     }
    140.                     break;
    141.  
    142.                 case eState.ScrollingText:
    143.                     UpdateScrollingText();
    144.                     break;
    145.  
    146.                 case eState.TransitioningOptionsOn:
    147.                     {
    148.                         m_stateTime += Time.deltaTime;
    149.                         float t = m_stateTime / TRANSITION_TIME;
    150.  
    151.                         if (t > 1)
    152.                         {
    153.                             SetState(eState.Idle);
    154.                             return;
    155.                         }
    156.  
    157.                         for (int i = 0; i < m_uiOptions.Count; i++)
    158.                             m_uiOptions[i].SetAlpha(t);
    159.                     }
    160.                     break;
    161.  
    162.                 case eState.Idle:
    163.                     {
    164.                         m_stateTime += Time.deltaTime;
    165.  
    166.                         if (m_currentSpeech.AutomaticallyAdvance)
    167.                         {
    168.                             if (m_currentSpeech.Dialogue != null || m_currentSpeech.Options == null || m_currentSpeech.Options.Count == 0)
    169.                             {
    170.                                 if (m_stateTime > m_currentSpeech.TimeUntilAdvance)
    171.                                 {
    172.                                     SetState(eState.TransitioningOptionsOff);
    173.                                 }
    174.                             }
    175.                         }
    176.                     }
    177.                     break;
    178.  
    179.                 case eState.TransitioningOptionsOff:
    180.                     {
    181.                         m_stateTime += Time.deltaTime;
    182.                         float t = m_stateTime / TRANSITION_TIME;
    183.  
    184.                         if (t > 1)
    185.                         {
    186.                             ClearOptions();
    187.  
    188.                             if (m_currentSpeech.AutomaticallyAdvance)
    189.                             {
    190.                                 if (m_currentSpeech.Dialogue != null)
    191.                                 {
    192.                                     DoSpeech(m_currentSpeech.Dialogue);
    193.                                     return;
    194.                                 }
    195.                                 else if (m_currentSpeech.Options == null || m_currentSpeech.Options.Count == 0)
    196.                                 {
    197.                                     EndConversation();
    198.                                     return;
    199.                                 }
    200.                             }
    201.  
    202.                             if (m_selectedOption == null)
    203.                             {
    204.                                 EndConversation();
    205.                                 return;
    206.                             }
    207.  
    208.                             SpeechNode nextAction = m_selectedOption.Dialogue;
    209.                             if (nextAction == null)
    210.                             {
    211.                                 EndConversation();
    212.                             }
    213.                             else
    214.                             {
    215.                                 DoSpeech(nextAction);
    216.                             }
    217.                             return;
    218.                         }
    219.  
    220.  
    221.                         for (int i = 0; i < m_uiOptions.Count; i++)
    222.                             m_uiOptions[i].SetAlpha(1 - t);
    223.  
    224.                         SetColorAlpha(DialogueText, 1 - t);
    225.                     }
    226.                     break;
    227.  
    228.                 case eState.TransitioningDialogueOff:
    229.                     {
    230.                         m_stateTime += Time.deltaTime;
    231.                         float t = m_stateTime / TRANSITION_TIME;
    232.  
    233.                         if (t > 1)
    234.                         {
    235.                             TurnOffUI();
    236.                             return;
    237.                         }
    238.  
    239.                         SetColorAlpha(DialogueBackground, 1 -t);
    240.                         SetColorAlpha(NpcIcon, 1 - t);
    241.                         SetColorAlpha(NameText, 1 - t);
    242.                     }
    243.                     break;
    244.             }
    245.         }
    246.  
    247.         private void UpdateScrollingText()
    248.         {
    249.             const float charactersPerSecond = 1500;
    250.             float timePerChar = (60.0f / charactersPerSecond);
    251.             timePerChar *= ScrollSpeed;
    252.  
    253.             m_elapsedScrollTime += Time.deltaTime;
    254.  
    255.             if (m_elapsedScrollTime > timePerChar)
    256.             {
    257.                 m_elapsedScrollTime = 0f;
    258.  
    259.                 DialogueText.maxVisibleCharacters = m_scrollIndex;
    260.                 m_scrollIndex++;
    261.  
    262.                 // Finished?
    263.                 if (m_scrollIndex >= m_targetScrollTextCount)
    264.                 {
    265.                     SetState(eState.TransitioningOptionsOn);
    266.                 }
    267.             }
    268.         }
    269.  
    270.  
    271.  
    272.  
    273.         //--------------------------------------
    274.         // Set state
    275.         //--------------------------------------
    276.  
    277.         private void SetState(eState newState)
    278.         {
    279.             switch (m_state)
    280.             {
    281.                 case eState.TransitioningOptionsOff:
    282.                     m_selectedOption = null;
    283.                     break;
    284.             }
    285.  
    286.             m_state = newState;
    287.             m_stateTime = 0f;
    288.  
    289.             switch (m_state)
    290.             {
    291.                 case eState.TransitioningDialogueBoxOn:
    292.                     {
    293.                         SetColorAlpha(DialogueBackground, 0);
    294.                         SetColorAlpha(NpcIcon, 0);
    295.                         SetColorAlpha(NameText, 0);
    296.  
    297.                         DialogueText.text = "";
    298.                         NameText.text = m_pendingDialogue.Name;
    299.                         NpcIcon.sprite = m_pendingDialogue.Icon != null ? m_pendingDialogue.Icon : BlankSprite;
    300.                     }
    301.                     break;
    302.  
    303.                 case eState.ScrollingText:
    304.                     {
    305.                         SetColorAlpha(DialogueText, 1);
    306.  
    307.                         if (m_targetScrollTextCount == 0)
    308.                         {
    309.                             SetState(eState.TransitioningOptionsOn);
    310.                             return;
    311.                         }
    312.                     }
    313.                     break;
    314.  
    315.                 case eState.TransitioningOptionsOn:
    316.                     {
    317.                         for (int i = 0; i < m_uiOptions.Count; i++)
    318.                         {
    319.                             m_uiOptions[i].gameObject.SetActive(true);
    320.                         }
    321.                     }
    322.                     break;
    323.             }    
    324.         }
    325.  
    326.  
    327.  
    328.  
    329.         //--------------------------------------
    330.         // Start / End Conversation
    331.         //--------------------------------------
    332.  
    333.         public void StartConversation(NPCConversation conversation)
    334.         {
    335.             m_conversation = conversation.Deserialize();
    336.             if (OnConversationStarted != null)
    337.                 OnConversationStarted.Invoke();
    338.  
    339.             TurnOnUI();
    340.             m_pendingDialogue = m_conversation.Root;
    341.             SetState(eState.TransitioningDialogueBoxOn);
    342.         }
    343.  
    344.         public void EndConversation()
    345.         {
    346.             SetState(eState.TransitioningDialogueOff);
    347.  
    348.             if (OnConversationEnded != null)
    349.                 OnConversationEnded.Invoke();
    350.         }
    351.  
    352.  
    353.  
    354.  
    355.         //--------------------------------------
    356.         // Public functions
    357.         //--------------------------------------
    358.  
    359.         public void SelectNextOption()
    360.         {
    361.             int next = m_currentSelectedIndex + 1;
    362.             if (next > m_uiOptions.Count - 1)
    363.             {
    364.                 next = 0;
    365.             }
    366.             SetSelectedOption(next);
    367.         }
    368.  
    369.         public void SelectPreviousOption()
    370.         {
    371.             int previous = m_currentSelectedIndex - 1;
    372.             if (previous < 0)
    373.             {
    374.                 previous = m_uiOptions.Count - 1;
    375.             }
    376.             SetSelectedOption(previous);
    377.         }
    378.  
    379.         public void PressSelectedOption()
    380.         {
    381.             if (m_state != eState.Idle) { return; }
    382.             if (m_currentSelectedIndex < 0) { return; }
    383.             if (m_currentSelectedIndex >= m_uiOptions.Count) { return; }
    384.             if (m_uiOptions.Count == 0) { return; }
    385.  
    386.             UIConversationButton button = m_uiOptions[m_currentSelectedIndex];
    387.  
    388.             if (button.Speech != null)
    389.                 ConversationManager.Instance.DoSpeech(button.Speech);
    390.             else
    391.                 ConversationManager.Instance.OptionSelected(button.Option);
    392.         }
    393.  
    394.         public void AlertHover(UIConversationButton button)
    395.         {
    396.             for (int i = 0; i < m_uiOptions.Count; i++)
    397.             {
    398.                 if (m_uiOptions[i] == button && m_currentSelectedIndex != i)
    399.                 {
    400.                     SetSelectedOption(i);
    401.                     return;
    402.                 }
    403.             }
    404.  
    405.             if (button == null)
    406.                 UnselectOption();
    407.         }
    408.  
    409.  
    410.  
    411.  
    412.         //--------------------------------------
    413.         // Do Speech
    414.         //--------------------------------------
    415.  
    416.         public void DoSpeech(SpeechNode speech)
    417.         {
    418.             if (speech == null)
    419.             {
    420.                 EndConversation();
    421.                 return;
    422.             }
    423.  
    424.             m_currentSpeech = speech;
    425.  
    426.             // Clear current options
    427.             ClearOptions();
    428.             m_currentSelectedIndex = 0;
    429.  
    430.             // Set sprite
    431.             if (speech.Icon == null)
    432.             {
    433.                 NpcIcon.sprite = BlankSprite;
    434.             }
    435.             else
    436.             {
    437.                 NpcIcon.sprite = speech.Icon;
    438.             }
    439.  
    440.             // Set font
    441.             if (speech.TMPFont != null)
    442.             {
    443.                 DialogueText.font = speech.TMPFont;
    444.             }
    445.             else
    446.             {
    447.                 DialogueText.font = null;
    448.             }
    449.  
    450.             // Set name
    451.             NameText.text = speech.Name;
    452.  
    453.             // Set text
    454.             if (string.IsNullOrEmpty(speech.Text))
    455.             {
    456.                 if (ScrollText)
    457.                 {
    458.                     DialogueText.text = "";
    459.                     m_targetScrollTextCount = 0;
    460.                     DialogueText.maxVisibleCharacters = 0;
    461.                     m_elapsedScrollTime = 0f;
    462.                     m_scrollIndex = 0;
    463.                 }
    464.                 else
    465.                 {
    466.                     DialogueText.text = "";
    467.                     DialogueText.maxVisibleCharacters = 1;
    468.                 }
    469.             }
    470.             else
    471.             {
    472.                 if (ScrollText)
    473.                 {
    474.                     DialogueText.text = speech.Text;
    475.                     m_targetScrollTextCount = speech.Text.Length + 1;
    476.                     DialogueText.maxVisibleCharacters = 0;
    477.                     m_elapsedScrollTime = 0f;
    478.                     m_scrollIndex = 0;
    479.                 }
    480.                 else
    481.                 {
    482.                     DialogueText.text = speech.Text;
    483.                     DialogueText.maxVisibleCharacters = speech.Text.Length;
    484.                 }
    485.             }
    486.  
    487.  
    488.             // Call the event
    489.             if (speech.Event != null)
    490.                 speech.Event.Invoke();
    491.  
    492.             // Play the audio
    493.             if (speech.Audio != null)
    494.             {
    495.                 AudioPlayer.clip = speech.Audio;
    496.                 AudioPlayer.volume = speech.Volume;
    497.                 AudioPlayer.Play();
    498.             }
    499.  
    500.             // Display new options
    501.             if (speech.Options.Count > 0)
    502.             {
    503.                 for (int i = 0; i < speech.Options.Count; i++)
    504.                 {
    505.                     UIConversationButton option = GameObject.Instantiate(ButtonPrefab, OptionsPanel);
    506.                     option.InitButton(speech.Options[i]);
    507.                     option.SetOption(speech.Options[i]);
    508.                     m_uiOptions.Add(option);
    509.                 }
    510.             }
    511.             else
    512.             {
    513.                 // Display "Continue" / "End" if we should.
    514.                 bool notAutoAdvance = !speech.AutomaticallyAdvance;
    515.                 bool autoWithOption = (speech.AutomaticallyAdvance && speech.AutoAdvanceShouldDisplayOption);
    516.                 if (notAutoAdvance || autoWithOption)
    517.                 {
    518.                     // Else display "continue" button to go to following dialogue
    519.                     if (speech.Dialogue != null)
    520.                     {
    521.                         UIConversationButton option = GameObject.Instantiate(ButtonPrefab, OptionsPanel);
    522.                         option.SetFollowingAction(speech.Dialogue);
    523.                         m_uiOptions.Add(option);
    524.                     }
    525.                     // Else display "end" button
    526.                     else
    527.                     {
    528.                         UIConversationButton option = GameObject.Instantiate(ButtonPrefab, OptionsPanel);
    529.                         option.SetAsEndConversation();
    530.                         m_uiOptions.Add(option);
    531.                     }
    532.                 }
    533.             }
    534.             SetSelectedOption(0);
    535.  
    536.             // Set the button sprite and alpha
    537.             for (int i = 0; i < m_uiOptions.Count; i++)
    538.             {
    539.                 m_uiOptions[i].SetImage(OptionImage, OptionImageSliced);
    540.                 m_uiOptions[i].SetAlpha(0);
    541.                 m_uiOptions[i].gameObject.SetActive(false);
    542.             }
    543.  
    544.             SetState(eState.ScrollingText);
    545.         }
    546.  
    547.  
    548.  
    549.         //--------------------------------------
    550.         // Option Selected
    551.         //--------------------------------------
    552.  
    553.         public void OptionSelected(OptionNode option)
    554.         {
    555.             m_selectedOption = option;
    556.             SetState(eState.TransitioningOptionsOff);
    557.         }
    558.  
    559.  
    560.  
    561.  
    562.         //--------------------------------------
    563.         // Util
    564.         //--------------------------------------
    565.  
    566.         private void TurnOnUI()
    567.         {
    568.             DialoguePanel.gameObject.SetActive(true);
    569.             OptionsPanel.gameObject.SetActive(true);
    570.  
    571.             if (BackgroundImage != null)
    572.             {
    573.                 DialogueBackground.sprite = BackgroundImage;
    574.  
    575.                 if (BackgroundImageSliced)
    576.                     DialogueBackground.type = Image.Type.Sliced;
    577.                 else
    578.                     DialogueBackground.type = Image.Type.Simple;
    579.             }
    580.  
    581.             NpcIcon.sprite = BlankSprite;
    582.         }
    583.  
    584.         private void TurnOffUI()
    585.         {
    586.             DialoguePanel.gameObject.SetActive(false);
    587.             OptionsPanel.gameObject.SetActive(false);
    588.             SetState(eState.Off);
    589. #if UNITY_EDITOR
    590.             // Debug.Log("[ConversationManager]: Conversation UI off.");
    591. #endif
    592.         }
    593.  
    594.         private void ClearOptions()
    595.         {
    596.             while (m_uiOptions.Count != 0)
    597.             {
    598.                 GameObject.Destroy(m_uiOptions[0].gameObject);
    599.                 m_uiOptions.RemoveAt(0);
    600.             }
    601.         }
    602.  
    603.         private void SetColorAlpha(MaskableGraphic graphic, float a)
    604.         {
    605.             Color col = graphic.color;
    606.             col.a = a;
    607.             graphic.color = col;
    608.         }
    609.  
    610.         private void SetSelectedOption(int index)
    611.         {
    612.             if (m_uiOptions.Count == 0) { return; }
    613.  
    614.             if (index < 0)
    615.                 index = 0;
    616.             if (index > m_uiOptions.Count - 1)
    617.                 index = m_uiOptions.Count - 1;
    618.  
    619.             if (m_currentSelectedIndex >= 0)
    620.                 m_uiOptions[m_currentSelectedIndex].SetHovering(false);
    621.             m_currentSelectedIndex = index;
    622.             m_uiOptions[index].SetHovering(true);
    623.         }
    624.  
    625.         private void UnselectOption()
    626.         {
    627.             if (m_currentSelectedIndex < 0) { return; }
    628.  
    629.             m_uiOptions[m_currentSelectedIndex].SetHovering(false);
    630.             m_currentSelectedIndex = -1;
    631.         }
    632.     }
    633. }
     
  5. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481