• 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 pro_metedor · Apr 18, 2011 at 03:19 PM · iphoneplugin

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();

UnityPlugin.mm:

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?

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
0

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?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

A node in a childnode? 1 Answer

Calling UnitySendMessage requires which library on iOS? 2 Answers

Marshal.PtrToStructure in iPhone makes error. 1 Answer

Is there possible to use Objective-C code inside C unity plugin? 0 Answers

Linking Native toolkit 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges