UIImagePickerController : Unity3d

Unity developers may need to access native ios code, I know there is a need since I had it myself. I wanted to use image picker to access pictures from device. How do we allow our game code to make native calls? Plugins.

We will write a simple plugin, which will call native UIImagePickerController from our unity game code. First we will create an objective-c++ code, which would act as an interface. Create a folder named “Plugins” in assets folder. Create folder “iOS” in Plugins folder. We will save our objective-c++ plugin inside iOS folder. Lets give our plugin a name “ImagePickerPlugin”. Structure should look something like this.

unity-plugin-structure

We create a function that brings UIImagePickerController, which will be actual interface to unity game code. We also have a viewcontroller called “PickerDelegate”, that will be a delegate for our UIImagePickerController. Download plugin source

extern UIViewController *UnityGetGLViewController(); // Root view controller of Unity screen.

static PickerDelegate* pickDele=NULL;

extern "C" char* _ImagePickerOpen () {

char* ret = (char*)[(pickDele==NULL ? @"access!!!!" : @"re-access!") UTF8String];

if(pickDele==NULL) pickDele = [[PickerDelegate alloc] init];

UIImagePickerController *imagePicker = [[[UIImagePickerController alloc] init] autorelease];

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {

imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

}else{

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

}

imagePicker.delegate = pickDele;

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {

UIPopoverController *popover = [[[UIPopoverController alloc] initWithContentViewController:imagePicker] autorelease];

[popover presentPopoverFromRect:CGRectZero inView:UnityGetGLViewController().view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

else{

[UnityGetGLViewController() presentModalViewController:imagePicker animated:YES];

}

return [PickerDelegate makeStrCopy:ret];

}

We will create a c# script that will expose our native plugin functions to unity. This script will be a bridge between native plugin and unity. We are wrapping native plugins calls with c# functions. Download plugin source

using UnityEngine;

using System.Collections;

using System.Runtime.InteropServices;

public class ImagePicker : MonoBehaviour
{
private static ImagePicker instance;

// iOS platform implementation.

[DllImport ("__Internal")]

private static extern string _ImagePickerOpen ();

[DllImport ("__Internal")]

private static extern string _ImagePickerGetPath ();

[DllImport ("__Internal")]

private static extern int _ImagePickerGetCalllCount ();

public static string ImagePickerOpen()

{

return _ImagePickerOpen();

}

public static string ImagePickerGetPath()

{

return _ImagePickerGetPath();

}

public static int ImagePickerGetCalllCount()

{

return _ImagePickerGetCalllCount();

}

private static void InstallPlatform()

{

}

}

Now we just need to create an implementation javascript file(TestInterface.js) that will make native calls. For simplicity we will add this script component to our main camera. We will create a simple GUI button and when clicked will call native plugin function to bring image picker.

function Start ()

{

}

function OnGUI ()

{
if (GUI.Button(Rect(10,70,50,30),"Click"))
{
ImagePicker.ImagePickerOpen();
}

}

Download plugin source

5 Comments

  • there is an error in UNITY (Assets/Plugins/TestInterface.js(13,1): BCE0005: Unknown identifier: ‘ImagePicker’.)

    How can i fix that?

  • You need to take the TestInterface.js file outside of the plugins folder so the files inside it compile first.

  • sir, i can open the image picker already but how can i use the image i selected? i want to assign the image into texture2D for a game object.

  • Hello. I really appreciate your work. How could I get the image path after selecting the image?

  • Hello.

    Would like you assistant on implementing the plugin, I manage to insert the plugin into my unity project and make the necessary code to call the “ImagePicker.ImagePickerOpen();”.

    I tried and build the app on xCode but it seem I get an error regarding [super dealloc];

    Below here I list what error that i recieve :

    1. ARC forbids explicit message send of ‘dealloc’

    2. ARC frobids explicit message send of ‘autorelease’

    3. ‘autorelease’ is unavailable : not available in automatic reference counting mode

    How do I fix this error, do i need to import some external framework into my xcode before I build the app?

    Thank you.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>