How to Send an Android Toast in Unity

Поділитися
Вставка
  • Опубліковано 5 лют 2025
  • In this video, you'll learn how to send awesome little android toast messages. Check out the script below if you need help getting started and the video's not doing it for you. Since angle brackets are not allowed in youtube descriptions, I advise you to check the video for their locations. I've replaced them with ~ for the time being.
    using UnityEngine;
    public class ToastFactory : MonoBehaviour
    {
    AndroidJavaObject currentActivity;
    public bool isLongToast;
    public void ToggleToastLength()
    {
    isLongToast = !isLongToast;
    }
    public void Start()
    {
    //currentActivity androidjavaobject must be assigned for toasts to access currentactivity;
    AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    currentActivity = UnityPlayer.GetStatic~AndroidJavaObject~("currentActivity");
    }
    public void SendToastyToast(string message)
    {
    if (!isLongToast)
    {
    AndroidJavaObject context = currentActivity.Call~AndroidJavaObject~("getApplicationContext");
    AndroidJavaClass Toast = new AndroidJavaClass("android.widget.Toast");
    AndroidJavaObject javaString = new AndroidJavaObject("java.lang.String", message);
    AndroidJavaObject toast = Toast.CallStatic~AndroidJavaObject~("makeText", context, javaString, Toast.GetStatic~int~("LENGTH_SHORT"));
    toast.Call("show");
    }
    else
    {
    AndroidJavaObject context = currentActivity.Call~AndroidJavaObject~("getApplicationContext");
    AndroidJavaClass Toast = new AndroidJavaClass("android.widget.Toast");
    AndroidJavaObject javaString = new AndroidJavaObject("java.lang.String", message);
    AndroidJavaObject toast = Toast.CallStatic~AndroidJavaObject~("makeText", context, javaString, Toast.GetStatic~int~("LENGTH_LONG"));
    toast.Call("show");
    }
    }
    }

КОМЕНТАРІ • 18

  • @kensyayala7412
    @kensyayala7412 3 роки тому +1

    Thanks for the video, simple and easy to understand!!!

  • @batkovichtech
    @batkovichtech 2 роки тому +1

    Thanks a lot

  • @AaronRod
    @AaronRod 3 роки тому +1

    Thanks, very clear tutorial. I just have a question. Why is it not necessary to include native plugins (.aar)?
    I've seen other examples where they do the same, but they include a .aar to access native features.

    • @wolfscrygames
      @wolfscrygames  3 роки тому +1

      I'm pretty sure AndroidJavaObject is Unity's way of giving easier access to Android specific programming. That would prevent you from needing plugins.

  • @TheDukeOfReason
    @TheDukeOfReason 2 роки тому

    Hey great video! I was thinking not too long ago about getting a toast notification in my game. Nice one. 👌🏽
    I did however notice that if there is a small amount of text in the toast, like just a few characters, if you used the word "Boo" for example, the text isn't centered, it's to the left. I attempted to try and add an extra parameter to modify the "Gravity.CENTER_VERTICAL" for example but I couldn't find out the correct way to implement it. If you can indeed implement it. Any ideas?

  • @davidmason7519
    @davidmason7519 4 роки тому +1

    great tutorial and very helpful and thank you for this.but i have a question and that is
    i want to implement a feature in my game and that is reward system for user and that is when a player install my other game then he/she will get some reward for installing my game so how can i give then reward after they install my game i mean how can i know that they installed my game in android using unity.if you can please make a video.
    and thank you so much again.

    • @wolfscrygames
      @wolfscrygames  4 роки тому

      Great question. I can put something together.

    • @davidmason7519
      @davidmason7519 4 роки тому

      @@wolfscrygames ok sir then if possible then please make a video on it.and thanks for reply.

  • @crakbac
    @crakbac 4 роки тому

    Thanks for the video, can you show your Android Studio project so that I can get a better understanding of your setup?

    • @wolfscrygames
      @wolfscrygames  4 роки тому +1

      You're welcome, I didn't use android studio, just unity.

    • @crakbac
      @crakbac 4 роки тому

      @@wolfscrygames oh then how did you know how to correctly set the AndroidJavaObject in your code? I'm trying to make use of one right now by using
      MyJavaObject = MyJavaClass.Get (), like you did but it never fills MyJavaObject for some reason

    • @crakbac
      @crakbac 4 роки тому

      @@wolfscrygames I assumed that by specifying your activity in AndroidStudio, you made it work. But now that I know you didn't use Android Studio I'm a little confused as to how this all worked without it

    • @wolfscrygames
      @wolfscrygames  4 роки тому

      @@crakbac Are you using MyJavaClass.GetStatic? It's been a while since I put this content together, but I did a bit of research on android devices to find the appropriate documentation.

    • @wolfscrygames
      @wolfscrygames  4 роки тому +1

      @@crakbac Also, are you using unity? It will be a bit different in android studio developer.android.com/guide/topics/ui/notifiers/toasts that's the link to relevant documentation

  • @naaffax6700
    @naaffax6700 4 роки тому

    hi there. Please help me. The test scene which i have compiled as an sdk file and installed on my android S9 plus phone is not showing the 3d elements which i have placed on the scene. It is just a basic cube with rigid body applied to it and falls onto the plane and stops. The sky and horizon shows on the android app as well as the unity splash screen as it loads. the only thing is the 3d objects which i have placed on the scene are not loading.
    However they are loading fine when i test the scene using the play button. And they also work when i built a .exe file for windows and run that also. Also they are loading fine on the android unity remote 5 app after connecting it to the computer using usb cable.
    Am i doing something wrong like do i need to separately place those cube and plane into a folder before compiling them? hope someone can help resolve this for me.

    • @wolfscrygames
      @wolfscrygames  4 роки тому

      Usually, the only issue is corners get cut off if you don't scale properly, but an s9+ shouldn't have any issues with the scene you've described. I use the same phone. It sounds like you forgot to add the scene to build settings, so it's not building your scene. Try saving the scene first, then add it to build settings and build again.