Hi folks, I’m having a tough time with this.
I’ve successfully created plugins that do not extend UnityPlayerActivity without a hitch, but extending is troublesome.
I have an SDK I want to integrate that needs to initialize in an onCreate method.
I want to make this plugin generic so that I don’t have to change it for every project.
All of the examples I can find seem to require that you use the same package name in the plugin as you would use in the Unity project. Every attempt I’ve made to change this leads to a Class Not Found Exception as it appears Unity is replacing the plugin’s package name with the Unity project bundle identifier.
So I’m lost…
Does anyone have an Eclipse project that is a plugin which is meant to be packaged into a jar and it extends UnityPlayerActivity AND it works with using its own package name instead of the Unity project package name?
Alternatively is there a better way to do this such as creating a custom activity so that it won’t conflict with other plugins that extend UnityPlayerActivity?
Thanks!!
Does the plugin code has to know its package name ? if so, why ?
Or, do you mean the Unity (C#) code that interfaces with that plugin? (e.g: the code that locates your plugin class and calls into it).
For example, Chartboost uses this as their manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chartboost.sdk.unity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application>
<!-- Required by Chartboost -->
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>
</manifest>
They have 2 jar files: chartboost.jar (which I imagine is their standard Android library) and chartboost_unity_android_bridge.jar (which I imagine is the package com.chartboost.sdk.unity).
When they instance their class they call
var pluginClass = new AndroidJavaClass("com.chartboost.sdk.unity.CBPlugin")
Then they make a call to a method called init() in java. I’m not sure what that might do since they usually need to run their code in an onCreate method which I imagine gets called from an extended activity just by running the app. But they aren’t extending UnityPlayerNative
But looking at their manifest it looks like they are not attaching to UnityPlayerActivity because they are not using an intent filter on MAIN and LAUNCHER.
From the example here (Unity - Manual: Create and use plug-ins in Android) on extending the UnityPlayerActivity the manifest looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company.product">
<application android:icon="@drawable/app_icon" android:label="@string/app_name">
<activity android:name=".OverrideExample"
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>
</application>
</manifest>
Then, say, my Unity Android project package name is com.naplandgames.myproject
I try to call AndroidJavaClass("com.company.product.OverriedExampleNative")
I get an error like this in logCat:
Cannot find class com.naplandgames.myproject.OverriedExampleNative
I think some of my confusion lies in the plugin’s manifest:
- I want to name my plugin package com.naplandgames.plugin and be able to use any name I want in my actual Unity project. So in the plugin’s manifest should com.company.product be replaced with com.naplandgames.plugin?
- I’d prefer not to extend UnityPlayerNativeActivity because I’ve read this can cause conflicts with other plugins that do so. Is there a way to have a plugin with an onCreate method that doesn’t extend UnityPlayerNativeActivity?
I’d really love an example of an Eclipse project that uses onCreate without extending UnityPlayerNativeActivity as well as one that does. The tutorials I see out there leave out some important details. At any rate, the more I dig the more I get confused on this.
I appreciate any help I can get!
I don’t think you can use onCreate without extending the default Unity activity.
If you don’t create your own activity, Unity will use its default one, which will call its own onCreate method.
Regarding your other points:
-
It’s not mandatory to have a manifest for your plugin. Why do you need to have one? Also, the plugin’s package doesn’t necessarily have to be the same one as your game package. For example - your game can use Chartboost, both have a different package. I think Unity will merge these 2 together to form your final game package, so that should be fine.
-
You’re right that extending the activity may conflict with other plugins, but that may not be your case, so you should check it out.
-
Lastly, some plugins mention in their documentation that you should call something in onCreate, but what they mean is that you should initialize them early in the game startup. You can sometimes get by simply by calling them pretty early, but not in onCreate. This frees your from creating your own activity just for overriding the onCreate method.
Let me know if you have any other questions 
Thanks a ton for the info. I’m just going to try doing the plugin without onCreate(). It’ll be 100% easier!