- Home /
Objective-C and C code in unity plugin
Hi !
I am trying to open a webView from my application, I have prepared this function:
UnityPlugin.h:
extern "C" void DisplayWebViewFromPlugin();
extern "C" {
void DisplayWebViewFromPlugin()
{
CGRect webFrame = CGRectMake(20.0, 250.0, 280.0, 150.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
NSString *urlAddress = @"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[[[UIApplication sharedApplication ] keyWindow] addSubview:webView];
[webView release];
}
}
Then I build a libUnityPlugin.a file and put it into assets dir. I also include UnityPlugin.h file into my xCode project, to link the whole project.
NewBehaviorScript.cs :
public class NewBehaviourScript : MonoBehaviour { [DllImport ("__Internal")] private static extern void DisplayWebViewFromPlugin();
// Use this for initialization void Start () { }
// Update is called once per frame void Update () {
}
void OnGUI () { if (GUI.Button(new Rect (15, 25, 200, 50), "Show Web Viev")) { DisplayWebViewFromPlugin();
}
}
}
Unfortunately it crashes my application while its starting sending EXC_BAD_ACCESS. When I get rid of Objective-C code everything works just fine. What did I miss including this code into my Unity project?
Answer by pro_metedor · Apr 26, 2011 at 01:07 PM
I have found this piece of code on this forum: answers.unity3d.com/questions/2631/ but the code is placed at AppDelegate.mm file, and not in the plugin file. This means plugin is only pure C bridge between unity and the application. Am I correct?