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.so
.
You 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 <exported_project_directory>/unityLibrary/src/main/cpp/GameActivity/
. 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.
File | 目的 |
---|---|
UGAInput.cpp |
Input events: Here you can adjust or transform input data before GameActivity passes it to Unity. |
UGAApplication.cpp |
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. |
UGASoftKeyboard.cpp |
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:
<exported_project_directory>/unityLibrary/src/main/cpp/GameActivity/
directory into your modified bridge code directory.<exported_project_directory>/unityLibrary/src/main/cpp/GameActivity/
directory. When Unity builds your application, this code will overwrite the default bridge code files with your modified versions.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 ):
using UnityEngine;
public class SendMessageReceiver : MonoBehaviour
{
public void SendMessageFromCpp(string message)
{
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, message);
}
}
\<unityproject\>/ExtraSourceFiles
directory.Note: Don’t place .cpp files in Assets directory, because they will link into IL2CPP’s libil2cpp.so shared library and won’t compile.
The below code calls C# method SendMessageFromCpp on a GameObject called ‘SendMessageReceiver’ and passes HelloFromBridge as an extra parameter whenever you touch the phone screen.
#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");
}
}
});
}
(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)
{
}
}
You should now see HelloFromBridge log, sent from MyFile.cpp and printed by SendMessageReceiver.cs script.
注意 * UnityGameActivityPluginLoad in MyFile.cpp is weakly linked and is called when GameActivity bridge initializes. There’s also ShutdownUserCode if you need it. * MyFile.cpp contains UnityEventProcessInput event. You can find more events in UGAEvents.h file.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.