Modify GameActivity bridge code
Make changes to the code that connects the Unity runtime to the GameActivity library.
Read time 4 minutesLast updated 12 days ago
GameActivity interacts with Unity via a bridge that you can modify to make changes and implement additional features. The code that makes up the bridge is written in C++ and during the build process, GameActivity builds it into a shared library called .
libgame.soYou can't modify bridge code within Unity itself; you must first export your project. After you export your project, you can find the files that comprise the bridge code in . Most of the code files in this directory contain utility code. The following table shows you the purpose of the most important bridge code files.
<exported_project_directory>/unityLibrary/src/main/cpp/GameActivity/File | Purpose |
|---|---|
| Input events: Here you can adjust or transform input data before GameActivity passes it to Unity. |
| Lifecycle events: Here you can change how to handle events such as pausing, resuming, focusing, and unfocusing. This is the core of the code bridge. |
| Touchscreen keyboard: Here you can change the implementation of the on-screen keyboard. The default implementation uses GameTextInput. |
During the project export process, Unity's incremental build pipeline might overwrite any changes you make in the exported project. If you want your changes to persist:
- Export your project.
- Create a new directory that's outside your Unity project. This new directory is your modified bridge code directory.
- Copy the code files that you want to modify from the directory into your modified bridge code directory.
<exported_project_directory>/unityLibrary/src/main/cpp/GameActivity/ - In Unity, create a new C# script that uses Android.IPostGenerateGradleAndroidProject to copy the code files from your modified bridge code directory back into the directory. When Unity builds your application, this code will overwrite the default bridge code files with your modified versions.
<exported_project_directory>/unityLibrary/src/main/cpp/GameActivity/ - Make any bridge code modifications in the files in your modified bridge code directory.
Expand GameActivity bridge code
You can add extra source files to the existing GameActivity bridge files which are then compiled together.
For example (You can find an example project here ):
-
Create C# script in Unity and name it SendMessageReceiver.cs.using UnityEngine;public class SendMessageReceiver : MonoBehaviour{ public void SendMessageFromCpp(string message) { Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, message); }}
-
Create a new GameObject and name it SendMessageReceiver.
-
Attach a SendMessageReceiver script to SendMessageReceiver GameObject.
-
Createin
MyFile.cppdirectory.\<unityproject\>/ExtraSourceFilesThe following code callson a GameObject calledSendMessageFromCppand, passesSendMessageReceiveras an extra parameter whenever you touch the phone screen.HelloFromBridge#include "UGAApplication.h"#include "game-activity/native_app_glue/android_native_app_glue.h"void UnityGameActivityPluginLoad(Unity::UnityApplication& application){ application.GetEvents().Register<Unity::UnityEventProcessInput>([](const Unity::UnityEventProcessInput& e) { auto inputBuffer = e.GetInputBuffer(); if (inputBuffer->motionEventsCount != 0) { for (uint64_t i = 0; i < inputBuffer->motionEventsCount; ++i) { GameActivityMotionEvent* motionEvent = &inputBuffer->motionEvents[i]; if (motionEvent->action == AKEY_EVENT_ACTION_DOWN) e.GetApplication().SendMessage("SendMessageReceiver", "SendMessageFromCpp", "HelloFromBridge"); } } });} -
Place the following editor script PostProcessor.cs in the Assets/Editor folder:(It ensures that 'ExtraSourceFiles/MyFile.cpp' is copied to 'unityLibrary/src/main/cpp/GameActivity/CustomFolder/MyFile.cpp' in an incremental build friendly way. )using System;using UnityEditor.Android;using UnityEditor;using UnityEngine;public class PostProcessor : AndroidProjectFilesModifier{ const string CustomSourceFileSrc = "ExtraSourceFiles/MyFile.cpp"; const string CustomSourceFileDst = "unityLibrary/src/main/cpp/GameActivity/CustomFolder/MyFile.cpp"; public override AndroidProjectFilesModifierContext Setup() { var ctx = new AndroidProjectFilesModifierContext(); ctx.Dependencies.DependencyFiles = new[] { CustomSourceFileSrc }; ctx.AddFileToCopy(CustomSourceFileSrc, CustomSourceFileDst); return ctx; } public override void OnModifyAndroidProjectFiles(AndroidProjectFiles projectFiles) { }}
-
From the Android Player settings window, go to Other Settings > Configuration > Application Entry Point and select GameActivity.
-
Select Build & Run.
-
Touch the phone screen and check the logcat.
You can now check the log, sent from and printed by script.
HelloFromBridgeMyFile.cppSendMessageReceiver.csNotes:
- in
UnityGameActivityPluginLoadis weakly linked and is called when GameActivity bridge initializes. There's also ShutdownUserCode if you need it.MyFile.cpp - contains
MyFile.cppevent. You can find more events inUnityEventProcessInputfile.UGAEvents.h