Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call ios native code through unity

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to call ios native code through unity. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

First of all, we need to create a button on the interface. There are several ways. You can create it with ngui, or you can write GUI code directly.

Let me use the simple GUI code first.

GUI means graphical user interface, which is something that produces an interface.

First of all, in our

Add a script to the MetaioSDK control

Click add component

Select newscript

Give me a name and click create and add. We choose the C # script here, and you can also use javaScript or Boo

And then you'll see one more script coming out.

At this point, double-click the script file to enter the monoDevelop editing script

Every script you just created will automatically populate two methods, one of which is start ().

The other is update ()

The Start () method knows by its name that it is a method to do some initialization operations, so some of your initialization operations can be done in start.

The update method is a method that is executed every frame your program runs, so you can update it in it.

Here, because GUI is involved, we add a method

OnClick:

Void OnGUI () {

If (GUI.Button (new Rect (Screen.width-200)

0,200,100), "Back"))

{

}

}

Very simple, is to draw a button in the upper right corner of the screen, the following is the key to this article

To call the ios code, first add a sentence at the beginning

Using System.Runtime.InteropServices

Then in the class body, we want to declare our ios external method

[DllImport ("_ _ Internal")]

Extern static public void _ callPhone (string str)

Then we can do it in the method, and the complete code is as follows

Using UnityEngine

Using System.Collections

Using System.Runtime.InteropServices

Public class CallPhone: MonoBehaviour {

[DllImport ("_ _ Internal")]

Extern static public void _ callPhone (string str)

/ / Use this for initialization

Void Start () {

}

/ / Update is called once per frame

Void Update () {

}

Void OnGUI () {

If (GUI.Button (new Rect)

Screen.width-200

0

two hundred,

100), "Back")) {

_ callPhone ("13845129527")

}

}

}

Here, the work of unity is finished, as before, export build once xcode project

The next task is completed in xcode.

It is also very simple, to open the xcode project, you only need to add two files in the class directory. I have already written these two files, and I will put them in an attachment for you to download. The contents of the files are very simple. They are all OC codes.

After clicking add, you can run it, and then you can broadcast the phone by clicking on the upper left corner of the screen. If you change the parameter of callphone to a variable, you can also customize the number. Is it right to compare cool?

Let's parse the code in the file. There is basically nothing in the unityplugin.h file. Let's look directly at the unityplugin.mm file.

There is mainly one method in this.

Extern "C" {

Void _ callPhone (char* str)

{

NSString* astr = [[NSString alloc] initWithCString:str encoding: (NSUTF8StringEncoding)]

NSString* titl = @ "tel://"

NSString* allstr = [titl stringByAppendingString:astr]

[[UIApplication sharedApplication] openURL: [NSURL URLWithString:allstr]]

}

}

Anyone who has a basic program should be able to understand it. Initialize the parameter string in utf8 format first.

NSString* astr = [[NSString alloc] initWithCString:str encoding: (NSUTF8StringEncoding)]

2Jing three sentences of code is to combine the strings.

NSString* titl = @ "tel://"

NSString* allstr = [titl stringByAppendingString:astr]

The last sentence is dialed out.

[[UIApplication sharedApplication] openURL: [NSURL URLWithString:allstr]]

Then we can add other functional code in this, such as calling email

Void _ takeEmail (char* str)

{

NSString* astr = [[NSString alloc] initWithCString:str encoding: (NSUTF8StringEncoding)]

NSString* titl = @ "mailto://"

NSString* allstr = [titl stringByAppendingString:astr]

[[UIApplication sharedApplication] openURL: [NSURL URLWithString:allstr]]

}

This is the end of the article on "how to call ios native code through unity". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report