AUDIO LOG SYSTEM

TYPE

Prototype


ROLES

C# Scripter

 

YEAR

2020

 

TIME

2 Weeks

 

ENGINE

Unity 2019

I made this prototyp in Unity. The goal was to create a system that would fit in a game based on a previously made narrative design document. I chose to create an audio log system.


The player has an inventory with audio logs, and by walking over a new audio log, it will be added to that inventory.


The player will get a tool tip about the new audio log. If the player were to open their inventory, they would see the added audio log and if they haven’t listened to it, the new audio log will be highlighted.


When playing the audio log, the player will get subtitles and a tool tip about being able to turn it off.


I made the system that plays the audio logs and that keeps track of which has been listened to. I also made the UI that shows subtitles, inventory, and tool tips.


I learned a lot about C# during this course. The system I created helped me during the third project when we were making And Then Jack Woke Up.


using UnityEngine;
using TMPro;

public class AudioLogPickup : MonoBehaviour
{
    public TextAsset audioLog;

    private TextMeshProUGUI audioLogTooltip;
    private ListOfAudioButtons audioLogList;
    private PlayAudioLog playAudioLog;

    private void Start()
    {
        audioLogTooltip = GameObject.Find("AudioLogTooltip").GetComponent();
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            Debug.Log(audioLog.name);

            audioLogList = other.gameObject.GetComponent();
            playAudioLog = other.gameObject.GetComponent();

            if (!playAudioLog.hasAudioLog)
            {
                playAudioLog.hasAudioLog = true;
                GameObject.Find("Warning").SetActive(false);
                playAudioLog.currentAudioLog = audioLog;
            }


            playAudioLog.hasNewAudioLog = true;
            if (!playAudioLog.isReading)
            {
                if (!playAudioLog.hasSeenOpenTip)
                {
                    playAudioLog.hasSeenOpenTip = true;
                    audioLogTooltip.text = "You have a new audio log!\nPress \"P\" to listen";
                }
                else
                {
                    audioLogTooltip.text = "You have a new audio log!";
                }
            }

            playAudioLog.unheardAudioLogs.Insert(0, audioLog);

            audioLogList.nameOfAudioLog = audioLog.name.ToString();
            audioLogList.CreateButton(audioLog);

            Destroy(gameObject);
        }
    }
}
using UnityEngine;
using TMPro;

public class AudioLogPickup : MonoBehaviour
{
    public TextAsset audioLog;

    private TextMeshProUGUI audioLogTooltip;
    private ListOfAudioButtons audioLogList;
    private PlayAudioLog playAudioLog;

    private void Start()
    {
        audioLogTooltip = GameObject.Find("AudioLogTooltip").GetComponent();
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            Debug.Log(audioLog.name);

            audioLogList = other.gameObject.GetComponent();
            playAudioLog = other.gameObject.GetComponent();

            if (!playAudioLog.hasAudioLog)
            {
                playAudioLog.hasAudioLog = true;
                GameObject.Find("Warning").SetActive(false);
                playAudioLog.currentAudioLog = audioLog;
            }


            playAudioLog.hasNewAudioLog = true;
            if (!playAudioLog.isReading)
            {
                if (!playAudioLog.hasSeenOpenTip)
                {
                    playAudioLog.hasSeenOpenTip = true;
                    audioLogTooltip.text = "You have a new audio log!\nPress \"P\" to listen";
                }
                else
                {
                    audioLogTooltip.text = "You have a new audio log!";
                }
            }

            playAudioLog.unheardAudioLogs.Insert(0, audioLog);

            audioLogList.nameOfAudioLog = audioLog.name.ToString();
            audioLogList.CreateButton(audioLog);

            Destroy(gameObject);
        }
    }
}
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;

public class ListOfAudioButtons : MonoBehaviour
{
    public GameObject buttonPrefab;

    private CanvasGroup listCanvasGroup;
    private GameObject contentLayoutGroup;

    private List buttonObjects = new List();

    [System.NonSerialized] public string nameOfAudioLog;

    [System.NonSerialized] public bool listIsOpen;
    [System.NonSerialized] public GameObject lastSelect;

    [System.NonSerialized] public TextMeshProUGUI inventoryTooltip;
    private PlayAudioLog playAudioLog;


