- Home /
How to combine Unity and Native OpenGL in iOS (Sparrow)?
Hi there,
i want to combine Unity with a native 2D Drawing View which uses OpenGL (that is the Sparrow Framework). I successfully build an app where a native GUI loads the Unity view, with an Overlay to switch back to the native GUI. Within the native GUI the OpenGL view works fine. After the first start of the UnityView, Unity also works well. But after switching back to the native OpenGL (which works fine) and then switching to Unity again, the unity screen doesnt update. I know that Unity is still running and "working" from debug outputs, there is just no redraw.
I already tried calling GL.InvalidateState()... didn't solve the problem :(
So my question: is it possible to combine native OpenGL Drawing with Unity on iOS? And if so: what need to be done?
thx for any hints and replys.
Could I ask you to elaborate on how you managed to create a view that loads the Unity player on demand? I've been looking for a solution to do that but have been running into a bunch of issues. So far I've managed to create an initial view that will simply load the Unity view when a button is pressed (and switching delegates at that time) but Unity crashes during the loading process.
Thanks!
I bet you are working with Unity 4.2. I did this on Unity 3.5 when things where a bit different. For updating to 4.2 i need to look into it again myself. I try to keep you informed as soon as i get it working.
Answer by markusNetural · Aug 13, 2012 at 12:57 PM
Ok, i kinda got it myself. Originally i started the native app in its own window, then (later in the app life) started the appController by initializing it and calling "didFinishLaunching..". This creates a second window with the unity stuff and one can easily switch between native and unity. But OpenGL seems not really happy with this solution.
Here is what i did:
Now there is only the unity-window (My own AppDelegate directly calls AppControllers launching method) where i put my native stuff on top by letting the UnityGLViewController present my native ViewController modally. Additionally i call applicationWillResignActive and applicationDidEnterBackground before i "switch" to the native stuff. Therefore Unity stopps drawing and is not interfering with my Native OpenGL.
After starting the app (in "Unitymode") i save the GLContext to a member variable (via [EAGLContext currentContext]). Before switching back to Unity i "restore" the context by [EAGLContext setCurrentContext:unityContext]; (where unityContext is the member-variable). Then i call applicationWillEnterForeground and applicationDidBecomeActive and dismiss the modal view controller.
This way everything works perfectly fine. I dont even need GL.invalidateState. Both the Unity and the Sparrow-View are only paused (or stopped for Sparrow) when they are not visible so they not disturb each other.
I dont know if its possible to do it with seperate windows (so one can start the unity-player in the background and dont have to wait on startup), but it works this way.
UPDATED VERSION FOR UNITY 4.3
Here is some edited Version as i am doing it for Unity 4.3 at the moment: I have a lot of native iOS stuff and therefor didnt derive from UnityAppController but use my own AppDelegate which i use in main.mm My own AppDelegate creates an instance of the UnityAppController and handles the switch between Unity and Native Code. Therefore i had to change some Unitycode: In UnityAppController i changed GetAppController() to return the UnityAppController stored within my own AppDelegate
I think the startup progress changed with Unity 4.2 because it was no longer possible to just switch to native after applicationDidFinishLaunching... so i added a Callback at the end of OnUnityReady() withing iPhone_View.mm. In my own applicationDidFinishLaunching... i do as follows: -create my own view hierachy but no own window. -create the UnityAppController and call didFinischLaunching on it - use the window returned by UnityGetMainWindow() but not setting the rootViewController yet - wait till the callback in OnUnityReady() is called, then set the rootViewController to my own viewhierachy (after sending WillResignActive and DidEnterBackground to the UnityAppController)
It is necessary that no OpenGL stuff is executed during the startup until unity is done. Otherwise the context and framebuffers etc. got messed up.
regarding the context: its no longer necessary to save the context yourself cause unity already does that. i just make sure to set it when switching back to unity (before calling willEnterForeground but after setting the rootViewcontroller back to unityAppController:
[EAGLContext setCurrentContext:[DisplayManager Instance].mainDisplay->surface.context];
Hope this helps markus
I would love if you could elaborate more on this. Specifically starting in Unitymode. I have just derived my app delegate from the UnityAppController. Synthesize _window = window. However I get some weird results such as my app loads quickly but then stalls for 30s after launching before giving me OpenGL Context back. $$anonymous$$y app is a dual native ios opengl + unity3d app.
Saving the GLContext seems to be my issue now. At which point can you save the unity gl context? $$anonymous$$y app is crashing on gl calls from unity so I am assu$$anonymous$$g the context error.
I cant add another answer so i just edited the existing one
Thank you very much! This might cure a 3 day headache. Working through it now.