Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by lukas77i · Sep 10, 2011 at 06:57 PM · iospluginscreenxcodenative

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

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 Peter G · Sep 10, 2011 at 07:11 PM 0
Share

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.

avatar image lukas77i · Sep 10, 2011 at 07:20 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

  1. 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*.

  2. 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];
              }
          }
    
    
  3. 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.

  4. 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;
         }
    
    
  5. 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];
           }
    
    
  6. 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.

Comment
Add comment · Show 3 · 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 lukas77i · Sep 11, 2011 at 08:01 AM 0
Share

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.

avatar image BTamas · Oct 04, 2012 at 12:48 PM 0
Share

Hello

On iOS6, this code dies at this line: self.view = view;

Bad access

Any idaes?

avatar image PDZ · May 29, 2014 at 11:42 AM 0
Share

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.

avatar image
0

Answer by Leopard · Oct 25, 2012 at 06:34 AM

who successful? given me a sample code ,help!!!

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

How do I remove Google Plus from iOS Build only? 1 Answer

Unity Facebook Plugin terminates app after login (iOS) 1 Answer

ios plugin Undefined symbols for architecture i386 0 Answers

iOS - Mimic Native Layout 0 Answers

Xcode error after upgrading Unity project from 2019.2.0 to 2019.3.11 0 Answers


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