Home > database >  Screencapture of Unity AR view from native iOS
Screencapture of Unity AR view from native iOS

Time:10-04

I am trying to take a screenshot of my Unity view from the native iOS. My native iOS has been written in Xamarin.iOS

When a button click event fires on unity view, my native app fires a notification and callig a method in native side. Inside this native method I am trying to capture the screenshot of my current view(Unity AR view). This is my xamarin code to take the screenshot.

            UnityFramework _ufw;
            _view = _ufw.AppController().RootView;
            var mainLayer = _view.Window.Layer;
            
            mainLayer.RenderInContext(UIGraphics.GetCurrentContext());
            var img = UIGraphics.GetImageFromCurrentImageContext();
            img.SaveToPhotosAlbum((iRef, status) =>
            {
                if (status != null)
                {
                    new UIAlertView("Problem", status.ToString(), null, "OK", null).Show();
                }
                else
                {
                    new UIAlertView("Saved", "Saved", null, "OK", null).Show();
                }
            });
        }
        finally
        {
            UIGraphics.EndImageContext();
        }

My problem is it doesn't capture my AR view. Instead, it just capture a white view. I want to capture my Unity view which has some overlayed items on a camera view. How could I do this? Thank you!

CodePudding user response:

This post helped me. So I replaced my below lnes

var mainLayer = _view.Window.Layer;         
mainLayer.RenderInContext(UIGraphics.GetCurrentContext());

With

UIGraphics.BeginImageContextWithOptions(_view.Frame.Size, false, 0f);
_view.DrawViewHierarchy(_view.Frame, true);

Works like a charm

  • Related