Reputation: 1389
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
}
Upvotes: 11
Views: 27468
Reputation: 1389
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;
}
Upvotes: 2
Reputation: 209
/// <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");
}));
}
}
Upvotes: 19
Reputation: 11
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;
}
Upvotes: 0
Reputation: 125415
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;
}
}
Upvotes: 15
Reputation: 41
You can use: SSTools.Message( ).
I 've found a speed guide on youtube
Upvotes: 4