学习交流群:
U3D游戏 SDK学院 :175492844
Customizing the Default Unity Activity (Optional)
The Mobage Unity SDK uses customized versions of UnityPlayerActivity
andUnityPlayerNativeActivity
. When you install the SDK, the default classes are replaced with Mobage's customized versions. To add your own customizations to UnityPlayerActivity
orUnityPlayerNativeActivity
:
- Make a copy of the source files and build scripts that ship with the Mobage Unity SDK.
- Customize the source code.
- Compile the source code into a
jar
file. - Install the new
jar
file.
| Important Every time you upgrade to a newer version of the Mobage SDK, you must port your customizations forward to the version of the If there are differences, port your customizations forward and then follow the instructions on this page to compile and install your updated If there are no differences, re-install your custom |
The following conventions are used in this procedure:
-
<WORK_DIR>
—Path to the directory where you edit and build your customizedUnityPlayerActivity.java
. In the first step of the procedure, you create this directory copy all necessary files into it. -
<MOBAGE_SDK>
—Path to the unpacked Mobage Unity SDK. -
<PROJECT_DIR>
—Path to your Unity project.
Step 1: Get a Copy of the Source Files and Build Scripts
Get a copy of the source files for UnityPlayerActivity
and UnityPlayerNativeActivity
that ship with the Mobage SDK.
- Create a directory to hold your custom classes and their build scripts.
$ mkdir <WORK_DIR>
- Copy the contents of the
UnityPlayer
directory from the Mobage Unity SDK to your build directory.
$ cp -a <MOBAGE_SDK>/UnityPlayer/* <WORK_DIR>
Step 2: Customize the Source
Make your customizations to the template in <WORK_DIR>/template/MBUnityPlayerActivity.java
.
The template contains a class that:
- Extends
UnityPlayerActivity
. - Contains all of Mobage's required customizations.
In the next step, a number of source files are auto-generated from the template, including anMBUnityPlayerNativeActivity
class. These source files are built into UnityPlayer.jar
.
Step 3: Build UnityPlayer.jar
Use the build scripts to generate a new UnityPlayer.jar
file:
- Generate a source file from
template/MBUnityPlayerActivity
.
There are several source generation scripts.
If your app does not use the Facebook SDK, run the following command:
$ node genUnityPlayer.js
If your app integrates with the Facebook SDK for Unity, run the following command:
$ node genUnityPlayerFacebook.js
If your app integrates with the Prime31 Social Networking plugin, run the following command:
$ node genUnityPlayerPrime31.js
- Open the build script
<WORK_DIR>/make-unityplayer
for editing. -
Update the
ANDROID_SDK_ROOT
,UNITY_INSTALL_DIR
, andPROJECT_DIR
variables for your environment.-
ANDROID_SDK_ROOT
—Path to a platform directory in the Android SDK. Use the directory that corresponds to the version of Android that you are developing against. The example path specifiesandroid-10
, but you can specify any platform that isandroid-8
or higher.
-
UNITY_INSTALL_DIR
—Path to the directory where Unity is installed. Unity is normally installed in/Applications/Unity
. -
PROJECT_DIR
—Path to your Unity project directory.
-
-
Run the build script. The options you pass to the build script depends on how your app integrates with Facebook.
If your app does not use the Facebook SDK, run the following command:
$ ./make-unityplayer -b
If your app integrates with the Facebook SDK for Unity, run the following command:
$ ./make-unityplayer -b -f
If your app integrates with the Prime31 Social Networking plugin, run the following command:
$ ./make-unityplayer -b -p
The script creates a
UnityPlayer.jar
file in the current directory.
Step 4: Install Your New UnityPlayer.jar
The make-unityplayer
script has an install option that copies the generated UnityPlayer.jar
into your Unity project, replacing the existing UnityPlayer.jar
. Your new UnityPlayer.jar
is copied into<PROJECT_DIR>/Assets/Plugins/Android
.
To install your new UnityPlayer.jar
:
$ ./make-unityplayer --install
Additional Notes
This section contains additional details on how the Mobage SDK integrates with Unity.
The make-unityplayer Script
The make-unityplayer
script builds and installs UnityPlayerActivity
. It supports the following options:
Option | Description |
---|---|
| Builds UnityPlayerActivity.java . |
| Installs UnityPlayer.jar into <PROJECT_DIR>/Assets/Plugins/Android . |
| Sets up the class path to point to the Facebook jar files. TransformsMBUnityPlayerActivity and MBUnityPlayerNativeActivity to extendFBUnityPlayerActivity and FBUnityPlayerNativeActivity . |
| Sets up the class path to point to the Prime31 jar files. TransformsMBUnityPlayerActivity and MBUnityPlayerNativeActivity to extend Prime31's UnityPlayerActivity and UnityPlayerNativeActivity . |
By default (without no options), make-unityplayer
only builds UnityPlayer.jar
. With both options (-b -i
), make-unityplayer
builds and installs UnityPlayer.jar
. With just the -i
option, make-unityplayer
only installs UnityPlayer.jar
.
UnityPlayerActivity Implementation Details
This section describes:
- How the original
UnityPlayerProxyActivity
,UnityPlayerActivity
, andUnityPlayerNativeActivity
classes work. - What changes Mobage made to these classes.
The Mobage Unity SDK does not support |
When a Unity app starts, UnityPlayerProxyActivity
chooses which activity to instantiate:UnityPlayerActivity
or UnityPlayerNativeActivity
. It chooses which class to instantiate based on the version of Android running on the device. Support for native activities was not added to Android until Gingerbread (API level 9) and so UnityPlayerNativeActivity
is only instantiated if the device is running Gingerbread or higher. Otherwise, UnityPlayerActivity
is instantiated.
Both UnityPlayerActivity
and UnityPlayerNativeActivity
are responsible for starting the Unity game engine. These classes act like a bridge between the Java world of an Android app and the C# world of the Unity game engine. The UnityPlayer*
classes let you do things that you would expect to do in any Android app, such as receive and handle lifecycle events.
All three classes—UnityPlayerProxyActivity
, UnityPlayerActivity
, UnityPlayerNativeActivity
—are compiled into a file called UnityPlayer.jar
. The Mobage SDK ships with a customUnityPlayer.jar
, which is used when you build your Unity app. The classes in the MobageUnityPlayer.jar
have the following modifications:
-
MBUnityPlayerProxyActivity
—No changes. The code for this class is generated by<MOBAGE_SDK>/WW/UnityPlayer/createJavaFiles*.rb
. -
MBUnityPlayerActivity
—Extended fromUnityPlayerActivity
. If you integrated the Facebook SDK for Unity, it extendsFBUnityPlayerActivity
. If you integrated the Prime31 Social Networking plugin, it extends Prime31'sUnityPlayerActivity
. The base class is determined by the options you pass tomake-unityplayer
. TheMBUnityPlayerActivity
class contains customizations such as support for the Mobage Ad Tracking Library. -
MBUnityPlayerNativeActivity
—Extended fromUnityPlayerNativeActivity
. If you integrated the Facebook SDK for Unity, it extendsFBUnityPlayerNativeActivity
. If you integrated the Prime31 Social Networking plugin, it extends Prime31'sUnityPlayerNativeActivity
. The base class is determined by the options you pass tomake-unityplayer
. TheMBUnityPlayerNativeActivity
class contains Mobage customizations such as support for the Ad Tracking Library.
For additional details about the original UnityPlayer.jar
classes, see the source code in /Applications/Unity/Unity.app/Contents/PlaybackEngines/AndroidPlayer/src/com/unity3d/player
.
For additional details about the modifications in the Mobage UnityPlayer.jar
classes, see the source code in <MOBAGE_SDK>/WW/UnityPlayer
.
Fore more details
Package Name
If you change the package name in UnityPlayerActivity.java
andUnityPlayerNativeActivity.java
from com.test.android
to a another value, then you must update your AndroidManifest.xml
.
- Open
<PROJECT_DIR>/Assets/Plugins/Android/AndroidManifest.xml
for editing. - Specify your new
UnityPlayerActivity
class in place of the original class.
Find all<activity>
elements where theandroid:name
attribute is set to...-
com.test.android.UnityPlayerActivity
-
com.test.android.UnityPlayerNativeActivity
-
com.test.android.UnityPlayerProxyActivity
android:name
with your custom package name. -