- Home /
Creating a native photo picker screen on iOS
Hi,
I have searched all around but I still don't understand how I can create a working native plugin to open and use the photo picker on the iOS.
I have also tried the Native Toolkit available from asset store but I still don't know how to do it. I'm not experienced with xcode.
I would really appreciate any ready code snippets or steps on how to do it exactly.
PS: I do not wish to buy any plugins because I only need this single function.
Regards
I can basically tell you everything that you'll need to make it including UI classes and interfacing with Unity as well as a step by step guide, but its quite a bit of work to code it all for you.
Awesome, thanks for the reply. I don't need something very complex but I would massively appreciate if you could take me through how I can get a simple xib screen display in unity and basically just a quick guide on how to get started.
I checked out the unity sample project, but it doesn't explain it at all. I'm just looking for a simple starter that I could extend on.
Thanks
Answer by Peter G · Sep 11, 2011 at 01:09 AM
Ok, so I need a system update to look at my old code that did this so I'm going to tell you from memory and you can come back if I missed something. Let me also say this is a good opportunity to create you main menu in Interface Builder as well since you will need a view controller.
First, let's give a real brief explanation of some important UI things in Xcode. You have the UIApplication. This is the class that manages all the states in your application. The UIApplicationDelegate Protocol (protocols in Obj-C are similar to interfaces in C#) has a list of messages that will be sent to any class that implements it. The application has all the windows known as UIWindows that are responsible for distributing messages to the various views on the screen. These UIViews are spaces on the screen. If you've ever used Photoshop, the whole piece is like the window and the layers are like UIViews. That example isn't perfect since you can have multiple windows, but it gets the idea across.
Now its time for the UIViewController. ViewControllers are managers for views. The control when views appear, how views transition, and manage more complex features. The specific view controller you will need is the UIImagePickerController. It takes your current view controller and hides it while displaying its own on top of it. It's important to note that the Unity app doesn't create a view controller. Everything is done in the Unity Engine. It doesn't change views or have any transitional behavior so it never creates a view controller. What's this mean for us? We have to create a view controller and assign the Unity view to it.
So let's go through the steps you will need to take to get here.
You will need to create a plugin class. You can name it whatever you want, but for clarities sake we will name it Plugin.mm. It's important that its .mm that's Objective-C++ and that's important for compilation and working with Unity. Spend a little time learning your way around Obj-C syntax including the Interfaces and Implementations. Variables and properties go in interfaces and you will be able to mimic other examples almost verbatim. Its basic stuff so can probably figure it quickly from some example code. You need to inherit from UIViewController. You will also need to implement the UIImagePickerControllerDelegate. You'll also need a UIView*.
You need to instance your class. There are a number of ways to do this. An easy way is to do it just like the example plugin project does. Create a null pointer to your class and then instance it to a class from Unity to your plugin (manage -> unmanaged call). Something like:
static Plugin* instance = null; extern "c" { void Instance () { if(instance == null) instance = [[Plugin alloc] init]; } }
I don't know how much Objective-C you know, but that's a pretty standard way of creating an object. Most objects get two calls: alloc which creates space for the object and init which is like a constructor. There are many variations of the init that takes all sorts of arguments depending on the object.
Now its time to set up your object's init function. I want to thank Matt at Blurst for some of this code here. You can find his original post here.
-(Plugin*) init { self = [[super alloc] init]; UIWindow* main = [[UIApplication main] keyWindow]; //find the main window in the app. for(UIView* view in main.subviews) { //iterate through all its sub views to find one that is like the Unity window. if(view.exclusiveTouch && view.multipleTouchEnabled) { pluginView = view; //plugin view is the UIView* declared in your interface self.view = view; } } return self; }
Now you need some way of telling Xcode to open your view controller's image picker. Create a function in Unity that calls your objective-c function.
extern "C" { void PresentImagePicker () { [plugin presentImagePicker]; } } then your Plugin class needs a method called `presentImagePicker`. -(void) presentImagePicker { UIImagePickerController* imagePicker = [[UIImagePicker alloc] init]; imagePicker.delegate = self; pluginView.exclusiveTouch = NO; [self presentModalViewController:imagePicker animated:YES]; [imagePicker release]; }
That should pretty much display your image picker. The controller sends messages to its delegate. In this case, that's this object. All the messages you can respond to are here. When the user clicks and image, the picker sends a message to the delegate with the image chosen. You can do stuff with that image, and you are expected to dismiss the image controller.
[self dismissModalViewController]
.
I don't know what else you want to do with the image so I can't go to much further than that. That's a basic guide on how to bring the UIImagePickerController into your Unity plugin. I'm pretty sure there are a few mistakes. I haven't done this in a while, and as I said at the top, I can't get into my old files right now. Most of the problems come from the Unity view. the Unity view is a GL view. Simply put, it wants to control the screen. It will sometimes draw itself over other views even if its below them. But I'll try and fix those as you come across them. For now, this should keep you busy for a little while.
Thanks Peter. This will surely keep me busy for a while. I'll go through it and try to get it working. I'll come back if I come across some problems. BTW, I'll probably have to study some obj C too; I'm only familiar with c sharp and unity script.
Hello
On iOS6, this code dies at this line: self.view = view;
Bad access
Any idaes?
I can't believe you held lukas77i's hand through the whole problem and he didn't have the decency to give you a 1up or a correct answer mark.