在Unity中是否有类似于Android中Toast消息的东西,除了GUI之外?在Android中可以通过一行代码轻松实现。
public void buttonclick()
{
// Message to show
}
在Unity中是否有类似于Android中Toast消息的东西,除了GUI之外?在Android中可以通过一行代码轻松实现。
public void buttonclick()
{
// Message to show
}
/// <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");
}));
}
}
Text
组件和 Mathf.Lerp
函数来实现此操作,将文本淡入至 Color.clear
颜色,等待一段时间后再将其淡出。这篇文章介绍了如何使用简单的 fadeInAndOut
函数来完成此操作。在淡出文本之前,获取原始文本颜色,然后启用 Text 组件。淡出后,恢复颜色,然后禁用 Text 组件。
以下是一个带有 Text 组件的简化版 toast:
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;
}
}
Color currentColor = Color.clear;
这一行,并从 orginalColor 设置 currentColor。 - maidi在看了@Programmer的回答后,我有一个简单的想法来显示文本。我使用相同的按钮来显示和隐藏文本。 如果你不想使用按钮,删除btnShowHide并使用带有invoke的函数Show(),然后在几秒钟后将其关闭。
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";
}
}
}
或者
使用Invoke函数,但需要编写两个函数来显示和隐藏值。您可以根据需要自定义它。
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;
}
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;
}