Untitled
unknown
plain_text
a year ago
5.3 kB
14
Indexable
using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; public class TextAnim : MonoBehaviour { [SerializeField] private TextMeshProUGUI _textMeshPro; [SerializeField] private TextMeshProUGUI[] optionTextMeshes; public List<DialogueSet> dialogueSets = new List<DialogueSet>(); private int currentSetIndex = 0; [SerializeField] float timeBtwnChars; [SerializeField] float timeBtwnWords; public Movement movement; public GameObject dialougeGO; public bool CanMove; [SerializeField] Camera mainCamera; void Start() { if (_textMeshPro == null) { Debug.LogError("TextMeshProUGUI component is not assigned."); } else { Debug.Log("TextMeshProUGUI component assigned."); } if (mainCamera == null) { mainCamera = Camera.main; // Automatically find the main camera if not set if (mainCamera == null) { Debug.LogError("Main camera is not assigned and could not be found automatically."); } else { Debug.Log("Main camera found automatically."); } } if (dialougeGO == null) { Debug.LogError("Dialogue GameObject is not assigned."); } if (optionTextMeshes.Length == 0) { Debug.LogError("Option TextMeshProUGUI components are not assigned."); } foreach (var option in optionTextMeshes) { if (option == null) { Debug.LogError("One or more Option TextMeshProUGUI components are not assigned."); } } } public void Option1() { HandleOptionSelection(0); } public void Option2() { HandleOptionSelection(1); } public void Option3() { HandleOptionSelection(2); } private void HandleOptionSelection(int optionIndex) { if (dialogueSets.Count > currentSetIndex && dialogueSets[currentSetIndex].options.Count > optionIndex) { int nextSetIndex = dialogueSets[currentSetIndex].options[optionIndex].nextSetIndex; if (nextSetIndex == -1) { EndConversation(); } else { currentSetIndex = nextSetIndex; DisplayCurrentSet(); } } } public void DisplayCurrentSet() { if (movement != null) { movement.CanMove = false; } Cursor.lockState = CursorLockMode.None; if (dialogueSets.Count > currentSetIndex) { DisplayText(dialogueSets[currentSetIndex].text); for (int i = 0; i < optionTextMeshes.Length; i++) { if (i < dialogueSets[currentSetIndex].options.Count) { optionTextMeshes[i].text = dialogueSets[currentSetIndex].options[i].text; optionTextMeshes[i].gameObject.SetActive(true); } else { optionTextMeshes[i].gameObject.SetActive(false); } } if (mainCamera != null) { mainCamera.transform.LookAt(transform); } } } private void DisplayText(string text) { if (_textMeshPro != null) { _textMeshPro.text = text; StartCoroutine(TextVisible()); } else { Debug.LogError("TextMeshProUGUI component is not assigned."); } } private IEnumerator TextVisible() { if (_textMeshPro == null) { Debug.LogError("TextMeshProUGUI component is not assigned."); yield break; } _textMeshPro.ForceMeshUpdate(); int totalVisibleCharacters = _textMeshPro.textInfo.characterCount; if (totalVisibleCharacters == 0) { Debug.LogError("No characters found in TextMeshProUGUI."); yield break; } int counter = 0; while (true) { int visibleCount = counter % (totalVisibleCharacters + 1); _textMeshPro.maxVisibleCharacters = visibleCount; if (visibleCount >= totalVisibleCharacters) { break; } counter += 1; yield return new WaitForSeconds(timeBtwnChars); } } private void EndConversation() { if (dialougeGO != null) { dialougeGO.SetActive(false); } if (movement != null) { movement.CanMove = true; } Cursor.lockState = CursorLockMode.Locked; } } [System.Serializable] public class DialogueSet { public string text; public List<DialogueOption> options = new List<DialogueOption>(); } [System.Serializable] public class DialogueOption { public string text; public int nextSetIndex; }
Editor is loading...
Leave a Comment