Using the Unity3d development needs to transplant the animation effect to the existing app. Unity for Android is special, unity for IOS Packaging is to give the Xcode project directly to the developer, developers can continue to add new views on the basis of the project, and finally by the developer to package the creation of the IPA package, publish the program. and the unity for Android package directly generated APK package, is equal to saying that the source code developers are not visible. Imagine that, since they can pack into the APK installation package must have gone ANDROID-SDK packaging process, there is this process we can naturally get the source code (that is, anti-compilation). Study for a while, found in fact very simple, wrote a small demo put up. All actions are as follows
Create an Android project first, which is simply not explained in detail. Then open the Unity installation directory, find the need to use the Class.jar file, is the package calls the Unity Engine Java source code, location in unity\editor\data\playbackengines\androidplayer\bin\ Classes.jar (installation directory), and then copy this file to the Android Project Libs directory, the system will automatically reference, adt22 the following do not remember, if not, you need to set up, right-click Project
You can add this file.
Then the mainativity slightly changed, is to inherit the parent class into unityplayeractivity, so docking on the Unity interface, the source code is as follows:
Package Com.lee.unityandroid;import Android.os.bundle;import Android.widget.relativelayout;import Com.unity3d.player.unityplayeractivity;public class Mainactivity extends Unityplayeractivity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); Relativelayout layout = (relativelayout) Findviewbyid (r.id.parent_layout); Layout.addview (Munityplayer.getview ());}}
For the sake of observation, the layout was slightly altered. Show this in our view.
Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= ". Mainactivity "> <relativelayout android:id=" @+id/parent_layout " android:layout_width=" 300DP " android:layout_height= "500DP" android:layout_centerinparent= "true" > </RelativeLayout> </RelativeLayout>
To configure the manifest file, the package name is used in the following unity
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android " package=" com.lee.unityandroid " android:versioncode=" 1 " android:versionname=" 1.0 "> <USES-SDK android:minsdkversion= "Ten" android:targetsdkversion= "/> <application " Android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" Android:theme= "@style/apptheme" > <activity android:name= "Com.lee.unityandroid.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= " Android.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter> </activity> </application></manifest>
Android is basically complete. Now create a unity project, name whatever you want, and then in the menu File->build settings->android->player Settings
The last generated apk file we directly unzip, the inside of the assets directory below the data directory copied to our project assets directory, and then the Lib directory under the armeabi-v7a or Armeabi copy together (if all, only one of them can be copied one ), and finally compile our program with the following results:
White is our layout container, and blue is the Unity view content.
Let's go on to the function called Andriod in unity, create a C # script in Unity Project, bind it directly to the camera, and use it to notify the interface to open the activity. As shown, using the GUI to create two buttons on the screen, click the button to open the different activity.
Using unityengine;using System.Collections; public class test:monobehaviour{//update is called once per framevoid update () {//When the user presses the phone's return key or home key to exit the game if (Input.getke Ydown (Keycode.escape) ¦¦input.getkeydown (keycode.home)) { application.quit (); }} void Ongui () {if ( Guilayout.button ("OPEN Activity01", Guilayout.height (100))) {//NOTE 1 Androidjavaclass JC = new Androidjavaclass (" Com.unity3d.player.UnityPlayer "); Androidjavaobject Jo = JC. Getstatic<androidjavaobject> ("currentactivity"); Jo. Call ("Method01", "first Method");} if (Guilayout.button ("OPEN Activity02", Guilayout.height ()) {Androidjavaclass JC = new Androidjavaclass (" Com.unity3d.player.UnityPlayer "); Androidjavaobject Jo = JC. Getstatic<androidjavaobject> ("currentactivity"); Jo. Call ("Method02", "second Method");}} }
The above code is the reflection mechanism inside Java.
- This article fixed link: http://www.ithtw.com/2910.html
- Reprint Please specify: Leehom May 15, 2015 in it 100,000 why published
Unity3d porting to your Android program