Search Unity

Question Unity3D export to Android with transparent background?

Discussion in 'Android' started by ywj7931, Jan 11, 2018.

  1. ywj7931

    ywj7931

    Joined:
    Jun 24, 2015
    Posts:
    41
    I am trying to export my unity project to other android projects, my Unity content will be an avatar, I am displaying my Unity content on a FrameLayout/RelativeLayout above another layout, is there anyway I can make my avatar's background transparent? ( so that I can drag the avatar but still can see the layout on the background. a

    For example: How to achieve the following? Right now,



    Right now, its whatever background from Unity3D (sorry for my poor drawing):

     
  2. ywj7931

    ywj7931

    Joined:
    Jun 24, 2015
    Posts:
    41
    Should I set clear flag on the camera some how?
     
  3. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    This should be possible with Unity 2018.1.
    In Android player settings, check the "Preserve frame buffer alpha" flag, and set the camera to clear color with alpha set to 0.
    Keep in mind that at the moment it works with OpenGL ES only.
     
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Also works with 2017.3.0f3. Camera background color should be 0,0,0,0 though, otherwise the background was tinted for me.
     
  5. ywj7931

    ywj7931

    Joined:
    Jun 24, 2015
    Posts:
    41
    Thanks for replying! I tried but seemed not working, whats the setting I need to set for camera?
     
  6. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    Open camera settings, select "Solid Color" in "Clear Flags" and make sure the color has alpha set to 0. This should do the trick if the "Preserve Framebuffer Alpha" checkbox is set.
     
  7. ywj7931

    ywj7931

    Joined:
    Jun 24, 2015
    Posts:
    41
    Thanks, can I ask which android build file store this information?
    Is it assets/bin? Unityclass.jar?
     
  8. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    You're welcome :)
    I'm not sure I can answer this. The camera settings would definitely be in some place different from the android player settings.
     
  9. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    You're unlikely to be able to patch it - it's serialized in the scene file. Change the settings and rebuild the project instead.
     
  10. ywj7931

    ywj7931

    Joined:
    Jun 24, 2015
    Posts:
    41
    I am just curious what files I need to replace every build, right now I replace assets/bin, libs/unity-classes.jar, jniLibs files,
    1.If I have built once, and make some scene change, do I just need to replace assets/bin again?
    2.If I have built once and make some project settings and build settings, should I replace all?
     
  11. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    Is there a reason why you can't build a new APK in the Editor and use it?
     
  12. ywj7931

    ywj7931

    Joined:
    Jun 24, 2015
    Posts:
    41
    Since I still heavily use android development, unity is only for viewing some model
     
  13. iago-suarez

    iago-suarez

    Joined:
    Jan 19, 2017
    Posts:
    1
    It works!! You are a genius!!
     
    Yury-Habets likes this.
  14. SharkyLV

    SharkyLV

    Joined:
    Jul 16, 2018
    Posts:
    4
    Hi Aleksandrk,

    Has anything changed since? I am using 2018.2 with preserved alpha buffer and 0,0,0,0 rgba for the camera
    - if keep the "windowIsTranslucent: true" setting in Android when exported, the UnityPlayer keeps the transparency, but it is transparent through all the app views (like a hole through to the desktop)


    - if I remove "windowIsTranslucent: true", then the background becomes black

     
  15. braltan

    braltan

    Joined:
    Jun 2, 2017
    Posts:
    1
    I have the same issue did you find a workaround with this ?
     
  16. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    The only intended use case for having "Preserve frame buffer alpha" enabled is when you want to render some content with Unity on top of some native Android UI in the same application.

    That said, it works properly and there is no workaround needed. Just disable the checkmark.
     
  17. SharkyLV

    SharkyLV

    Joined:
    Jul 16, 2018
    Posts:
    4
    So it seems that UnityPlayer on Android is using a SurfaceView which displays the content in a separate "window" and has no view hierarchy with other views in your activity. Therefore you cannot overlay with other UI elements.

    There was a hack somewhere on GitHub I found that overrides some internals of UnityPlayer for it to use TextureView instead. TextureView is a normal view hierarchy member and can be animated, or overlayed with other UI views.

    The problem, however, (and the reason why they chose SurfaceView) is as soon as a view in a view hierarchy updates, it propagates an update/redraw to other other views in the hierarchy (up to the root view). UnityPlayer updates many times per second, prompting other views to update many times per second, therefore resulting in a very slow fps.

    There might be a way to isolate the updates of the UnityPlayer and do not propagate them to other views, but if you have an overlay view on top of or below the UnityPlayer, I don't see how it's possible not to redraw them.

    TLDR; Unity uses SurfaceView which uses a separate "window" and is not part of your activity view hierarchy.
     
  18. hail_miki

    hail_miki

    Joined:
    Jul 17, 2018
    Posts:
    40
    SIR! JUST WANTED TO LET YOU KNOW WHAT A MASSIVE LEGEND YOU ARE!
    my BUTTONS AND OTHER PANEL WAS SHOWING ME MY DAMN BACKGROUND APPS AND FOR THE LIFE OF ME , I WASTED WEEKS PLAYING WITH PANELS AND IMAGES LOSING HAIR! AND THIS FIXED IT ! JUST DISABLE THIS THING AND ITS NOW FIXED! (YES I KNOW ITS CAPS!!!!) THANK YOU LEGENDARY BEING!
     
  19. anuragbhandari

    anuragbhandari

    Joined:
    Jan 18, 2019
    Posts:
    1
    Pardon me for bumping into this rather old thread. We are faced with a similar task. This solution did not work for us. We are using Unity3D 2018.1.3f1.

    Our task is simple. We have a scene with camera attached. We want to load this scene on top of our Android app's live camera view (which is a Fragment loaded inside a RelativeLayout). When scene actually loads, it does so with a black background and covers the entire camera view. Rest everything works in the scene itself once it's loaded. We'd just like to get rid of the background by somehow making it transparent so that the live camera view behind it becomes visible.

    Setting "Clear Flags" to "Solid Color" (with alpha 0) has the following effect. The scene loads with the set solid color background, and within fraction of a second the background turns black and stays that way. This is with "Preserve Framebuffer Alpha" checked in Unity.
     
    Last edited: Jan 18, 2019
  20. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    When I was implementing this, I was modifying the gradle project exported by unity to show additional native Android UI underneath the Unity view. I'm not sure if any other configuration will work properly :)
     
    anuragbhandari likes this.
  21. infi2019

    infi2019

    Joined:
    Jan 24, 2019
    Posts:
    1
    Have you solve the problem?
     
  22. ywj7931

    ywj7931

    Joined:
    Jun 24, 2015
    Posts:
    41
    Hi, Does it work with iOS. Any solution to make it transparent in iOS?
     
  23. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    @ywj7931 yes, the same checkbox (it was renamed to "Render over native UI").
     
    ywj7931 likes this.
  24. Cliwo

    Cliwo

    Joined:
    Oct 12, 2016
    Posts:
    12
    Hi, I'm sorry to call you since quite some time passed.
    I want to make something like you wanted but I think my result is quite different with yours.
    Can you explain me what you made with more details? (ex. screenshot(?))

    For now my app is like the image I uploaded.
    top of screen, it shows unity view with 'main menu' not my activity. (it is like a hollow)
    and below that, my activity comes

    Thank you
     

    Attached Files:

  25. raviIOS

    raviIOS

    Joined:
    Mar 27, 2019
    Posts:
    13
    Still iam unable to get cleared color background of the unityView. Please any one could help to get the clear color backround of the unityView when integrating in the ios application.
     
  26. raviIOS

    raviIOS

    Joined:
    Mar 27, 2019
    Posts:
    13
    @aleksandrk please could you help us on the transparent background of the unity while implementing in the native iOS for the 2018.0.3f2 version. It is still not changing the color while above settings is turned on. Could you help me know where is the background color implementation is written in the classes exported by the unity where we can check by changing the color of the background and get the result.
     
  27. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    Can you post a screenshot of your camera settings and of "resolution and presentation options" from the player settings?
     
  28. raviIOS

    raviIOS

    Joined:
    Mar 27, 2019
    Posts:
    13
    @aleksandrk :this is the screenshot with the latest 2019 version of the unity as the option of the "Render over native UI" is available in this unity version.
     

    Attached Files:

  29. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    Is this the only camera that renders to the screen?
     
  30. raviIOS

    raviIOS

    Joined:
    Mar 27, 2019
    Posts:
    13
    @aleksandrk: Yes, this is the only camera setting. Attaching the MethalHelper.mm file also which we get after exporting the Project in ios environment
     

    Attached Files:

  31. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    Line 69 in MetalHelper.mm sets the background color for the layer. When Unity renders a frame, it's alpha-blended with the underlying view.
    I'm not sure, what exactly are you trying to achieve and what's not working :)
     
  32. raviIOS

    raviIOS

    Joined:
    Mar 27, 2019
    Posts:
    13
    >>>> I need the background of the unityview to be transparent so that the rendered view looks like the part of the view. Object should be visible as attached with the screenshot. In the POC, the background white color should be transparent so that the car seems like the part of the background view.
     

    Attached Files:

  33. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    It should work :)
    Please make a bugreport.
     
  34. Vishakh_A_V

    Vishakh_A_V

    Joined:
    Nov 26, 2015
    Posts:
    5
    Upon research, I found that the SurfaceView is rendered at the last of the window different from the view hierarchy and that it is possible to move the SurfaceView to the top by using SetZOrderOnTop(true) on SurfaceView during the time of instantiation. A sample code would be to modify the onCreate of UnityPlayerActivity -
    @override protected void onCreate(Bundle savedInstanceState)
    {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);

    mUnityPlayer = new UnityPlayer(this);
    try{
    UnityPlayer view = (UnityPlayer)mUnityPlayer.getView();
    Field f = view.getClass().getDeclaredField("s");
    f.setAccessible(true);
    SurfaceView sf = (SurfaceView) f.get(view);
    sf.setZOrderOnTop(true);
    }catch(Exception e){
    }

    setContentView(mUnityPlayer);
    mUnityPlayer.requestFocus();
    }
    The transparent pixels that were causing a hole to the desktop could be fixed by creating a dummy background view and adding it to UnityPlayer's FrameLayout.
    However, the downfall to this would be that other system overlays by plugins like AdMob will not be able to render above UnityGame(SurfaceView).
    Also I found that custom view behind transparent area(unity game) was found consuming touch events and unity game on transparent area couldn't capture touch events!
     
  35. raviIOS

    raviIOS

    Joined:
    Mar 27, 2019
    Posts:
    13
  36. Umresh

    Umresh

    Joined:
    Oct 14, 2013
    Posts:
    56
    Does this work on webgl builds?
     
  37. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    @Umresh no, it's Android and iOS only.
     
  38. mokhabadi

    mokhabadi

    Joined:
    Jul 25, 2018
    Posts:
    28
    Hi! I'm trying to create a transparent app using unity but i can't. i created an empty 3D project in unity 2019.4.9f1. i set the camera clear flags to transparent black (0,0,0,0), i set the "Render over native UI', i removed Vulkan, i built for android and tested on my android 10 phone. it is not transparent. can you please tell me what is wrong? Thanks in advance!
     
  39. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    Hi!
    Can you please report a bug?
    Thanks!
     
  40. RDudnikovTangoMe

    RDudnikovTangoMe

    Joined:
    Oct 8, 2020
    Posts:
    4
    I've checked a lot of unity versions so I think 2019.2.9f1 is most newest and stable for this issue until it fixed in 2020 versions.

    BTW I've reported a bug.

    Preset 2D
    Default scene with camera’s clear flags is set to Solid Color (0,0,0,0)
    Start in fullscreen mode is unchecked (if you have this option in the editor, not sure that affects)
    Preserve frame-buffer alpha is checked (Render over native UI)
    Disable splash screen (not sure that affects)
    Not worked
    2020.1.6f1
    2019.4.12f1 (with or without Vulkan)
    2019.3.15f1 (with or without Vulkan)
    2019.3.0b4
    2018.4.28f1
    2018.4.21f1
    2018.4.17f1
    2018.4.15f1
    Worked
    2019.2.9f1
    2018.4.14f1
    2018.3.14f1
    2018.2.21f1
     
  41. mokhabadi

    mokhabadi

    Joined:
    Jul 25, 2018
    Posts:
    28
    Thank you very much!
     
  42. RDudnikovTangoMe

    RDudnikovTangoMe

    Joined:
    Oct 8, 2020
    Posts:
    4
    Got it working on iOS by following this steps and adding the next code
    Code (CSharp):
    1. - (void)preStartUnity {
    2.   self.window.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0];
    3. }
    2020.1.9f1
     
  43. A2gaming

    A2gaming

    Joined:
    May 21, 2021
    Posts:
    2
    i made the background transparent but i want to make it so that you can control your phone while the app is open
    but only where there are no ui elements such as, the app is open, the background is transparent and i was on the home screen opening the app... now i want to swipe to go to a different page of the home screen.

    How can i Do This?
     
  44. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    You can't do that, afaik.
     
  45. A2gaming

    A2gaming

    Joined:
    May 21, 2021
    Posts:
    2
  46. Lulliflo

    Lulliflo

    Joined:
    Sep 17, 2017
    Posts:
    4
    Hi, is it possible to achieve this with Unity 2019.4.31f1, cannot find the "Preserve frame buffer alpha" option.
    Thanks
     
  47. itenot2

    itenot2

    Joined:
    May 21, 2022
    Posts:
    1
    Doesn't work in 2022.1.8.f1
    Exported as android project, made fixes in manifests and styles.xml but it doesn't help
     
  48. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    Hi do you guys know how i could filter touches? Like if the user clicks on a button it registers it and if he or she clicks on the transparent parts, then the interaction is on the app on the background?
     
  49. h824612113

    h824612113

    Joined:
    Jun 23, 2021
    Posts:
    1
    Unity2021.3.11f1
    1.I'm Success by Click upload_2022-10-18_16-8-26.png
    2.Change Default theme upload_2022-10-18_16-9-6.png
    3.build apk
     
  50. Sansonight

    Sansonight

    Joined:
    Mar 9, 2021
    Posts:
    8
    I managed to render the unity frame inside a react native application with transparent background, despite htat this take me to 'broke' the application because of AndroidManifest merge conflict during building. The conflict happen for the android:theme attribute: the base AppTheme and the Unity.Translucent can't coexist in <application>. So i replace the AppTheme with Translucent in AndroidManifest and everything works except for the app boot where it appears transparent before loading the view.

    This is the merged manifest after replace. blue section is UnityManifest and red seciont is React Native manifest.
     

    Attached Files: