Examples when to extend UnityPlayerActivity?

Hi,

I did not really understand what the differences are if we extend “UnityPlayerActivity” in a plugin, or if we just call a function from a .java class ?
Would you have some examples?

Thanks

If you don’t know if you need to extend UnityPlayerActivity, it means you don’t need to extend it :slight_smile:

Extending UnityPlayerActivity is only needed if you need to hook into Activity-specific methods. It’s also problematic, as it’s hard to make two plugins that both extend UnityPlayerActivity work together. (Hence Prime31 warns against using some of its plugins with Facebook’s plugin.) At look at how Facebook do it in their plugin, but it’s not that interesting. Just figure out what you want your plugin to do, then design for that.

OK, thanks vladimirg, would you have some examples of the “specific method” ? I am looking to create some plugin that would connect with Twitter, Facebook, and Google Play in app Billing.

You can take a look at the Activity documentation. If you need any of those hooks, then you may need to subclass Unity’s Activity. Your description is too general - it really depends on specific features.

OK, thanks for your answers

vladimirg, are you recommending to extend Activity instead of UnityPlayerActivity or are you suggesting there is no need to extend any kind of Activity at all?

Also, if we don’t extend any Activity, what is the default LAUNCH Activity in the AndroidManifest file?
The following link seems to suggest that if you don’t have an Activity, there is still a way to get the Context for use in your Android API calls.
http://www.vortech.net/2013/03/accessing-the-android-activity-context-in-unity3d/

Goat666, I’m afraid the answer is the ever-annoying “it depends”. What do you want to do in your plugin, exactly? For example, in our plugin we have our own Activity, but we don’t subclass Unity’s Activity. Facebook’s plugin does both.

The launched activity will be Unity’s ProxyActivity, which then decides between a regular Unity Activity and a Native Unity Activity. You don’t have to worry about that.

Finally, regarding the Context, yes, that technique is good.

Hi Vladimir,

Thanks so much for responding. I’m making a plugin for In App Billing and I need to use the onActivityResult() method which means I will need to have an Activity class. But I see no reason to use UnityPlayerActivity.

  1. Does the Unity game start UnityProxyActivity immediately on game startup or will it do it when you try to access Java for the first time?

  2. If I have a normal Activity, I will list it in my AndroidManifest but does it have to be marked with LAUNCH? Or do I just launch it from code by creating an object of it using AndroidJavaObject?

I wish this stuff was documented just a little better. About a couple hours of documentation on their part would say thousands of developer hours in trying to figure all this out.

Thanks again!

goat666,

If you need onActivityResult, I think you have to subclass Unity’s activity. That will make it very difficult to interoperate with any other plugin that does the same, though, for example Facebook’s plugin.

  1. If by “Dropbox” you mean “Unity”, then yes. I’m making an educated guess that the “concrete” activities are the ones that run the Mono VM, so by necessity they’ll be up long before any C# code executes.

  2. If you only subclass Unity’s activity, then the required changes are explained in the docs (yes, they’re not very good, but in this case it’s there under the “Extending the UnityPlayerActivity Java Code” section). Basically you just set your subclass as the LAUNCH activity and ignore the other Unity activity classes. If you also have a separate activity for UI or whatnot, then you just add it to manifest as you would any other auxiliary activity, and run it later from your code. Though I would suggest using fragments and not activities for UI if you can.

Vladimir,

Thanks for the quick responses. I did mean “Unity game/app” when I said “Dropbox”. Sorry, corrected now in my post.

I have read about the issues with interoperating with other plugins that subclass the Unity activity. Finally we are getting to the meat of the issue. Questions follow:

  1. I’m not yet sure why I need to subclass Unity’s Activity. Why not any Activity that I can start with “startActivity” from another Java class? I created a basic Java class which I called from C# and it worked. Then from there I temporarily create an Activity and then close (finish) it. This seemed to work and the Activity’s onActivityResult was also called.

  2. What is the difference between having your own Activity and subclassing, let’s say, UnityPlayerActivity? This is the actual question that led me here. Why would you specifically subclass UnityPlayerActivity?

  3. When I extended UnityPlayerActivity, it seemed like my onCreate got called without ever using JNI code explicitly so it does seem like the Unity system initializes it well before most other C# code. I will also be using the FB plugin so I don’t want to subclass UnityPlayerActivity.

Thanks so much!!!

goat666,

  1. If your in-app billing works by presenting a custom Activity at certain points in-game, and you need its result and can get it through onActivityResult, then by all means, do that. If that’s all you need, I don’t see a point in subclassing Unity’s activities.

  2. Subclassing Unity’s Activity essentially means that you’re taking control of the game activity itself. In your case it may not be required, if you only care about the result of a certain portion of UI that is yours and not the game’s. Facebook needs it for some reason. Perhaps to know when the game starts or exits? I don’t claim to understand their code.

If you’ll allow me to quote the first sentence of my first reply:

I still stand by that statement. Cheers! :slight_smile:

1 Like

thanks both for this very educational discussion.

@vladimirg Currently i am implementing the App Indexing plugin in my game , which requires acces to the manifest file, so can you please tell me is it require to extend the Java class with UnityPlayerActivity?