Untitled
unknown
csharp
a month ago
7.4 kB
0
Indexable
Never
using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; public class TextAnim : MonoBehaviour { [SerializeField] private TextMeshProUGUI TextMPro; [SerializeField] private TextMeshProUGUI personName; [SerializeField] private TextMeshProUGUI[] optionTextMeshes; [SerializeField] private Animator animator; // Reference to the Animator component public List<DialogueSet> dialogueSets = new List<DialogueSet>(); private int currentSetIndex = 0; [SerializeField] float timeBtwnChars; [SerializeField] float timeBtwnWords; public Movement movement; public GameObject dialogueGO; public bool CanMove; [SerializeField] Camera mainCamera; [SerializeField] float rotationDuration = 1.0f; // Duration for the smooth rotation void Update() { if (TextMPro == null) { Debug.LogError("TextMeshProUGUI component is not assigned."); } if (mainCamera == null) { mainCamera = Camera.main; if (mainCamera == null) { Debug.LogError("Main camera is not assigned and could not be found automatically."); } } if (dialogueGO == 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."); } } if (animator == null) { Debug.LogError("Animator component is 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; StartCoroutine(DisplayCurrentSetAfterAnimation()); } } } private IEnumerator DisplayCurrentSetAfterAnimation() { string animationName = dialogueSets[currentSetIndex].animationName; if (!string.IsNullOrEmpty(animationName)) { PlayAnimation(animationName); yield return new WaitForSeconds(GetAnimationClipLength(animationName)); } DisplayCurrentSet(); } private void PlayAnimation(string animationName) { if (animator != null) { animator.Play(animationName); } else { Debug.LogError("Animator component is not assigned."); } } private float GetAnimationClipLength(string animationName) { if (animator != null) { AnimationClip[] clips = animator.runtimeAnimatorController.animationClips; foreach (AnimationClip clip in clips) { if (clip.name == animationName) { return clip.length; } } } return 0f; } public void DisplayCurrentSet() { if (movement != null) { movement.CanMove = false; } Cursor.lockState = CursorLockMode.None; personName.text = gameObject.name; 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) { StartCoroutine(SmoothRotateCamera(mainCamera.transform, transform.position, rotationDuration)); } } } private void DisplayText(string text) { if (TextMPro != null) { TextMPro.text = text; StartCoroutine(TextVisible()); } else { Debug.LogError("TextMeshProUGUI component is not assigned."); } } private IEnumerator TextVisible() { if (TextMPro == null) { Debug.LogError("TextMeshProUGUI component is not assigned."); yield break; } yield return new WaitForEndOfFrame(); TextMPro.ForceMeshUpdate(); yield return new WaitForEndOfFrame(); int totalVisibleCharacters = TextMPro.textInfo.characterCount; if (totalVisibleCharacters == 0) { Debug.LogError("No characters found in TextMeshProUGUI."); yield break; } int counter = 0; while (true) { int visibleCount = counter % (totalVisibleCharacters + 1); TextMPro.maxVisibleCharacters = visibleCount; if (visibleCount >= totalVisibleCharacters) { break; } counter += 1; yield return new WaitForSeconds(timeBtwnChars); } } private IEnumerator SmoothRotateCamera(Transform cameraTransform, Vector3 targetPosition, float duration) { Quaternion startRotation = cameraTransform.rotation; Quaternion endRotation = Quaternion.LookRotation(targetPosition - cameraTransform.position); float elapsedTime = 0f; while (elapsedTime < duration) { cameraTransform.rotation = Quaternion.Slerp(startRotation, endRotation, elapsedTime / duration); elapsedTime += Time.deltaTime; yield return null; } cameraTransform.rotation = endRotation; } private void EndConversation() { if (dialogueGO != null) { dialogueGO.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>(); public string animationName; // Add this line } [System.Serializable] public class DialogueOption { public string text; public int nextSetIndex; }
Leave a Comment