Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Resolved How to slowly increase a button's alpha?

Discussion in 'Getting Started' started by Gummi_Ghost, May 30, 2022.

  1. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    I am trying to make a button slowly appear after the coroutine, TypeLine, has finished. Am I better off coding, or creating an animation to do this? Here is the code I have so far:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class IntroLines : MonoBehaviour
    8. {
    9.    public TextMeshProUGUI textComponent;
    10.    public string[] BeginningLines;
    11.    public float textSpeed;
    12.    private int index;
    13.    public Button IntroButton1;
    14.  
    15.    void Start()
    16.    {    
    17.       Button btn1 = IntroButton1.GetComponent<Button>();
    18.       btn1.onClick.AddListener(DisplayNextLine);        
    19.       textComponent.text = string.Empty;
    20.       startIntroLines();
    21.    }
    22.  
    23.    void DisplayNextLine()
    24.    {
    25.       if(textComponent.text == BeginningLines[index])
    26.          {
    27.             NextIntroLine();
    28.          }
    29.          else
    30.          {
    31.             StopAllCoroutines();
    32.             textComponent.text = BeginningLines[index];
    33.          }
    34.    }
    35.  
    36.  
    37.    void startIntroLines()
    38.    {
    39.       index = 0;
    40.       StartCoroutine(TypeLine());
    41.  
    42.      
    43.    }
    44.  
    45.    IEnumerator TypeLine()
    46.    {
    47.       foreach (char c in BeginningLines[index].ToCharArray())
    48.       {
    49.          textComponent.text += c;
    50.          yield return new WaitForSeconds (textSpeed);
    51.       }    
    52.    }
    53.  
    54.    void NextIntroLine()
    55.    {
    56.       if (index < BeginningLines.Length - 1)
    57.       {
    58.          index++;
    59.          textComponent.text = string.Empty;
    60.          StartCoroutine(TypeLine());
    61.       }
    62.       else
    63.       {
    64.          //gameObject.SetActive(false);
    65.       }
    66.    }
    67.  
    68. }
    69.  
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    You have this post tagged as "Resolved." If you solved it yourself, it'd be cool to share your solution for other people who might be looking to do the same thing as you. If you haven't got a solution yet, you might want to remove that tag on the post so people don't pass on by, thinking you no longer need help.
     
    Vryken and JoeStrout like this.