Unity and Android Interactive summary

Source: Internet
Author: User

On the internet to find a lot of tutorials, basically using the method is to create a new normal Android project on Eclipse, let the main activity inherit unityplayeractivity, Then write the interface for unity to invoke in the activity, or proactively callback the Unity interface, and then export the jar package to unity.

This way, as long as the character is not good, there will be a lot of messy problems:

If you choose the SDK version less than 4.0 when creating a new project in the unity build apk times is wrong, because Style.xml will not have parent= "Theme.AppCompat.Light, but parent=" Android: Theme.light ";

If you succeed in bundling the APK into unity, the runtime will not be able to enter the scene content in unity but directly into the activity in Android

Such as....

Also tried to use Android Studio, thinking that the official things may be more reliable, the results found that Android Studio can not directly export the jar package, need to create a new library and then modify the configuration release files and many other complex steps ... Finally generated jar package, and then the egg, still can not use, seemingly generated jar package content is wrong, then give up, do not want to continue to study, perhaps my method error ...

Here's what I think is the most convenient way to do this:

New empty project in 1.unity.

2. Select Export Android Project.

3. Open the project with Eclipse (if an Android project that requires access is merged), start the write interface in Eclipse.

4. After the interface is finished, right click on the SRC directory Export->java->jar file export JAR Package, note that only need to export the SRC directory of Things to do, do not add other useless things (third-party plugins may need to package plug-in related things).

5. Go back to unity and create a new plugins/in project Android (unity5.x Previous version must be the directory structure) directory, the previous export of the jar package into the directory (it is better to build a bin directory in the bin), as well as the Android project Androidmainfest.xml also copied to Android (with third-party plugins The entire directory of RES also needs to be copied).

6. You can now freely use the Android plugin in unity!

This way of exporting Android works eliminates the need to manually go to the Unity installation path to find and import Class.jar and bundle identifier to align with unity.

Next, there are two ways unity calls the Android interface:

1. Most of the online tutorials are used:

1 if(Guilayout.button ("Test", Guilayout.height ( -)))2 {3Androidjavaclass JC =NewAndroidjavaclass ("Com.unity3d.player.UnityPlayer");4Androidjavaobject Jo = JC. Getstatic<androidjavaobject> ("currentactivity");5Jo. Call ("androidfunction","teststring");6}

This approach requires modifying the Android main activity to allow it to inherit unityplayeractivity, by getting Jo to invoke the various interfaces in the activity.

2. Do not modify any code in the Android project, create a new normal class directly in SRC (which can also be placed in the new package), do not need to inherit any classes, write directly to the interface you need to provide unity calls, followed by unity calls as follows:

1 if(Guilayout.button ("Test", Guilayout.height ( -)))2 {3Androidjavaclass JC =NewAndroidjavaclass ("Com.package.package.YourNewClass");4      //or Androidjavaobject Jo = new Androidjavaobject ("Com.package.package.YourNewClass");5Lch Callstatic ("androidfunction","teststring");6      //or Jo. Callstatic ("Androidfunction", "teststring");7      The //method can be called either public or private.if you want to invoke a non-static method, you can use only Jo8}

When you have multiple plug-in SDKs that need to be plugged in, you can write them separately in different classes to reduce the coupling. Of course, if the SDK has UI interaction, it still needs activity.

As for Android callback unity there is nothing to say, only unityplayer.unitysendmessage ("Testgameobject", "Func", str) this method only.

Unity and Android Interactive summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.