Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Gillissie · Dec 05, 2011 at 06:46 AM · iospluginxcode

Compiling plugin for iOS - need more info.

I looked at these docs: http://unity3d.com/support/documentation/Manual/Plugins.html But they really skim over it.

Exactly how do I compile an Objective-C function and get it to work in Unity? Is it something I do using Xcode? If so, how do I set up a project in Xcode to do it? When I compile, do I get a compiled file of some sort that I have to add to my Unity Assets?

Thanks.

Comment
Add comment · Show 3
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 Gillissie · Dec 08, 2011 at 05:31 AM 0
Share

Really, no answers? Nobody is doing this?

avatar image crazyKnight · Dec 08, 2011 at 05:40 AM 0
Share

which plugin do u want to add..????

avatar image Gillissie · Dec 08, 2011 at 05:52 AM 0
Share

I want to write and compile my own plugins so I can use native iOS functionality from within Unity's scripts. I think that's what plugins are for, if I'm not mistaken.

4 Replies

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

Answer by crazyKnight · Dec 08, 2011 at 06:29 AM

this is how you a call a function declared in xcode

 using UnityEngine;
 using System.Collections;
 
     public class NativeMethod : MonoBehaviour {
     
         [System.Runtime.InteropServices.DllImport("__Internal")]
         extern static public int AwesomeFunction(int awesomeParameter);
         private int temp;
   
         void OnGUI()
         {
             if(GUI.Button(new Rect(10*(Screen.width)/100,10*(Screen.height)/100,25*(Screen.width)/100,20*(Screen.height)/100),"CallNativeFunction"))
             {
                 PlayerPrefs.SetInt("Save",0);
                 temp = AwesomeFunction(5);
             }
         }
     }

this is the code for the xcode side

AppController.h

 #ifdef __cplusplus
 extern "C" {
 #endif
         
     int AwesomeFunction(int awesomeParameter);    
 #ifdef __cplusplus
 }
 #endif

AppController.mm

     int AwesomeFunction(int awesomeParameter)
     {
         // My awesome code goes here.
         NSLog(@"Function pressed from unity with value %d",awesomeParameter);
             
         
         if ([[NSUserDefaults standardUserDefaults] integerForKey: @"Save"] == 0)  //to read playerpref set from unity
         {
             NSLog(@"value recieved from unity for save with value 0");
         }
         
         [[NSUserDefaults standardUserDefaults] setInteger: 42 forKey: @"Save"];  // set new value for playerpref
 
 // you can call whichever plugin you want here in this function or if you want to return some value back to unity or whatever else you like
 
         return 70;
     }



Hope this helps....

Comment
Add comment · Show 7 · 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 Gillissie · Dec 08, 2011 at 06:34 AM 0
Share

Yes, thanks, that does help. But it doesn't tell me how to compile it and what files are a result of the compile, and what I do with said file(s) after compiling. Would I put the file in the Unity project's Assets folder somewhere?

avatar image crazyKnight · Dec 08, 2011 at 06:42 AM 0
Share

you dont have to put the files in unity asset folder...

lets go by an example suppose you have to add inapp plugin to ur game...

1.Copy the sdk provided for the inapp in your xcode project. 2. now you can call the method required for the inapp(from the sdk) in the method awesome function as declared above. 3. now when the inapp is successful you can send a confirmation back to unity by changing the value set for playerpref as i shown in the code above or there are many other ways to do it i am just suggesting how i would do that.

now if you ask how to use the sdk methods in xcode then you have to go to stackoverflow and search there ...

avatar image Gillissie · Dec 08, 2011 at 07:26 AM 0
Share

Thanks, but I don't want to call someone else's sdk. I just want to know how to create an xcode plugin project from scratch like your example above, then compile it, and any other steps required to make it so I can make calls to it from Unity. I understand how to make calls to it, but I don't know how to set up the plugin project and compile it.

avatar image Gillissie · Dec 08, 2011 at 07:28 AM 0
Share

