Mixing Unity with Objective-C?

Hi,

Is it possible to mix iOS Objective-C code with Unity generated iOS code?

For example if I am working with a game developer who is developing a game, could I take his xCode project (via Unity) and add interface elements which I code myself using Objective-C Interface Builder etc?

From what I can see this isn’t possible as everything is created via Unity… but hopefully I am wrong…

Thanks!

Anyone?

It might technically be possible, but it would be far more work than porting either Unity’s GUI or your friend’s game.

yes you can do that, if you know about xcode and objective c and the iphone sdk in general.

You can certainly do that. In my latest project I’ve done all of the UI elements in xCode/Objective-c and maintained just the actual “game” bits within Unity.

It takes a bit more in setup time, but once it’s done it actually makes the rest of UI creation and management much easier. For instance, adding in Retina / iPad support is -alot- easier (at least in my opinion.) Retina support / layout essentially comes “for free” when using IB, and simply creating a different xib for iPad is also a lot easier then doing a completely different Unity GUI layout. Also, it’s easy to add in 2D animation to liven up the GUI with CoreAnimation.

Thanks, are there any resources online which show how this can be done? I’m new to Unity so I’m not sure where to start…

I didn’t find anything that really clearly laid it out for me; I just struggled through the example project that’s up on the Unity site.

When it’s all said and done (and in retrospect), the example project does show you what to do, but it took me quite awhile to figure out how all the pieces worked together. For whatever reason, I was just being dim that day, I think.

The basics are:
From your Unity app, you need to call a C function:

...
using System.Runtime.InteropServices;

public class MainMenuBinding : MonoBehaviour
{

    [DllImport("__Internal")]    
    private static extern void _levelStartMenu();    
    public static void levelStartMenu()
    {
    	if( Application.platform != RuntimePlatform.OSXEditor )
    		_levelStartMenu();
    }

So from Unity, I call MainMenuBinding.levelStartMenu(), This then would talk to the function _levelStartMenu in your xCode project that has something like this in it:

extern "C" {
	
	void _levelStartMenu()
	{
		[[MainMenuBinding sharedManager] ShowLevelStartMenu]; // and this gets you into your Obj-c world...
	}

}

(I’ve named both classes that communicates from one environment to another “MainMenuBinding”. I (don’t think) that there’s any reason you need to have them both named the same thing.)

From there, you can then either overlay your own windows, view controllers, etc. on top of the running Unity code (pausing or not pausing Unity by defining “void UnityPause( bool pause );” in your class and using that.

To send a message to Unity from Obj-c, then you use:

void UnitySendMessage( const char * className, const char * methodName, const char * param );
...
...
UnitySendMessage( "UnityCSharpClassName" , "UnityMethod", "WhateverValueToSendToUnity");

I hope that shows all the right steps… at this point I have a somewhat template-d system in place so it’s easy for me to go back and forth between the two, but I may have forgotten a step or two that I did initially in there, though.

Thanks again for the help! I discovered this page which outlines a similar process to what you have explained: Unity - Manual: Plug-ins

One thing I am not clear on, what file names do you give the .h and .mm files that contain the _levelStartMenu function? And how does Unity know where these are?

From what I understand these .h and .mm files are added manually to the xCode classes directory after Unity exports the project, am I right?

Try this to get you started

Or, if you want to do native plugins yourself I found this a useful little tutorial on where to start for OSX http://www.reigndesign.com/blog/unity-native-plugins-os-x/

Just reading through this, might be helpful : Blog — Jon Trainer

Prime31, the link you supplied doesn’t work anymore. Do you still have this example somewhere for reference on how to make native calls for iOS and Android?

Thanks.

We are debating whether or not to open up all of UIKit as a higher priority. Our Unity3D plugin allows you to access the iOS SDK. Currently, our API allows you to create UIView, some labels, and webkit (next release) using C#. I know OP wants to mix ObjC and Unity but I just want to let you her/him know of an alternative.

Universal plugin for unity for iOS that allow mixing C# with objective-C

Unity C#

var callbackClass = AKiOSMagic.CreateClass("MyCallbackClass", "NSObject");
callbackClass.AddMethod("methodWithArg:anotherArg:", (args)
{
    // do something...
    // args.GetObject(0);
    // args.GetObject(1);
});
callbackClass.RegisterClass();

Objective-C

[[NSClassFromString(@"MyCallbackClass") new] methodWithArg:@"arg1" anotherArg:@"arg2"]

@alex_k

I am interested in you package.
Can this plugin be used for retrieving and saving video files?

Thanks.