Native iOS and Unity integration orientation change overwrites rootViewController

Hi all,

I’m integrating a unity view as part of a larger iOS application. I’m using Unity (5.1.2.f1) and xcode (7.0 beta 6) and have done a little work because Swift is used for the larger iOS application. All was working well with that.

However, whenever the orientation of the unity view is changed, whether through setting Screen.orientation in unity in CS or by invoking functions from UnityAppController+ViewHandling.h (like orientView, orientUnity), the unity view becomes fullscreen and it looks like this is a result of rootViewController getting reassigned. I have assigned the rootViewController over to another viewcontroller (part of the larger ios app). Any ideas on how to prevent this, or control the unity view’s orientation without that occurring?

Furthermore, leaving the unity view to autorotation seems to base its orientation on the width and height of the unityViewController’s view. Why is this?

Lastly, note that if I don’t explicitly try to force orientation (and just let autorotate do its thing) then I don’t have the problem with the rootViewController getting reassigned and the unity view going fullscreen, but instead have the orientation of the unity view dependent on the width and height of the view… also seems to arbitrarily decide to be portrait or portraitUpsideDown if w > h and landscape and landscapeUpsideDown if h > w.

Here’s some example code in the app delegate that reassignes the rootViewContoller to DrawerControllerNavigation.

unityView = window.rootViewController?.view

        let uiviewControl = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("DrawerControllerNavigation")
        window.rootViewController = uiviewControl
1 Like

because this is “natural” - as in - if your width is greater than height it is landscape orient. What you seem to miss, is that if you plug unity view in your hierarchy than it is kinda normal that unity orientation is different from “device” orientation. I would gladly get rid of “orientation” concept altogether, but for some reason people are very fond of it and we need to make sure that we keep things sane (e.g. imagine you have different ui layout for landscape/portrait - you care about view being “more wide” vs “more tall” not about device orientation per-se)

yes this is expected - check comments in UI/UnityAppController+ViewHandling.h and implementation details in UI/UnityAppController+ViewHandling.mm. Essentially if you want to have custom controller - you want to override some methods and provide your custom one (instead of default). Also you might find some help there https://bitbucket.org/Unity-Technologies/iosnativecodesamples as there are some samples of customizing view hierarchy

1 Like

Thanks for your reply.

I noticed that my problem occurs even when not changing physical orientation, but simply resizing the view by changing it’s width/height.

UnityReportResizeView() that gets called in UI/UnityView.mm:

_curOrientation = (ScreenOrientation)UnityReportResizeView(renderSize.width, renderSize.height, self.contentOrientation);

I’m not sure what UnityReportResizeView is doing exactly, but it seem to take into account width and height when setting the orientation. For example, if the phone is in portrait, but the viewport’s width is longer than it’s height (say non-fullscreen mode, a 16:9 viewport at top of screen) the orientation of the view will be sideways.

Any way to prevent this? Or do I need to add my own logic that takes into account viewport size and pass in the corresponding orientation?

As Alexy already said it makes sense to have landscape view when the scene view is wider then the scene heigh even if your app is in portrait mode. Maybe it would be possible to provide some images/gifs to show what you actually trying to do, just to make sure you ask the right questions and we can deliver the right answer. Did you already have a look into the iOS Native Code Samples?