Hi,
I am investigating how to use Unity with Java code and have so far made some progress. I do have a few questions from what I have done so far:
-
What is good practice, i.e. what is editable and what should I leave alone? Is it perceived as “ok” to edit the exported java activity files for example?
-
How would you go about getting objects from the activity and then transferring those objects to other java classes? For example, I want to get the context from the activity class to pass as a constructor to another class.
-
Any advice on generally debugging an application? At the moment I’m having to build and eclipse project in order to best debug the application. Just wondered if anyone else had some thoughts.
Thanks.
Hi,
I have one question too:
How can I create a native Android plugin for Unity 3D?
I use NetBeans and NBAndroid plugin and I have created new Android project with name “Test1” and package name “com.Test1”, selected target platform Android 2.2 then I added “classes.jar” from Unity Editor in my new projects Libs folder.
And here is my code:
package com.Test1;
import android.os.Bundle;
import android.util.Log;
import com.unity3d.player.UnityPlayerActivity;
public class Test1 extends UnityPlayerActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("DEBUG","On create success");
super.onCreate(savedInstanceState);
}
}
Then I build the project, after that I opened my project in windows explorer and navigated to “bin/classes/” there I opened cmd and created jar file from command line ( jar -cf Test1.jar * ). My jar file created contains “com” directory with all class files and a “META-INF” folder. After that I copied Test1.jar in my Unity project on “Assets/Plugins/Android/” folder and I have created a C# class to access that jar file created.
Here is my C# class:
using UnityEngine;
using System.Collections;
public class rImportExportPluginAndroid : MonoBehaviour
{
public static string ImportString()
{
if (Application.platform != RuntimePlatform.Android)
{
return "";
}
AndroidJavaObject test1 = new AndroidJavaObject("com.Test1.Test1");
return "";
}
}
But when I build app on my device he crashed.
What I’m doing wrong?
Also here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
<application
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:debuggable="true">
<activity android:name="com.unity3d.player.UnityPlayerProxyActivity"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.unity3d.player.UnityPlayerActivity"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
</activity>
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<meta-data android:name="android.app.lib_name" android:value="unity" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>
<activity android:name="com.unity3d.player.VideoPlayer"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
</activity>
</application>
<!-- PERMISSIONS -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
** <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
</manifest>