2
votes

I am building an app for iPad composed of two parts:

  1. a user interface built with cocoa
  2. a Unity scene

And I needed to find a way of integrating both parts and have them communicate with each other.

After a lot of research the best approach that I have found was one based on this tutorial

Basically, I have two UIWindows on my app delegate, one where I keep the hierarchy of all cocoa viewcontrollers and the other is the Unity window. When I want to switch which window is visible I just call [someWindow makeKeyAndVisible]; on the target window. When I need to communicate from the cocoa side to Unity I call UnitySendMessage.

This approach seems to work fairly well in the beginning but as I play with my app and after switching between UIWindows a couple of times, the calls to UnitySendMessage randomly stop producing any effects, the Unity scene stops updating frames and it even reaches a point where the windows don't change anymore and until the app explodes...

Basically, what I would like to know is:

  1. Is this the best approach to integrate Unity and cocoa views? Is there an 'official' way of doing this? Can anyone point me out an exhaustive tutorial or sample code?
  2. Being this the best approach, any idea on why does it starts failing after a while? Do I need to take any precautions when changing windows? (memory management? Pausing Unity thread?)

Note: I am using Unity Pro, and I have tried both xcode4/ios6 and xcode5/ios7 and the behavior is the same.

Any help would be much appreciated.

1
On the ObjC side there is a method called UnityPause(bool) that gets called when Unity gains and loses focus. You might want to manually call that method when you are flipping between windows. - Calvin
Thanks for the tip, I already do that. Actually, I thought that could be the issue and tried without doing that keeping Unity running in the background but the issue arises in both cases... Any other ideas? - André S

1 Answers

2
votes

The solution we've come to with AteBit is to have the ObjC portion of the application store its data in our SQLite database. The Unity app is constantly polling that database for changes naturally so it is a great fit for a transparent (no pun intended) UI solution.

Separate your UI completely from your data and get yourself some sushi.

Update

Similarly you could use the NSUserDefaults system, which is accessible inside unity using PlayerPrefs.Get# // Set#