VIDE Dialogues : A free solution for dialogue systems - Supports interactive dialogues

VIDE DIALOGUE is proving to be useful so far, ty for putting this out there, for free too! It's saved me re-inventing the wheel.

So far I haven't fully integrated it into my game. However following the manual you've done and with what Aridez has posted, I thought i'd share what I have so far to help get anyone reading started with this. (it's not the best approach but like I've said i'm still playing with it)

` using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Collections.Generic;

public class DialogueUI : MonoBehaviour { //***************************************************************************

//Desc: vars used to fade the containers visibility in and out. 

private Image _image;
private Color _colour;
private float _alpha;
[Header("BOX CONTAINER VISIBILITY SPD")]
public float _intensity;// = 0.0025f;
private bool _fadeIn;
private bool _setVisibility;

//***************************************************************************

[Header("TEXT CONVERSATION REF")]
public Text _conversation;

[Header("TEXT SPEAKER REF")]
public Text _speakerId;

[Header("TEXT OPTIONS REF")]
public Text _option_1;
public Text _option_2;
public Text _option_3;
public Text _option_4;

private int _opt_i;

//Desc: Use this to store the current player dialogue options.
//private List<Text> _currentOptions = new List<Text>();

//***************************************************************************

[Header("DIALOGUE SINGLETON")]
[System.NonSerialized]
public static DialogueUI _instance; 

private VIDE_Data _dialogue;

[Header("DIALOGUE STARTED?")]
public bool _isLoaded = false;


//***************************************************************************

private string playerId;
private char[] myString = new char[8];

//***************************************************************************

private bool _animatingText;

void Awake()
{
    if(_instance == null)
    {
        _instance = this;
    }
}

void Start () 
{
    _image = this.gameObject.GetComponent<Image>();
    _alpha = 0f;
    _colour = new Color(1f,1f,1f,_alpha);
    _image.color = _colour;
    _fadeIn = true;
    _setVisibility = false;
    _animatingText = false;
    _opt_i = 0;

    //Apply user name for dialogue display.
    for(int z = 0; z < GameManager.userName.Length; z++)
    {
        myString[z] = GameManager.userName[z];
    }
    playerId = new string(myString);

    _dialogue = this.gameObject.GetComponent<VIDE_Data>();
}

// public void PlayerOptions(bool pressDown) // { // if(_dialogue.nodeData.currentIsPlayer) // { // //Color the Player options. Blue for the selected one // for (int i = 0; i < _currentOptions.Count; i++) // { // _currentOptions[i].color = Color.white; // if(i == _dialogue.nodeData.selectedOption) // { // _currentOptions[i].color = Color.yellow; // } // } // // //Scroll through Player dialogue options // if(pressDown) // { // if (_dialogue.nodeData.selectedOption < _currentOptions.Count - 1) // _dialogue.nodeData.selectedOption++; // } // else //pressUp // { // if (_dialogue.nodeData.selectedOption > 0) // _dialogue.nodeData.selectedOption--; // } // } // // }

// public void SetupOptions(string[] opts) // { // //Destroy the current options // foreach (Text op in _currentOptions) // Destroy(op.gameObject); // // //Clean the variable // _currentOptions = new List<Text>(); // // //Create the options // for (int i = 0; i < opts.Length; i++) // { // //This is just one way of creating endless options for Unity's UI class // //Normally, you'd have an absolute number of options and you wouldn't have the need of doing this // GameObject newOp = Instantiate(_conversation.gameObject, _conversation.transform.position, Quaternion.identity) as GameObject; // newOp.SetActive(true); // newOp.transform.SetParent(_conversation.transform.parent, true); // newOp.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 20 - (20 * i)); // newOp.GetComponent<Text>().text = opts[i]; // _currentOptions.Add(newOp.GetComponent<Text>()); // } // }

public void SetupOptions(string[] opts)
{
    //Clear previous NPC text.
    _conversation.text = "";

    switch(opts.Length)
    {
    case 1:

        //Set correct options active.
        if(!_option_1.gameObject.activeSelf)
            _option_1.gameObject.SetActive(true);

        //Set the correct options to inactive.
        _option_2.gameObject.SetActive(false);
        _option_3.gameObject.SetActive(false);
        _option_4.gameObject.SetActive(false);

        //Apply correct strings to text display.
        _option_1.text = opts[0];
        break;
    case 2:

        //Set correct options active.
        if(!_option_1.gameObject.activeSelf)
            _option_1.gameObject.SetActive(true);
        if(!_option_2.gameObject.activeSelf)
            _option_2.gameObject.SetActive(true);

        //Set the correct options to inactive.
        _option_3.gameObject.SetActive(false);
        _option_4.gameObject.SetActive(false);

        //Apply correct strings to text display.
        _option_1.text = opts[0];
        _option_2.text = opts[1];
        break;
    case 3:

        //Set correct options active.
        if(!_option_1.gameObject.activeSelf)
            _option_1.gameObject.SetActive(true);
        if(!_option_2.gameObject.activeSelf)
            _option_2.gameObject.SetActive(true);
        if(!_option_3.gameObject.activeSelf)
            _option_3.gameObject.SetActive(true);

        //Set the correct options to inactive.
        _option_4.gameObject.SetActive(false);

        //Apply correct strings to text display.
        _option_1.text = opts[0];
        _option_2.text = opts[1];
        _option_3.text = opts[2];
        break;
    case 4:

        //Set correct options active.
        if(!_option_1.gameObject.activeSelf)
            _option_1.gameObject.SetActive(true);
        if(!_option_2.gameObject.activeSelf)
            _option_2.gameObject.SetActive(true);
        if(!_option_3.gameObject.activeSelf)
            _option_3.gameObject.SetActive(true);
        if(!_option_4.gameObject.activeSelf)
            _option_4.gameObject.SetActive(true);

        //Set the correct options to inactive.

        //Apply correct strings to text display.
        _option_1.text = opts[0];
        _option_2.text = opts[1];
        _option_3.text = opts[2];
        _option_4.text = opts[3];
        break;
    }



}

public void PlayerOptions(bool pressDown)
{
    if(_dialogue.nodeData.currentIsPlayer) 
    {
        //Scroll through Player dialogue options
        if(pressDown)
        {
            if (_dialogue.nodeData.selectedOption < 3)
                _dialogue.nodeData.selectedOption++;
        }
        else //pressUp
        {
            if (_dialogue.nodeData.selectedOption > 0)
                _dialogue.nodeData.selectedOption--;
        }

        if(_opt_i == _dialogue.nodeData.selectedOption) 
        {

        }
    }

}

public void Begin(VIDE_Assign dialToGet)
{
    _dialogue.BeginDialogue(dialToGet);
    _fadeIn = true;
    _setVisibility = true;
    _isLoaded = true;

    if(!_conversation.gameObject.activeSelf)
        _conversation.gameObject.SetActive(true);
    if(!_speakerId.gameObject.activeSelf)
        _speakerId.gameObject.SetActive(true);

    _speakerId.text = _dialogue.nodeData.tag;

    UpdateUI();
}

public void UpdateUI()
{
    VIDE_Data.NodeData nodeData = _dialogue.nodeData;
    if(nodeData.isEnd) 
    {
        _dialogue.EndDialogue();
        _fadeIn = false;
        _setVisibility = true;
        _isLoaded = false;
        _animatingText = false;
        _conversation.gameObject.SetActive(false);
        _speakerId.gameObject.SetActive(false);
        return;
    }
    if(nodeData.currentIsPlayer) 
    {
        //PLAYER TEXT LOGIC

        SetupOptions(_dialogue.nodeData.playerComments);
        //_conversation.text = nodeData.playerComments[0];
        _speakerId.text = playerId;
    }
    else
    {
        //NPC TEXT LOGIC

        //_conversation.text = nodeData.npcComment[nodeData.npcCommentIndex];
        if(_conversation.text != nodeData.npcComment[nodeData.npcCommentIndex])
        {
            _conversation.text = "";
            StartCoroutine(TypeText()); 
        }
        _speakerId.text = nodeData.tag;

    }

}

public void NextNode()
{
    if(_animatingText) 
    {
        //If animating text is still true, speed it up and do not go to next
        //node until finished.
        _animatingText = false; 
        return;
    }

    AudioManager.RunUISFX(3);
    _dialogue.Next();
    UpdateUI();
}

private void FadeProcess()
{
    //If fading out.
    if(!_fadeIn)
    {
        if(_alpha >= 0.0001f)
            _alpha -= _intensity;
        else
        {
            //Set flag, as desired transpancy has been reached.
            _setVisibility = false;
            return;
        }
    }

    //If fading in.
    if(_fadeIn)
    {
        if(_alpha <= 0.9999f)
            _alpha += _intensity;
        else
        {
            //Set flag, as desired transpancy has been reached.
            _setVisibility = false;
            return;
        }
    }
    _colour = new Color(1f,1f,1f,_alpha);
    _image.color = _colour;
}

//Use StringBuilder for better performance
IEnumerator TypeText() 
{
    _animatingText = true;

    string c = _dialogue.nodeData.npcComment[_dialogue.nodeData.npcCommentIndex];

    if(!_dialogue.nodeData.currentIsPlayer)
    {
        while(_conversation.text != c)
        {
            if(!_animatingText)
            {
                break;
            }

            string letterToAdd = c[_conversation.text.Length].ToString();

            _conversation.text += letterToAdd; //Actual text updates here.
            AudioManager.RunUISFX(8);

            yield return new WaitForSeconds(0.02f);
        }
    }

    _conversation.text = _dialogue.nodeData.npcComment[_dialogue.nodeData.npcCommentIndex]; //Also here.        
    _animatingText = false; 

}

void Update () 
{
    if(_setVisibility)
    {
        FadeProcess();
    }

}

}

` I've used pre-defined text gameobject options as I'll only ever need a total of 4 options, also it's more efficient, despite it being ugly and hard-coded.

/r/Unity3D Thread