7

Is there something like Toast message in Unity one that is similar to android, other than GUI.In android it was easy with one line code.

public void buttonclick()
{
// Message to show
}
zyonneo
  • 1,319
  • 5
  • 25
  • 63
  • Well... Unity is not Android... Search the Unity API for something that suits you or write a toast Method yourself. Maybe this can help: https://answers.unity.com/questions/594755/how-to-make-a-message-appear-on-screen.html – Jan Oct 01 '18 at 12:10
  • It'd be helpful to have a screenshot of what you want it to look like, for those people who are not familiar with Android toast messages. – Draco18s no longer trusts SE Oct 01 '18 at 16:48

5 Answers5

17
/// <param name="message">Message string to show in the toast.</param>
    private void _ShowAndroidToastMessage(string message)
    {
        AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

        if (unityActivity != null)
        {
            AndroidJavaClass toastClass = new AndroidJavaClass("android.widget.Toast");
            unityActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
            {
                AndroidJavaObject toastObject = toastClass.CallStatic<AndroidJavaObject>("makeText", unityActivity, message, 0);
                toastObject.Call("show");
            }));
        }
    }
Angela Amarapala
  • 1,002
  • 10
  • 26
Awais Naseer
  • 181
  • 1
  • 2
14

You can do this with the Text component and the Mathf.Lerp function by fading the Text in to Color.clear color, waiting for some duration then fading it and out. This post describes how to do that with a simple fadeInAndOut function. Before fading the Text, get the original Text color, then enable the Text component. After fading out, restore the color then disable the Text component.

Here is a simplified toast with the Text component:

void Start()
{
    showToast("Hello", 2);
}

public Text txt;

void showToast(string text,
    int duration)
{
    StartCoroutine(showToastCOR(text, duration));
}

private IEnumerator showToastCOR(string text,
    int duration)
{
    Color orginalColor = txt.color;

    txt.text = text;
    txt.enabled = true;

    //Fade in
    yield return fadeInAndOut(txt, true, 0.5f);

    //Wait for the duration
    float counter = 0;
    while (counter < duration)
    {
        counter += Time.deltaTime;
        yield return null;
    }

    //Fade out
    yield return fadeInAndOut(txt, false, 0.5f);

    txt.enabled = false;
    txt.color = orginalColor;
}

IEnumerator fadeInAndOut(Text targetText, bool fadeIn, float duration)
{
    //Set Values depending on if fadeIn or fadeOut
    float a, b;
    if (fadeIn)
    {
        a = 0f;
        b = 1f;
    }
    else
    {
        a = 1f;
        b = 0f;
    }

    Color currentColor = Color.clear;
    float counter = 0f;

    while (counter < duration)
    {
        counter += Time.deltaTime;
        float alpha = Mathf.Lerp(a, b, counter / duration);

        targetText.color = new Color(currentColor.r, currentColor.g, currentColor.b, alpha);
        yield return null;
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • If you want to display the text in the color that was set in the inspector of the Text, delete the line `Color currentColor = Color.clear;` and set currentColor from orginalColor – maidi Aug 17 '20 at 10:42
4

You can use: SSTools.Message( ).

I 've found a speed guide on youtube

  • I think the best would be to add Text as a public gameobject then drag & drop the text in the inspector of the script attached.Then using SetActive hide and show according to use. – zyonneo Oct 01 '18 at 13:21
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes – Jeroen Heier Oct 11 '18 at 03:34
2

After seeing the answer by @Programmer I got a simple idea on how to show the text.I use the same button to show and hide text. If you dont want to use the button,remove btnShowHide and use the function Show() with invoke and turn it off after few seconds.

public class Toast : MonoBehaviour {

public Button btnShowHide;
public Text txt;
// Use this for initialization
void Start()
{ 
    //Text to be shown
    txt.enabled = false;

 //If using a button with name "Show"
 btnShowHide.GetComponentInChildren<Text>().text = "Show";

}


//Button click function

public  void Show()
{
    if (txt.isActiveAndEnabled)
    {
        txt.enabled = false;
        btnShowHide.GetComponentInChildren<Text>().text = "Show";
    }
    else
    {
        txt.enabled = true;
        btnShowHide.GetComponentInChildren<Text>().text = "Hide";
    }

}


}

OR

use Invoke function but will require to write two functions to display and hide the values.You can customise it according to use.

 public Text textfield;
// Start is called before the first frame update
void Start()
{
    textfield.text = "This is a Toast Message";
    textfield.enabled = false;
}

// Update is called once per frame
void Update()
{

}

public void TextShow()
{
    textfield.enabled = true;
    Invoke("HideText", 2f);

}

public void HideText()
{
    textfield.enabled = false;

}
zyonneo
  • 1,319
  • 5
  • 25
  • 63
0
public void activa_la_co_derum()
    {
        StartCoroutine("desactiva_el_radar_un_momento");
    }

    IEnumerator desactiva_el_radar_un_momento()
    {
        obj_dc.GetComponentInChildren<Text>().enabled = true;
        yield return new WaitForSeconds(1);
        obj_dc.GetComponentInChildren<Text>().enabled = false;
        yield return new WaitForSeconds(1);
        obj_dc.GetComponentInChildren<Text>().enabled = true;
        yield return new WaitForSeconds(1);
        obj_dc.GetComponentInChildren<Text>().enabled = false;
        //geimcaja_a.transform.position = new Vector3(4.57f, 0.5f, -1.34f); geimcaja_a.transform.position = new Vector3(4.57f, 0.5f, -1.34f);

        //yield return new WaitForSeconds(1);
        //Rompecabeza_triguer = false;

    }
fez gugel
  • 11
  • 1