- Home /
Unity5 Transparent UnityPLayer view in Android
As part of an AR project for a client, we need to to overlay a UnityPlayer on Android on top of a camera feed from Vuforia. We originally opted to not use the Unity plugin from Vuforia as we also need to shuttle Vuforia image captures to a 3rd party barcode scanning library, and had hoped to only incur the memory and processor footprint of Unity once an AR target was identified by Vuforia. We additionally have native components (views) which we need to overlaid on top of the the unity player.
The general approach we followed is documented here : http://answers.unity3d.com/questions/494767/unityplayer-view-with-transparent-background.html Change the zOrderOnTop flag and the background transparency on the GlSurfaceHolderView, and do use the UnityPlayer as the renderer. Here is a working example of this, where the Red background is visible behind the unity object:
Example Source Code is available here: https://deloitteus.sharefile.com/d-s7080526180a496b9
This approach is working for us on Unity 4.2.2f1 However, in Unity 4.3 and up, Unity changed the architecture so that rendering occurs natively and without the UnityPlayer implementing GLRenderer. We would release our working app on Unity 4.2.2f1, however, it contains libraries have Text relocations which means they can't run on Run level 23 on Android (Marshmallow), which we now need to support.
We hence are exploring the transparent unity view on top of a native Android view on Unity5, which does not suffer from the text relocation issue, and would be the preferable future solution. As the GLSurfaceView renderer is not exposed in this version, we tried reflection to gain access to it, and also tried to use blend modes, although these don't appear to work with SurfaceViews. The non working example of this can be found here: https://deloitteus.sharefile.com/d-s3f9ce2d8e0449119 Attempted Unity5 Transparent background camera approach: (not transparent)
Does anyone have any pointers on how to 1) An approach to overlay a UnityPlayer on-top of a native Android view in Unity 5.3.2f1 or above? (related to http://answers.unity3d.com/questions/494767/unityplayer-view-with-transparent-background.html) What I've tried: a) Using reflection to modify the underlying surface view to set the ZOrderOnTop flag.
b) Using reflection to the modify the underlying surface view background paint c) Changing the blend mode of the layer paint on the underlying surface view.
2) Alternately, anyone have a solution to text relocations on a 4.2.x version which is compatible with Marshmallow?
Target Run Level 22: 07-26 13:09:32.393 8229-8229/? W/linker: /data/app/com.sample.app/lib/arm/libmain.so has text relocations. This is wasting memory and prevents security hardening. Please fix. 07-26 13:09:32.399 8229-8229/? W/linker: /data/app/com.sample.app/lib/arm/libmono.so has text relocations. This is wasting memory and prevents security hardening. Please fix. 07-26 13:09:32.487 8229-8229/? W/linker: /data/app/com.sample.app/lib/arm/libunity.so has text relocations. This is wasting memory and prevents security hardening. Please fix.
Target Run Level 23: Caused by: java.lang.UnsatisfiedLinkError: dlopen failed: /data/app/com.sample.app/lib/arm/libmain.so: has text relocations at java.lang.Runtime.loadLibrary(Runtime.java:372) at java.lang.System.loadLibrary(System.java:1076) at com.unity3d.player.UnityPlayer.a(Unknown Source) at com.unity3d.player.UnityPlayer.(Unknown Source) at com.sample.app.UnityPlayerSubclass.(UnityPlayerSubclass.java:12) at com.sample.app.UnityViewWrapperImpl.setupView(UnityViewWrapperImpl.java:37) at com.sample.app.UnityViewWrapperImpl.(UnityViewWrapperImpl.java:32) at com.sample.app.UnityWrapperImpl.createUnityView(UnityWrapperImpl.java:17)
Thanks, Ben
I have same requirement and want to get a solution with unity 5.x , any new method or comments about it ?
Answer by danw_unity · Aug 15, 2016 at 10:00 PM
Hi Ben,
In general this isn't possible on Android with the latest Unity versions (not sure of which exact version made this impossible). For maximum device compatibility, Unity renders to a buffer and then blits it to the device backbuffer as a final step. The final blit does not support alpha.
If I understand correctly, you want to be using Vuforia, then have the Unity app come to life when you detect some AR target. At that point Unity would be using the Vuforia plugin anyway, correct? I don't quite understand how having the transparent backbuffer in Unity saves memory and performance in this scenario. Could you give me some more details please?
Regards,
Dan
Unity Support
Hey danw_unity,
What Ben meant was: Vuforia and Unity will exists in activity at same time (this is the order Vuforia -> Unity[technically it is GLSurfaceView(transparent BG) with UnityPlayer as its renderer] -> Android views).
Vuforia - which has the camera feed(to scan targets) and unity(since UnityPlayer exposes Renderer in 4.2.2 version, we have a GLSurfaceView with transparent background with UnityPlayer as it renderer) to display some 3D models when a target is identified.
The problem with Unity 5.x version, it doesn't expose its renderer which will be done natively.
I hope its clear now.
We are still looking to find a solution for this. Please let us know if you have any alternative.
Regards, Satish Bodi
I want to overlay UnityPlayer view on top of Native Android view where only the 3D objects being drawn and no camera background (transparent background) with Unity 5.x version Did you have new method about it?thanks
Answer by bingosabi · Jan 10, 2017 at 07:46 AM
We didn't ever figure this out on Unity 5.x on Android. One option which was suggested was purchasing a source code license where you could tinker with the underlying renderers. I expect that this would be prohibitively expensive. You'd need to contact Unity directly to pursue that option.
More broadly, I'd recommend against attempting to embed Unity in a primarily Native app. It's not how it's intended to be used.
Actually, Unity is somewhat restrictive in the grand scope of development and their $$anonymous$$m may not have intentionally remove this capability, so much as they seem unaware how to restore it. By offering the source at a cost, they both avoid trying to restore the capability and profit from those with enough interest in doing it themselves.
You can see from numerous forum posts about this topic that they either ignore requests for how to do it, refer back to using Unity 4.2, or reference using onDisplayChanged. onDisplayChanged will replace the surface that is used by a Unity player, but will also replace the entire rendered content on the surface. Not being able to manipulate the view is just poor design. There is no logical reason to block access other than forcing proprietary restrictions.
I develop my entire application in Android Studio, then import that library into Unity to perform the build. I use a makeshift library from building the Unity project with an empty jar/aar to fill reference requirements for Android Studio's build that is, in turn, put back into Unity. While the Unity contribution to the project improves the visual appeal, it pales in comparison to what is being done in the Android Studio portion. Replicating that in Unity would be tedious, at best.
It is possible to do a lot of things in Unity that aren't advertised or intended, but are highly effective and often more efficient. The number of bugs alone give a good indication that the people designing the editor may not always have a good grasp on how it will be used.
Have you solve this?I encountered a similar problem,thanks
Answer by clementmangin · Aug 13, 2019 at 03:33 PM
For anyone still stuck on this issue, there is a new option in Unity, "Preserve framebuffer alpha", and this post details how it works: https://forum.unity.com/threads/2017-3-0b4-transparency-on-android.499673/#post-3252597
Your answer
Follow this Question
Related Questions
How to translate ArrayList to C# using AndroidJavaClass? 1 Answer
Reading XML Data for andriod 1 Answer
Can't access android mediastore 1 Answer
Buttons touches dont work if touch splash screen 1 Answer
Daydream compatible Unity versions? 0 Answers