- Home /
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.
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.
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....
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?
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 ...
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.
$$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?
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.
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 ....
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.
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.
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.
Compiled plugins that are dll's that must be in the Assets folder of the Unity project. These have nothing to do with iOS.
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.
Thanks for your help. I have it figured out now. The Bonjour example project helped me too.
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
Your answer
Follow this Question
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
Linker error when creating a static iOS library that depends on another static library. 1 Answer
Problem trying to update texture using glTexImage2D on iOS. 0 Answers