Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity player transparent background on iOS

Discussion in 'iOS and tvOS' started by Daan D, Aug 30, 2013.

  1. Daan D

    Daan D

    Joined:
    Aug 26, 2013
    Posts:
    2
    Is it possible to make the background of the unity player transparent on iOS?
    I want to put a native view behind the unity view.

    I was able to do it on Android (using the tutorial in http://www.geekyhamster.com/2013/07/unityplayer-as-subview-with-transparent.html), but haven't been able to find a way to do it on iOS.

    I found a few posts on forums saying it is possible to make the CAEAGLLayer transparent using the following code:
    CAEAGLLayer* layer = (CAEAGLLayer*)eaglLayer;
    layer.opaque = NO;
    layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
    kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
    nil
    ];

    I added this in the OpenEAGL_UnityCallback method of UnityAppController.mm as follows:

    _mainDisplay->surface.layer.opaque = NO;
    _mainDisplay->surface.layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
    kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
    nil
    ];

    And putting the camera background color to (0,0,0,0).

    But it's still not working.
     
  2. Daan D

    Daan D

    Joined:
    Aug 26, 2013
    Posts:
    2
    I found the solution.
    I placed the following code in DisplayManager.mm in the method - (void)recreateSurface:(BOOL)use32bitColor use24bitDepth:(BOOL)use24bitDepth msaaSampleCount:(int)msaaSampleCount renderW:(int)renderW renderH:(int)renderH :

    Code (csharp):
    1. surface.layer.opaque = NO;
    2.     surface.layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
    3.                                                       [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
    4.                                                       kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
    5.                                                       nil
    6.                                                       ];
    And it worked.
     
  3. LucaHidden

    LucaHidden

    Joined:
    May 20, 2013
    Posts:
    1
    Hi Dan and thanks for your post :)

    I tried to do the same. But using the code that is already inside GlesHelper.mm

    Code (csharp):
    1.  
    2. void CreateSystemRenderingSurface(UnityRenderingSurface* surface)
    3. {
    4.     EAGLContextSetCurrentAutoRestore autorestore(surface->context);
    5.     DestroySystemRenderingSurface(surface);
    6.  
    7.     const NSString* colorFormat = surface->use32bitColor ? kEAGLColorFormatRGBA8 : kEAGLColorFormatRGB565;
    8.  
    9.     surface->layer.opaque = NO;
    10.     surface->layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
    11.                                             [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
    12.                                             colorFormat, kEAGLDrawablePropertyColorFormat,
    13.                                             nil
    14.                                         ];
    15. ...
    16.  
    MY background is then transparent. But! I have some weird artifacts with semi-transparent items like the water when in front of a strongly coloured UIView. Which clear flags are you using in your camera? A solid transparent color or depth only?
     
  4. franktinsley

    franktinsley

    Joined:
    Jul 1, 2010
    Posts:
    130
    All I can see that you changed was at "surface->layer.opaque" you changed YES to NO. So I tried this and didn't observe a change. Also tried changing my camera's Clear Flags to depth only and that only made the background black but not transparent.
    Any ideas?
    And did you solve the issue you still had?
    Thanks!
     
  5. Tassman

    Tassman

    Joined:
    Nov 24, 2015
    Posts:
    1