    private void Start()
    {
        listCanvasGroup = GameObject.Find("ScrollableListOfLogs").GetComponent();
        contentLayoutGroup = GameObject.Find("Content");
        inventoryTooltip = GameObject.Find("AudioLogTooltipInventory").GetComponent();

        playAudioLog = gameObject.GetComponent();

        Hide();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.O))
        {
            if (listIsOpen)
            {
                Hide();
            }
            else
            {
                Show();
            }
        }

        if (EventSystem.current.currentSelectedGameObject == null)
        {
            EventSystem.current.SetSelectedGameObject(lastSelect);
        }
        else
        {
            lastSelect = EventSystem.current.currentSelectedGameObject;
        }
    }

    public void CreateButton(TextAsset text)
    {
        GameObject newButtonObject = Instantiate(buttonPrefab) as GameObject;
        buttonObjects.Insert(0, newButtonObject);
        lastSelect = newButtonObject;

        newButtonObject.transform.SetParent(contentLayoutGroup.transform, false);
        newButtonObject.transform.SetAsFirstSibling();
        newButtonObject.GetComponentInChildren().text = nameOfAudioLog;

        newButtonObject.AddComponent().savedAudioLog = text;

        Button newButton = newButtonObject.GetComponent();

        lastSelect = newButtonObject;
        newButton.OnSelect(null);

        playAudioLog.unheardAudioLogsButtons.Insert(0, newButton);

        newButton.name = nameOfAudioLog + "Button";
    }

    private void Hide()
    {
        listCanvasGroup.alpha = 0f;
        listCanvasGroup.blocksRaycasts = false;
        listCanvasGroup.interactable = false;

        listIsOpen = false;
        inventoryTooltip.text = null;

        gameObject.GetComponent().canMove = true;
    }

    private void Show()
    {
        listCanvasGroup.interactable = true;

        if (buttonObjects.Count != 0)
        {
            EventSystem.current.SetSelectedGameObject(buttonObjects[0]);
        }

        listCanvasGroup.blocksRaycasts = true;
        listCanvasGroup.alpha = 1f;
        listIsOpen = true;

        if (!playAudioLog.hasSeenInvTip)
        {
            inventoryTooltip.text = "Press \"O\" to close";
            playAudioLog.hasSeenInvTip = true;
        }

        gameObject.GetComponent().canMove = false;
    }
}
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;

public class ListOfAudioButtons : MonoBehaviour
{
    public GameObject buttonPrefab;

    private CanvasGroup listCanvasGroup;
    private GameObject contentLayoutGroup;

    private List buttonObjects = new List();

    [System.NonSerialized] public string nameOfAudioLog;

    [System.NonSerialized] public bool listIsOpen;
    [System.NonSerialized] public GameObject lastSelect;

    [System.NonSerialized] public TextMeshProUGUI inventoryTooltip;
    private PlayAudioLog playAudioLog;


    private void Start()
    {
        listCanvasGroup = GameObject.Find("ScrollableListOfLogs").GetComponent();
        contentLayoutGroup = GameObject.Find("Content");
        inventoryTooltip = GameObject.Find("AudioLogTooltipInventory").GetComponent();

        playAudioLog = gameObject.GetComponent();

        Hide();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.O))
        {
            if (listIsOpen)
            {
                Hide();
            }
            else
            {
                Show();
            }
        }

        if (EventSystem.current.currentSelectedGameObject == null)
        {
            EventSystem.current.SetSelectedGameObject(lastSelect);
        }
        else
        {
            lastSelect = EventSystem.current.currentSelectedGameObject;
        }
    }

    public void CreateButton(TextAsset text)
    {
        GameObject newButtonObject = Instantiate(buttonPrefab) as GameObject;
        buttonObjects.Insert(0, newButtonObject);
        lastSelect = newButtonObject;

        newButtonObject.transform.SetParent(contentLayoutGroup.transform, false);
        newButtonObject.transform.SetAsFirstSibling();
        newButtonObject.GetComponentInChildren().text = nameOfAudioLog;

        newButtonObject.AddComponent().savedAudioLog = text;

        Button newButton = newButtonObject.GetComponent();

        lastSelect = newButtonObject;
        newButton.OnSelect(null);

        playAudioLog.unheardAudioLogsButtons.Insert(0, newButton);

        newButton.name = nameOfAudioLog + "Button";
    }

    private void Hide()
    {
        listCanvasGroup.alpha = 0f;
        listCanvasGroup.blocksRaycasts = false;
        listCanvasGroup.interactable = false;

        listIsOpen = false;
        inventoryTooltip.text = null;

        gameObject.GetComponent().canMove = true;
    }

    private void Show()
    {
        listCanvasGroup.interactable = true;

        if (buttonObjects.Count != 0)
        {
            EventSystem.current.SetSelectedGameObject(buttonObjects[0]);
        }

        listCanvasGroup.blocksRaycasts = true;
        listCanvasGroup.alpha = 1f;
        listIsOpen = true;

        if (!playAudioLog.hasSeenInvTip)
        {
            inventoryTooltip.text = "Press \"O\" to close";
            playAudioLog.hasSeenInvTip = true;
        }

        gameObject.GetComponent().canMove = false;
    }
}
using System.Collections;
using UnityEngine;
using TMPro;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class PlayAudioLog : MonoBehaviour
{
    [System.NonSerialized] public TextAsset currentAudioLog;
    [System.NonSerialized] public List unheardAudioLogs = new List();
    [System.NonSerialized] public List unheardAudioLogsButtons = new List();
    [System.NonSerialized] public Button thisAudioLogButton;

    [System.NonSerialized] public bool isReading;
    [System.NonSerialized] public bool isReadingSaved;
    [System.NonSerialized] public bool hasAudioLog;
    [System.NonSerialized] public bool hasNewAudioLog;
    [System.NonSerialized] public bool hasSeenOpenTip;
    [System.NonSerialized] public bool hasSeenInvTip;
    [System.NonSerialized] public bool stopIteration;
    [System.NonSerialized] public bool continueLoop;
    [System.NonSerialized] public bool pButtonPressed;
    [System.NonSerialized] public bool hasSeenCloseTip;
    [System.NonSerialized] public bool hasSeenBlinkTip;

    [System.NonSerialized] public float timer;

    [System.NonSerialized] public int indexOfUnheard;
    [System.NonSerialized] public string[] audioSentences;

    private TextMeshProUGUI audioLogText;
    private TextMeshProUGUI audioLogTitle;
    private TextMeshProUGUI audioLogTooltip;
    private ListOfAudioButtons audioLogList;

    private void Start()
    {
        isReading = false;
        isReadingSaved = false;
        indexOfUnheard = 0;
        hasNewAudioLog = false;
        hasAudioLog = false;
        hasSeenInvTip = false;
        hasSeenCloseTip = false;
        stopIteration = false;
        pButtonPressed = false;
        hasSeenOpenTip = false;
        hasSeenBlinkTip = false;

        audioLogText = GameObject.Find("AudioLogText").GetComponent();
        audioLogTitle = GameObject.Find("AudioLogTitle").GetComponent();
        audioLogTooltip = GameObject.Find("AudioLogTooltip").GetComponent();
        audioLogList = gameObject.GetComponent();
    }

    private void Update()
    {
        ListenToAudioLog();

    }

    private void ListenToAudioLog()
    {
        if (Input.GetKeyDown(KeyCode.Return) && hasAudioLog)
        {
            pButtonPressed = !pButtonPressed;
            if ( !pButtonPressed && (isReading || isReadingSaved))
            {
                continueLoop = true;
                stopIteration = true;
                StopCoroutine("ShowForSeconds");
            }
        }

        if (Input.GetKeyDown(KeyCode.P) && hasAudioLog)
        {
            pButtonPressed = !pButtonPressed;

            if (pButtonPressed)
            {
                if (!audioLogList.listIsOpen)
                {
                    isReading = true;

                    CheckForUnheardLogs();
                    StartCoroutine("Play");
                }
                else
                {
                    EventSystem.current.currentSelectedGameObject.GetComponent().ClickedSaved();
                }
            }
            else
            {
                if (isReading || isReadingSaved)
                {
                    continueLoop = true;
                    stopIteration = true;
                    StopCoroutine("ShowForSeconds");
                }

            }
        }
    }

    public void PlaySaved()
    {
        isReading = true;
        isReadingSaved = true;

        CheckForUnheardLogs();
        StartCoroutine("Play");

        isReadingSaved = false;
    }

    public void CheckForUnheardLogs()
    {
        if (unheardAudioLogs.Count != 0)
        {
            if (!isReadingSaved)
            {
                currentAudioLog = unheardAudioLogs[0];
            }

            if (unheardAudioLogs.Contains(currentAudioLog))
            {
                indexOfUnheard = unheardAudioLogs.IndexOf(currentAudioLog);
                unheardAudioLogs.RemoveAt(indexOfUnheard);
                thisAudioLogButton = unheardAudioLogsButtons[indexOfUnheard];
                unheardAudioLogsButtons.RemoveAt(indexOfUnheard);

                ChangeColorOfButton();
            }
        }

        if(unheardAudioLogs.Count == 0)
        {
            hasNewAudioLog = false;
        }
    }

    public void ChangeColorOfButton()
    {
        thisAudioLogButton.GetComponentInChildren().color = new Color(1, 1, 1, 1);
    }

    public IEnumerator Play()
    {
        audioSentences = currentAudioLog.text.Split('\n');

        while (isReading)
        {
            if (!hasSeenCloseTip)
            {
                audioLogTooltip.text = "Press \"P\" to stop audio log";
                hasSeenCloseTip = true;
            }
            else
            {
                audioLogTooltip.text = null;
            }

            audioLogTitle.text = currentAudioLog.name + ":";

            for (int i = 0; i < audioSentences.Length; i++)
            {
                audioLogText.text = audioSentences[i];
                continueLoop = false;

                if (stopIteration)
                {
                    break;
                }

                StartCoroutine("ShowForSeconds");

                while (!continueLoop)
                {
                    yield return null;
                }
            }
            isReading = false;

        }
        HasPlayed();
        StopCoroutine("Play");
    }

    public IEnumerator ShowForSeconds()
    {
        timer = 3f;
        float totalTime = 0;

        while (totalTime <= timer)
        {
            totalTime += Time.deltaTime;

            yield return null;
        }
        continueLoop = true;
    }

    private void HasPlayed()
    {
        if (currentAudioLog.name == "AudioLog2 - Special Abilities" && !hasSeenBlinkTip)
        {
            audioLogText.text = "Hold \"Shift\" to aim blink\nRelease to use";
            hasSeenBlinkTip = true;
        }
        else
        {
            audioLogText.text = null;
        }

        audioLogTitle.text = null;
        stopIteration = false;
        pButtonPressed = false;

        if (hasNewAudioLog)
        {
            audioLogTooltip.text = "You have a new audio log!";
        }
        else
        {
            audioLogTooltip.text = null;
        }

        if (!hasSeenInvTip)
        {
            if (!audioLogList.listIsOpen)
            {
                audioLogList.inventoryTooltip.text = "Press \"O\" to see your saved audio logs";
            }
            else
            {
                hasSeenInvTip = true;
            }
        }
    }
}
using System.Collections;
using UnityEngine;
using TMPro;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class PlayAudioLog : MonoBehaviour
{
    [System.NonSerialized] public TextAsset currentAudioLog;
    [System.NonSerialized] public List unheardAudioLogs = new List();
    [System.NonSerialized] public List unheardAudioLogsButtons = new List();
    [System.NonSerialized] public Button thisAudioLogButton;

    [System.NonSerialized] public bool isReading;
    [System.NonSerialized] public bool isReadingSaved;
    [System.NonSerialized] public bool hasAudioLog;
    [System.NonSerialized] public bool hasNewAudioLog;
    [System.NonSerialized] public bool hasSeenOpenTip;
    [System.NonSerialized] public bool hasSeenInvTip;
    [System.NonSerialized] public bool stopIteration;
    [System.NonSerialized] public bool continueLoop;
    [System.NonSerialized] public bool pButtonPressed;
    [System.NonSerialized] public bool hasSeenCloseTip;
    [System.NonSerialized] public bool hasSeenBlinkTip;

    [System.NonSerialized] public float timer;

    [System.NonSerialized] public int indexOfUnheard;
    [System.NonSerialized] public string[] audioSentences;

    private TextMeshProUGUI audioLogText;
    private TextMeshProUGUI audioLogTitle;
    private TextMeshProUGUI audioLogTooltip;
    private ListOfAudioButtons audioLogList;

    private void Start()
    {
        isReading = false;
        isReadingSaved = false;
        indexOfUnheard = 0;
        hasNewAudioLog = false;
        hasAudioLog = false;
        hasSeenInvTip = false;
        hasSeenCloseTip = false;
        stopIteration = false;
        pButtonPressed = false;
        hasSeenOpenTip = false;
        hasSeenBlinkTip = false;

        audioLogText = GameObject.Find("AudioLogText").GetComponent();
        audioLogTitle = GameObject.Find("AudioLogTitle").GetComponent();
        audioLogTooltip = GameObject.Find("AudioLogTooltip").GetComponent();
        audioLogList = gameObject.GetComponent();
    }

    private void Update()
    {
        ListenToAudioLog();

    }

    private void ListenToAudioLog()
    {
        if (Input.GetKeyDown(KeyCode.Return) && hasAudioLog)
        {
            pButtonPressed = !pButtonPressed;
            if ( !pButtonPressed && (isReading || isReadingSaved))
            {
                continueLoop = true;
                stopIteration = true;
                StopCoroutine("ShowForSeconds");
            }
        }

        if (Input.GetKeyDown(KeyCode.P) && hasAudioLog)
        {
            pButtonPressed = !pButtonPressed;

            if (pButtonPressed)
            {
                if (!audioLogList.listIsOpen)
                {
                    isReading = true;

                    CheckForUnheardLogs();
                    StartCoroutine("Play");
                }
                else
                {
                    EventSystem.current.currentSelectedGameObject.GetComponent().ClickedSaved();
                }
            }
            else
            {
                if (isReading || isReadingSaved)
                {
                    continueLoop = true;
                    stopIteration = true;
                    StopCoroutine("ShowForSeconds");
                }

            }
        }
    }

    public void PlaySaved()
    {
        isReading = true;
        isReadingSaved = true;

        CheckForUnheardLogs();
        StartCoroutine("Play");

        isReadingSaved = false;
    }

    public void CheckForUnheardLogs()
    {
        if (unheardAudioLogs.Count != 0)
        {
            if (!isReadingSaved)
            {
                currentAudioLog = unheardAudioLogs[0];
            }

            if (unheardAudioLogs.Contains(currentAudioLog))
            {
                indexOfUnheard = unheardAudioLogs.IndexOf(currentAudioLog);
                unheardAudioLogs.RemoveAt(indexOfUnheard);
                thisAudioLogButton = unheardAudioLogsButtons[indexOfUnheard];
                unheardAudioLogsButtons.RemoveAt(indexOfUnheard);

                ChangeColorOfButton();
            }
        }

        if(unheardAudioLogs.Count == 0)
        {
            hasNewAudioLog = false;
        }
    }

    public void ChangeColorOfButton()
    {
        thisAudioLogButton.GetComponentInChildren().color = new Color(1, 1, 1, 1);
    }

    public IEnumerator Play()
    {
        audioSentences = currentAudioLog.text.Split('\n');

        while (isReading)
        {
            if (!hasSeenCloseTip)
            {
                audioLogTooltip.text = "Press \"P\" to stop audio log";
                hasSeenCloseTip = true;
            }
            else
            {
                audioLogTooltip.text = null;
            }

            audioLogTitle.text = currentAudioLog.name + ":";

            for (int i = 0; i < audioSentences.Length; i++)
            {
                audioLogText.text = audioSentences[i];
                continueLoop = false;

                if (stopIteration)
                {
                    break;
                }

                StartCoroutine("ShowForSeconds");

                while (!continueLoop)
                {
                    yield return null;
                }
            }
            isReading = false;

        }
        HasPlayed();
        StopCoroutine("Play");
    }

    public IEnumerator ShowForSeconds()
    {
        timer = 3f;
        float totalTime = 0;

        while (totalTime <= timer)
        {
            totalTime += Time.deltaTime;

            yield return null;
        }
        continueLoop = true;
    }

    private void HasPlayed()
    {
        if (currentAudioLog.name == "AudioLog2 - Special Abilities" && !hasSeenBlinkTip)
        {
            audioLogText.text = "Hold \"Shift\" to aim blink\nRelease to use";
            hasSeenBlinkTip = true;
        }
        else
        {
            audioLogText.text = null;
        }

        audioLogTitle.text = null;
        stopIteration = false;
        pButtonPressed = false;

        if (hasNewAudioLog)
        {
            audioLogTooltip.text = "You have a new audio log!";
        }
        else
        {
            audioLogTooltip.text = null;
        }

        if (!hasSeenInvTip)
        {
            if (!audioLogList.listIsOpen)
            {
                audioLogList.inventoryTooltip.text = "Press \"O\" to see your saved audio logs";
            }
            else
            {
                hasSeenInvTip = true;
            }
        }
    }
}