Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by markusNetural · Aug 10, 2012 at 05:30 PM · iosopengl

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fuel_rhenry · Sep 09, 2013 at 07:36 PM 0
Share

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!

avatar image markusNetural · Sep 11, 2013 at 10:09 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image esraels · Jul 01, 2013 at 07:00 AM 0
Share

This solved my problem! Thank you so much! :)

avatar image halsafar · Dec 17, 2013 at 05:18 PM 0
Share

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.

avatar image halsafar · Dec 17, 2013 at 11:00 PM 0
Share

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.

avatar image markusNetural · Dec 18, 2013 at 09:11 AM 0
Share

I cant add another answer so i just edited the existing one

avatar image halsafar · Dec 18, 2013 at 05:34 PM 0
Share

Thank you very much! This might cure a 3 day headache. Working through it now.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GL ES1 vs ES2 (for iOS) 1 Answer

CPU Usage Not Constant on iOS 11.2 (Metal) in Profiler and Xcode FPS CPU time != Profiler 1 Answer

Issue with glDrawArrays in plugin 2 Answers

Graphics Emulation - OpenGL 3 1 Answer

ARB_sparse_texture support iOS / OSX 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges