Hello Everyone,
I’m trying to get a grasp on developing plugins used when building for the Android platform. Basically I’m at a loss right from the start. The example found in the Unity Docs on this topic suggest that creating a ‘UnityPlayerActivity’ that overrides the original activity is a good practice. Now I’ve found several forum posts that suggest that this will probably result in undesired behavior. They state that swapping activities is not a good practice, because this might interfere when multiple plugins are integrated in one project that do this as well.
I can imagine that this might result into some problems, so I would like to avoid this right from the start. Now I’ve found a tutorial very well explained that discusses ‘Android Fragments’ that can be attached to a 'UnityPlayerActivity". This makes way more sense to me, so I was wondering if anyone has some experience to share on this topic.
If you have another solution, which I didn’t mention (that I should know about) please let me know as well. I’m planning to really dive into this, so any information is greatly appreciated!
Hi,
I ventured here with a very similar query, but hadn’t come across the tutorial you listed above. Fragments do make a lot more sense (and solves some issues I have already had on my current project which have resulted in some dirty lash-ups which offend me!). It is also nice to see it explained in quite some detail how the UnityPlayerActivity works - which confirms my own gut instinct of multiple activities being problematic.
Have you got any further in your own experiments / conclusions? Would be good to hear how you find things are working out, as similar to you, I’m sort of diving head first into developing an Android plugin which will be used across a range of projects, so finding a tidy interface would be nice.
Edit: After just two hours - this works fantastically well by comparison.
Hi,
I haven’t made to much progress the last couple of days due to another project, but I’m planning to get onto it more the coming week. For now the Fragment based plugin seems the best for my purposes, but that is something I hope to find about more next week. When I’ve sorted it out I’ll report back.
@FakeBeard this manual page was rewritten and is planned to be updated next week.
A simple recommendation is to follow whatever Google recommends.
Small addition:
- make your plugin a AAR
- be careful with your manifest, especially with the permissions
Hi,
Back here again, I’ve got some work done on creating my first fragment and attaching this for use within Unity. This all worked pretty nice and was easy to setup. Next my goal was to adjust the fragment in a way so it could authorize itself to make request towards the Google Fit API, which in turn required me to use a different import instead of ‘android.app.Fragment’
This wouldn’t be to troublesome, next to that the process described in the tutorial previously mentions how to attach the Fragment to an Activity gives me the error that it cant resolve the following add method (where instance is an instance of the fragment):
UnityPlayer.currentActivity.getFragmentManager().beginTransaction().add(instance,TAG).commit();
So next I tried to use the Android approach (as mentioned in the documentation) to attach the fragment to the activity. which is comparable. Now my Android plugin is able to compile and turn the native code in a myplugin.aar file which should be usable from within Unity. So I tried calling the function that would attach the Fragment to the Activity, and my log shows the following output:
AndroidJavaException: java.lang.NoSuchMethodError: no non-static method with name=‘AttachFragment’
Which is strange since it is a non-static function. So I’m not quite sure how to proceed from here.
The exception means the method is missing. 
Why? - you have to investigate.
Is it possible to have the fragment filled part of the screen and pass the touches outside it to the below views (Unity’s surfaceview) ?
@Yury-Habets Did you try this kind of setup earlier?