Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Hakimo · Jan 25, 2013 at 04:24 AM · screenshotnativeobjective-c

Passing a value from Unity to Objective C

Hi, from the title I'm trying to pass a value in this case a file from using Application.CaptureScreenshot which is a string type. The capture screen function is in a js script so I pass the value to cs code. Here's what I have so far:

     //saveToAlbum.cs

     private CaptureScreen captureScreen;
     
     [DllImport("__Import")]
     extern static public string savePicture(string picture);
     private string temp;
     
     void Awake()  {  
         //Get the JavaScript component  
         captureScreen = this.GetComponent<CaptureScreen>(); 
     }  
     
     void Update()    {
         if(captureScreen.press == true)    {
             //print(captureScreen.picName);
             temp = savePicture(captureScreen.picName);
         }
     }

It builds fine but now the tricky part for me in calling it in Objective C. So I wrote this:

 // MyApplication.h

 extern "C"  {
     NSString *savePicture(NSString *picture);
 }

and

 // MyApplication.mm 

 NSString *savePicture(NSString *picture)  {
     NSLog(@ "@", picture);
     
     return picture;
 }

At the moment, this is just to test printing out the names of the pictures. Whenever i run this, xcode crashes and has this in the output console "Unable to resolve pinvoke method"

What am I doing wrong or is there a better way of doing this? If this works I'm hoping to use UIImageWriteToSavedPhotosAlbum.

Appreciate the help, thanks very much.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by DeveshPandey · Jan 25, 2013 at 05:15 AM

Hi Hakimo,

Remember me?

Anyway the problem is, you call a objective c function by passing a string, but I can see you don't change the received string to NSString.

Use this function to change your string to NSString

 NSString* CreateNSString (const char* string)
 {
   if (string)
     return [NSString stringWithUTF8String: string];
   else
         return [NSString stringWithUTF8String: ""];
 }
 
 and modify your function as:
 extern "C"  {
  void savePicture(const char* picture)
  {
     NSLog(@"Hi i am in objective c---",CreateNSString(picture));
  }
 }

Note : Save this file with .mm extention

Comment
Add comment · Show 12 · 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 DeveshPandey · Jan 25, 2013 at 11:08 AM 1
Share

Dear Hakimo, please see my previous posts then you get the answer why x-code is going to crash. The reason is native function are only worked on actual device not on the simulator or etc.

See this : http://docs.unity3d.com/Documentation/$$anonymous$$anual/PluginsForIOS.html

Using Your Plugin from C# : here is the answer

avatar image Hakimo · Jan 30, 2013 at 04:14 AM 1
Share

Hi Devesh, apparently I've made a huge arse of myself. I just noticed what was giving me the errors and it's one of the first few lines. I placed "[DllImport("__Import")]" ins$$anonymous$$d "[DllImport("__Internal")]". I guess my $$anonymous$$d was someplace else when i was writing it. It now prints "@"Hi i am in objective c---"" but it's not printing the string values from unity. Is there something I should add in my saveToAlbum.cs file? Thanks again.

avatar image DeveshPandey · Feb 01, 2013 at 02:36 PM 2
Share

Hakimo,I faced same error and the solution is :

You have to make a communication layer in objective c which will be accessed from C#, and save it with .mm extension(You have already done).

$$anonymous$$ake a separate class with .m extension which will transfer your photo from given URL. You have to call function of .m file from .mm file.

Flow is given below -

 // class CommunicationLayer.mm
 
 extern "C" {
     void savePicture(const char* picture)   {
        call function savePhoto(CreateNSString(picture)) of TransferImage class
     }
 }
 
 // TransferImage.m (also have .h file)
 in this class you have to implement your whole logic.
 i.e.
 savePhoto()
 didFinishSavingWithError()
 
 I think you have an idea to do that. If you have still facing any problem then you can contact me.
avatar image DeveshPandey · Feb 06, 2013 at 03:24 PM 1
Share

As I told you earlier, you have write code in Objective -C to transfer image and save it as .mm file. Your flow is correct, I don't know objective-C therefore I can't help you in Objective-C.

avatar image DeveshPandey · Feb 08, 2013 at 02:26 PM 1
Share

Now your flow and everything is correct, you have to debug your code by yourself. $$anonymous$$ake sure your image will stored in Document directory, and you have called objective C code after image saved in Document directory and all possibilities. :)

Show more comments

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

10 People are following this question.

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

Related Questions

Finding a gameobject in native code 0 Answers

native ios screencapture vs unity Application.captureScreenshot 0 Answers

Native Sharing dialog cause crash on iPad 2 iOS 7.1.2 1 Answer

Share screen and text on social media 1 Answer

Android Native share (intent) an image with a link 4 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