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 : UnityPlayer as a SubView with Transparent Background? - Questions & Answers - Unity Discussions 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: ShareFile Login

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: ShareFile Login
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 UnityPlayer as a SubView with Transparent Background? - Questions & Answers - Unity Discussions)
    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

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

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.

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