$$anonymous$$aybe I'm misunderstanding how it works overall. $$anonymous$$aybe I don't compile the plugin? $$anonymous$$aybe it is added to the xcode project that is generated by Unity when I build?

avatar image Gillissie · Dec 08, 2011 at 07:32 AM 0
Share

The docs are confusing (go figure). At the top it says this: "In order to use a plugin you need to do two things: Write a plugin in a C based language and compile it. Create a C# script which calls functions in the plugin to do something."

Then it goes on to say this: "Add your native implementation to the generated XCode project's "Classes" folder (this folder is not overwritten when project is updated, but don't forget to backup your native code)."

So based on the first statement, I expected to compile a plugin by itself, not as part of the generated project. But the second statement says to put it in the generated project.

Show more comments
avatar image
0

Answer by crazyKnight · Dec 08, 2011 at 07:41 AM

@ Gillissie : see there are two ways to create a plugin

1.Write a plugin in c based language compile it and then copy it to the plugin folder in your unity project and then call the methods as per your need

2 call a static extern function in unity,build the project,go to xcode created build,declare and define the function called in unity and do your required in that specific function

i would prefer the 2 ways as i have done enough work on objective c before coming to unity so it helps me there,and i am kinda of confused what you are actually trying to do can you just be a little more precise of what you actually want to create maybe i can suggest you a more easy way out ....

Comment
Add comment · Show 2 · 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 crazyKnight · Dec 08, 2011 at 07:43 AM 0
Share

sorry that should have been a comment...

avatar image Gillissie · Dec 08, 2011 at 07:47 AM 0
Share

I want to get access to native iOS properties, such as locale, and maybe sqlite3 database support for saving games. I want to do things that you can do on an iPhone but not natively in Unity.

avatar image
0

Answer by crazyKnight · Dec 08, 2011 at 07:56 AM

okay so now suppose you want to get the device locale in unity so what you can do here is

write this code in the native function ou created in xcode(awesome function in the code provided above)

 int AwesomeFunction(int awesomeParameter)
 {
 NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
 }

This will return a two letter code for the currently selected language. "en" for English, "es" for Spanish, "de" for German, etc. For more examples, please see this Wikipedia entry (in particular, the 639-1 column):

http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

so now you have the locale in the variable which you can return to unity and do whatever changes you want to do based on the locale, you can either return it as a return type or set it in player prefs and read it in unity.

correct me if i took your question wrong.

Comment
Add comment · Show 2 · 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 Gillissie · Dec 08, 2011 at 08:23 AM 0
Share

I appreciate your help, but this isn't answering my question. However, I think I may be able to figure it out based on bits and pieces that I can gather from this whole conversation.

The question isn't how to get the locale (that was just one example of what I want to do with plugins), the question is how to get it working. I think the answer lies in the fact that there are two different kinds of plugins.

  1. Compiled plugins that are dll's that must be in the Assets folder of the Unity project. These have nothing to do with iOS.

  2. Native device plugins like iOS, where the "plugin" code lives in the generated Xcode project and is compiled as part of the generated Xcode project, not separately.

From what I gather, what I want is #2.

avatar image Gillissie · Dec 08, 2011 at 09:28 AM 0
Share

Thanks for your help. I have it figured out now. The Bonjour example project helped me too.

avatar image
0

Answer by mannu · Jul 18, 2012 at 02:22 PM

I have developed game application in unity and give login facility from facebook. i used the AppController.mm to login from facebbok and its working fine but i need if user login from facebook and return to the game then game will start automatically, so needed one function to call from AppController to unity for test user is looged or not. it is possible, anyone can help?

Thanks

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

iOS build is including Mac OSX .bundle files into Xcode Project 2 Answers

iOS - Disable ARC on XCode Projects 2 Answers

Call Unity class in XCode 1 Answer

Problem trying to update texture using glTexImage2D on iOS. 0 Answers

Linker error when creating a static iOS library that depends on another static library. 1 Answer


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