【问题标题】:How to show a Toast message in Unity similar to one in android?如何在 Unity 中显示类似于 android 中的 Toast 消息?
【发布时间】:2018-10-01 11:53:34
【问题描述】:

Unity 中是否有类似于 android 的 Toast 消息,除了 GUI。在 android 中,只需一行代码就可以轻松完成。

public void buttonclick()
{
// Message to show
}

【问题讨论】:

  • 嗯... Unity 不是 Android... 在 Unity API 中搜索适合您的内容或自己编写一个 toast 方法。也许这可以帮助:answers.unity.com/questions/594755/…
  • 对于那些不熟悉 Android Toast 消息的人来说,截取您想要的样子会很有帮助。

标签: c# unity3d


【解决方案1】:
/// <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");
            }));
        }
    }

【讨论】:

  • 你太棒了!
  • ios中没有toast这个东西,你应该自己实现
【解决方案2】:

您可以使用Text 组件和Mathf.Lerp 函数执行此操作,方法是将文本淡入Color.clear 颜色,等待一段时间然后淡出。这个post 描述了如何使用一个简单的fadeInAndOut 函数来做到这一点。在淡化文本之前,获取原始文本颜色,然后启用文本组件。淡出后,恢复颜色,然后禁用 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;这一行并设置currentColor from orginalColor
【解决方案3】:

你可以使用:SSTools.Message()。

我在youtube找到了速度指南

【讨论】:

  • 我认为最好将文本添加为​​公共游戏对象,然后将文本拖放到附加脚本的检查器中。然后根据使用情况使用 SetActive 隐藏和显示。
  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效
【解决方案4】:

看到@Programmer 的答案后,我对如何显示文本有了一个简单的想法。我使用相同的按钮来显示和隐藏文本。 如果您不想使用按钮,请删除 btnShowHide 并使用函数 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";
    }

}


}

使用调用函数,但需要编写两个函数来显示和隐藏值。您可以根据使用情况自定义。

 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;

}

【讨论】:

    【解决方案5】:
    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;
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 2014-07-04
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多