SDK学院 第二节、自定义Unity Activity

本文介绍如何定制Mobage Unity SDK中的UnityPlayerActivity和UnityPlayerNativeActivity。通过复制源文件、进行自定义修改、重新编译并安装到Unity项目中,可以实现对默认活动的个性化定制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习交流群:

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 UnityPlayerActivity class that ships with the Mobage SDK. To streamline the process, first determine if there are any differences between the UnityPlayerActivity class in your current SDK and the latest SDK using a tool such asdiff.

If there are differences, port your customizations forward and then follow the instructions on this page to compile and install your updated .jar file.

If there are no differences, re-install your custom jar file, following the instructions in Step 3: Installing Your New UnityPlayerActivity.jar. When you upgrade a project to a newer version of the Mobage Unity SDK, all old Mobage SDK files are overwritten. Therefore, your custom jarfile must be re-installed.

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.

  1. Create a directory to hold your custom classes and their build scripts.
    $ mkdir <WORK_DIR>
  2. 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:

  1. 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
  2. Open the build script <WORK_DIR>/make-unityplayer for editing.
  3. Update the ANDROID_SDK_ROOTUNITY_INSTALL_DIR, and PROJECT_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 specifies android-10, but you can specify any platform that is android-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.
  4. 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

-b--build

Builds UnityPlayerActivity.java.

-i--install

Installs UnityPlayer.jar into <PROJECT_DIR>/Assets/Plugins/Android.

-f--facebook

Sets up the class path to point to the Facebook jar files. TransformsMBUnityPlayerActivity and MBUnityPlayerNativeActivity to extendFBUnityPlayerActivity and FBUnityPlayerNativeActivity.

-p--prime31

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 UnityPlayerProxyActivityUnityPlayerActivity, andUnityPlayerNativeActivity classes work.
  • What changes Mobage made to these classes.
 

The Mobage Unity SDK does not support UnityPlayerNativeActivity. It is discussed here only to provide context.

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—UnityPlayerProxyActivityUnityPlayerActivityUnityPlayerNativeActivity—are compiled into a file called UnityPlayer.jarThe 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 from UnityPlayerActivity. If you integrated the Facebook SDK for Unity, it extends FBUnityPlayerActivity. If you integrated the Prime31 Social Networking plugin, it extends Prime31's UnityPlayerActivity. The base class is determined by the options you pass to make-unityplayer. The MBUnityPlayerActivity class contains customizations such as support for the Mobage Ad Tracking Library.
  • MBUnityPlayerNativeActivity—Extended from UnityPlayerNativeActivity. If you integrated the Facebook SDK for Unity, it extends FBUnityPlayerNativeActivity. If you integrated the Prime31 Social Networking plugin, it extends Prime31's UnityPlayerNativeActivity. The base class is determined by the options you pass to make-unityplayer. The MBUnityPlayerNativeActivity 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.

  1. Open <PROJECT_DIR>/Assets/Plugins/Android/AndroidManifest.xml for editing.
  2. Specify your new UnityPlayerActivity class in place of the original class.
    Find all <activity> elements where the android:name attribute is set to...
    • com.test.android.UnityPlayerActivity
    • com.test.android.UnityPlayerNativeActivity
    • com.test.android.UnityPlayerProxyActivity
    ...and replace the value for android:name with your custom package name.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值