Generate and Use Unity Plugins
Displaying Toast within unity project — Android Oriented Post
2 min readJan 18, 2017
Step 1: Create a new Android Library
You can do it by creating a new android application project and transform it to an android library project. Or you can create a new android application and add a new library module.
Step 2: Crate a Class and set it to display a Toast
Watch out:
- To display a Toast you need a Context, but the library have no UI context, so the way to solve it, is to pass the context from the unity project using the method setActivity(Activity activity)
Step 3: Build the library project and generate aar file.
You can just build your project and open next folder for the aar file:
ProjectFolder\ModuleFolder\build\outputs\aar
If you can’t find any aar files in this folder:
- Open the Gradle windows in Android studio
(View → Tool Windows → Gradle) - Locate your library → drill down to build folder → right click on assemble and Run the first option, this will generate aar files to above folder
Step 4: Open a new Unity Project
- Add your aar file to the project
- Create a new script and attach it to view, in our example we add a Text to the main scene and attach the next script to it. But you can of course attach the script to any object or call it as you like.