Search Unity

How can I switch the TextMesh objects texts to new text or to the default text ?

Discussion in 'Scripting' started by DubiDuboni, Sep 3, 2019.

  1. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    How can I switch the TextMesh objects texts to new text or to the default text depending on what button clicked?

    This is script is attached to one empty gameobject :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEngine;
    6.  
    7. public class SwitchMenuText : MonoBehaviour
    8. {
    9.    public List<GameObject> objectsTexts;
    10.    public List<GameObject> defaultObjectsTexts;
    11.  
    12.    private List<GameObject> objectsWithText;
    13.  
    14.    public void Init()
    15.    {
    16.        objectsWithText = GameObject.FindGameObjectsWithTag("ObjectToAddText").ToList();
    17.    }
    18.  
    19.    private void Update()
    20.    {
    21.        if (Rotate.desiredAngle == true)
    22.        {
    23.            Rotate.desiredAngle = false;
    24.  
    25.            if (objectsWithText.Count > 0 && objectsTexts.Count > 0)
    26.            {
    27.                StartCoroutine(UpdateDisplayPhrase(objectsWithText[0], objectsTexts[0]));
    28.                objectsWithText.RemoveAt(0);
    29.                objectsTexts.RemoveAt(0);
    30.            }
    31.        }
    32.    }
    33.  
    34.    private IEnumerator UpdateDisplayPhrase(GameObject objectwithtext, GameObject objecttext)
    35.    {
    36.        var t = objectwithtext.GetComponent<TextMesh>();
    37.        var text = objecttext.name;
    38.  
    39.        yield return StartCoroutine(FadeTextTo(0, 0.6f, t));
    40.  
    41.        t.text = text;
    42.  
    43.        yield return StartCoroutine(FadeTextTo(1, 0.6f, t));
    44.    }
    45.  
    46.    public IEnumerator FadeTextTo(float targetAlpha, float maxDuration, TextMesh textMesh)
    47.    {
    48.        // more efficient to get both colors beforehand
    49.        var fromColor = textMesh.color;
    50.        var toColor = new Color(fromColor.r, fromColor.g, fromColor.b, targetAlpha);
    51.  
    52.        var actualDuration = maxDuration * Mathf.Abs(fromColor.a - toColor.a);
    53.  
    54.        var passedTime = 0f;
    55.        while (passedTime < actualDuration)
    56.        {
    57.            var lerpFactor = passedTime / actualDuration;
    58.  
    59.            textMesh.color = Color.Lerp(fromColor, toColor, lerpFactor);
    60.  
    61.            passedTime += Mathf.Min(Time.deltaTime, actualDuration - passedTime);
    62.            yield return null;
    63.        }
    64.  
    65.        // just to be sure in the end always set it once
    66.        textMesh.color = toColor;
    67.    }
    68. }
    69.  
    And this script is attached to each gameobject I want to click on to make the objects to rotate :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Rotate : MonoBehaviour
    7. {
    8.    public GameObject[] objectsToRotate;
    9.    public float duration = 5f;
    10.    public static bool desiredAngle = false;
    11.  
    12.    private Vector3 lastFwd;
    13.    private int rotcount = 0;
    14.  
    15.    private void OnMouseDown()
    16.    {
    17.        if (rotcount == 0)
    18.        {
    19.            rotcount = 1;
    20.            StartRotations();
    21.        }
    22.    }
    23.  
    24.    public void StartRotations()
    25.    {
    26.        StartCoroutine(StartRotationOfObjects());
    27.    }
    28.  
    29.    private IEnumerator StartRotationOfObjects()
    30.    {
    31.        for (int i = 0; i < objectsToRotate.Length; i++)
    32.        {
    33.            // Random wait period before rotation starts
    34.            if (i == 0)
    35.            {
    36.                yield return new WaitForSeconds(0);
    37.            }
    38.            else
    39.            {
    40.                yield return new WaitForSeconds(Random.Range(0, 2f));
    41.            }
    42.  
    43.            StartCoroutine(Rotates(objectsToRotate[i].transform, duration));
    44.        }
    45.    }
    46.  
    47.    private IEnumerator Rotates(Transform objectToRotate, float duration)
    48.    {
    49.        Quaternion startRot = objectToRotate.rotation;
    50.        float t = 0.0f;
    51.        lastFwd = objectToRotate.transform.forward;
    52.  
    53.        rotcount++;
    54.  
    55.        while (t < duration)
    56.        {
    57.            t += Time.deltaTime;
    58.  
    59.            objectToRotate.rotation = startRot * Quaternion.AngleAxis(t / duration * 360f, Vector3.up);
    60.  
    61.            var curFwd = objectToRotate.transform.forward;
    62.            // measure the angle rotated since last frame:
    63.            var ang = Vector3.Angle(curFwd, lastFwd);
    64.  
    65.            if (myApproximation(ang, 179f, 1f) == true)
    66.            {
    67.                desiredAngle = true;
    68.            }
    69.  
    70.            yield return null;
    71.        }
    72.        objectToRotate.rotation = startRot;
    73.  
    74.        if (rotcount == 4)
    75.            rotcount = 0;
    76.    }
    77.  
    78.    private bool myApproximation(float a, float b, float tolerance)
    79.    {
    80.        return (Mathf.Abs(a - b) < tolerance);
    81.    }
    82. }
    83.  
    The idea is when I click on the Options gameobject(Cube) it will start rotating the 3 objects : New Game , Options , Quit and while rotating to switch the texts on this objects TextMesh text.

    After switched I want that if I click on the Back gameobject (Back is the same Quit gameobject just the text has changed to Back) to switch back to the default texts : New Game , Options , Quit

    This is a screenshot when running the game :



    when I click on the Options and after the 3 objectes rotated :



    Now the text on the objects is Video , Audio , Back Now I want to make that when I click on the Back object (Back is the same Quit object just the text changed) it will change back to the default text New Game , Options , Quit

    The object Quit have attached also the Rotate script like the Options :



    I just can't figure out how to make that when clicking on the Quit button but when the text on it is Back it will switch the text back to the original text in the SwitchMenuText script it's the List defaultObjectsTexts

    It should rotate and then while rotating again switching the text to the names of the objects in the List defaultObjectsTexts
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    It might be easier to actually put two sets of three TextMeshPro objects in your scene, all parented to the same "rotating" pivot, then just turn each set on or off as your mode changes from the top menu to the options menu and back.

    Something like this:

    Screen Shot 2019-09-03 at 5.57.27 PM.png

    You would then turn on and off TopMenu and OptionsMenu at the right spots in your rotation, as in when the object is obscured by being faced away momentarily.
     
  3. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    It is very easy to change TextMeshPro text via script

    First use the using tag
    using TMPro;


    Then declare a public TextMeshProUGUI (I think that's right) variable
    public TextMeshProUGUI optionsTMP;


    Obviously assign it in the inspector

    Then, when you want to change the text,
    optionsTMP.text = "Back";