This is the 20th day of my participation in the August Text Challenge.More challenges in August
Recommended reading
- CSDN home page
- GitHub open source address
- Unity3D plugin sharing
- Jane’s address book
- My personal blog
- QQ group: 1040082875
Hello everyone, I am a Buddhist engineer ☆ quiet small magic dragon ☆, update Unity development skills from time to time, think useful remember one key three link oh.
One, foreword
This article focuses on how to communicate with Unity using Android Studio. The main content includes how to create an Android Studio project, how to set it up, and how to export it. And how to call Android from Unity.
Ii. Reference materials
- Unity communicates with Android blog.csdn.net/qq_33747722…
- Unity and Android communicate with each other blog.csdn.net/qq_15267341…
- [Unity] [Android] Unity and Android interactive communications Studio 3.0 (1) the Android Studio 3.0 set blog.csdn.net/bulademian/…
Three, directories,
- Creating an Android Project
- Create an Android template Module
- Import and load jar files
- Modify the androidMainfest.xml file
- Modify the MainActivity file
- Compile build project
- Import the Unity
- Unity calls the Android project method
Four, the body
1. Create an Android project
Fiele->New->New Project Click FINISH to create a new project.
2. Create the Android template Module
Right-click project New->ModuleSelect the Android LibraryThis is where you can set the module nameJust click FinshIf you accidentally write the wrong Module name and want to delete it, right-click Open Module SettingsJust delete it
3. Import and load external JAR files
There are three ways to load external JAR files. Here I only use the first method. The rest of the methods can be referred to in my other article. 【Android Studio】 Import external JAR packagesBlog.csdn.net/q764424567/…Switch to the Project viewFind the LIBS folderDrag and drop the jar package you want to use (copy and paste will do) Right-click the Jar file, click Add As Library, and then click OK in the pop-up box that appearsThe JAR package is successfully added and importedOpen the build.gradle file and you can see the last line of successfully added code
4. Load Unity’s classes.jar into your project
The classes.jar file can be found in Unity’s installation directory, copied and pasted into your project’s libs file OK
5. Create MainActivity
Switch to Android View and expand unity_exchange-> Java -> right-click the first folder Do nothing and click the FINISH button.
The MainActivity is created, and the initial script is on the right.
6. Modify androidMainfest.xml
1. Delete activitY_MAIN_XML from layout in the RES folder under Unity_exchangeIf there is an errorThe mainactivity. Java scriptComment out this line
2. Modify the androidmanifest.xml file in the mainfests file under the unity_exchange directory
Open androidmanifest.xml in the MANIFESTS folder in the app directoryCopy this codePaste to unity_exchange – > manifest – > AndroidManifest. The XML
7. Modify the MainActivity file
This time, as shown below.Change to the following.Here are the methods to call when writing Unity on Android. For example, I’ll just write a simple sum of two numbers:
8. Compile the build items
Select unity_exchange and click build — Make Module ‘unityExchange’If you wait, something like the following will appear. If not, try again a few times.Recompile the Project using Build — Make Project or Build — Rebuild Project. If you switch to the Project view, you can see that there is a packaged-classes folder in the build file. Different versions of Android Stuido may have different folder namesRight-click show in Explorer and open the folder to the current directory2. Move classes.jar to the libs folder3. Copy the androidmanifest.xml and res folder into this folder
9. Import Unity
Create a new Unity project and create Plugins->Android in the project directory. Then copy the 3 files shown above
Unity calls the Android project method
1. Create test1.cs and mount it on the Main Camera2. Write code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class test1 : MonoBehaviour
{
private Transform cantrans;//Canvas
private Text text;//text
private Button button;/ / button
private AndroidJavaObject jo = null;
private InputField input1;
private InputField input2;
void Start()
{
//
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
cantrans = GameObject.Find("Canvas").transform;
text = cantrans.Find("Text").GetComponent<Text>();
button = cantrans.Find("Button").GetComponent<Button>();
input1 = cantrans.Find("InputField").GetComponent<InputField>();
input2 = cantrans.Find("InputField2").GetComponent<InputField>();
button.onClick.AddListener(OnClick);
}
// Button method
public void OnClick()
{
text.text = "";
int res = jo.Call<int> ("Add".int.Parse(input1.text), int.Parse(input2.text)); text.text = res.ToString(); }}Copy the code
3. Make the UI4. Pack it up
Copy this line from androidmanifest.xmlPaste it herePublish, run