代码之家  ›  专栏  ›  技术社区  ›  zyonneo

如何在Unity中显示类似于android的Toast消息?

  •  0
  • zyonneo  · 技术社区  · 7 年前

    里面有类似祝酒词的东西吗 团结 一种类似于android的操作系统,而不是GUI。在android中,用一行代码就很容易了。

    public void buttonclick()
    {
    // Message to show
    }
    
    4 回复  |  直到 7 年前
        1
  •  10
  •   Programmer    7 年前

    你可以用 Text Mathf.Lerp Color.clear 颜色,等待一段时间,然后淡出。这个 post 描述如何使用简单的 fadeInAndOut

    下面是一个带有文本组件的简化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;
        }
    }
    
        2
  •  9
  •   Angela Amarapala    6 年前
    /// <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");
                }));
            }
        }
    
        3
  •  4
  •   Mike Diginet    7 年前

    我在街上找到了一本速递指南 youtube

        4
  •  1
  •   zyonneo    6 年前

    如果不想使用该按钮,请删除btnShowHide并将函数Show()与invoke一起使用,几秒钟后将其关闭。

    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
  •  0
  •   fez gugel    7 年前
    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;
    
        }