Skip to content

Instantly share code, notes, and snippets.

@prime31
Last active August 22, 2019 15:19
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save prime31/0908e6100d7e228f1add to your computer and use it in GitHub Desktop.
Save prime31/0908e6100d7e228f1add to your computer and use it in GitHub Desktop.
Instructions to make a plugin that needs to override the Unity Activity play nice with others.
In order to take part in the Unity Activity sharing system, there are a few steps you will need to take outlined below. First, some background. You can signify which class you would like to receive all the Activity lifecycle methods. When you implement the methods they should have the exact same signature but they should be *public static void*. That is important! They will not be called if they are not public static void. You can implement 1 or all of the methods. That is entirely up to your needs.
1. Download the Prime31UnityActivity.jar file from here: https://app.box.com/s/xw6hq1ltjaniycc14j21 You are free to distribute the file but *do not change it's name*! That would defeat the purpose of the sharing mechanism!
2. Implement any of the methods that are available in this Gist (https://gist.github.com/prime31/10747997)
3. Perform the AndroidManifest changes below
If successful, on app launch you will see a log that looks like the following:
found Activity proxy class: com.yourpackage.yourclass
Note for plugin vendors: one really simple way to have your plugin "just work" when the Activity sharing system is being used is to perform steps 1 and 2 above and only add the meta-data line to the AndroidManifest. By doing that your plugin will work as it does currently and when the Activity sharing system is being used by the end user.
Manifest Changes:
- add the following line to the AndroidManifest replacing the "com.yourpackage.yourclass" with the full qualified name of the class that you would like to be notified of Activity lifecycle methods.
<meta-data android:name="com.yourpackage.yourclass" android:value="UnityPlayerActivityProxy"/>
- add the following Activity:
<activity android:name="com.prime31.UnityPlayerNativeActivity"
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>
@kennethljj
Copy link

Hi, I am currently using Prime31 Android Social Networking Plugin. I am currently working on a plugin to open the photo gallery and get the Image URI from the result. I have posted a question on SO and a kind soul guided me here. http://stackoverflow.com/questions/27356675/unity-android-gallery/27373880?noredirect=1#comment43201610_27373880

From the Instruction, the part before manifest changes is done in Eclipse and the manifest changes are in Unity. Am I correct?

@maconbot
Copy link

I was wondering the same thing.

@LiKe85
Copy link

LiKe85 commented Apr 14, 2015

To be more precise step 1-2 is creating a java class (not an activity) and implement there one or more of the listed methods (https://gist.github.com/prime31/10747997#). You can create your classes with eclipse or android studio or whatever IDE you prefer. Compile your java class into a .jar folder and place it under Assets/Plugins/Android in your Unity project folder. In the same folder you find the AndroidManifest. Place the meta-data tag for example right after the activity tag of prime31.

@chentotal
Copy link

`import com.prime31.UnityPlayerNativeActivity;

// I got error here. "it is indirectly referenced from required .class files"
public class MitFlurry extends UnityPlayerNativeActivity {

}`
I got error, but I have put Prime31UnityActivity.jar in libs folder.
Could anyone help me to fix this?
Thanks

@majid90132028
Copy link

thank you!

@tgraupmann
Copy link

Where can the source for Prime31UnityActivity.jar be found?

The default Unity source can be found at The source for which is in Program Files\Unity-5.1.2\Editor\Data\PlaybackEngines\androidplayer\com\unity3d\player.

In my case I'd like to customize the UnityPlayerActivity to get more access to UnityPlayer.

UnityPlayer has a protected method restartFMODAudioDevice that I'd like to try invoking to see if I can stop the audio playback on pause.

That's to work around an issue with Unity 5.1.2 where audio plays after onPause because the Unity pause event is timing out. It doesn't crash, it just takes a few seconds before the audio stops after hitting the HOME button.

@Barricks
Copy link

Barricks commented May 17, 2017

Compiler throws an error if you try and use "public static void" as suggested.
If you just use "public void" it compiles but doesn't get called.
What is the answer here?

The answer is to not extend from UnityPlayerNativeActivity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment