using System.Collections;
public class MainScript : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnGUI()
{
//oneButton
if (GUI.Button (new Rect (0, 0, 200, 100), "OneButtonTap"))
{
SDK.ActivateButton0("lj jia");
}
//twoButton
if (GUI.Button (new Rect (0, 200, 200, 100), "TwoButtonTap"))
{
SDK.ActivateButton1("泰戈尔");
}
}
}
using System.Runtime.InteropServices;
public class SDK
{
//导出按钮以后将在xcode项目中生成这个按钮的注册,
//这样就可以在xocde代码中实现这个按钮点击后的事件。
[DllImport("__Internal")]
private static extern void _PressButton0 (string str);
public static void ActivateButton0 (string str)
{
if (Application.platform != RuntimePlatform.OSXEditor)
{
//点击按钮后调用xcode中的 _PressButton0 ()方法,
//方法中的内容须要我们自己来添加
_PressButton0 (str);
}
}
//和上面一样
[DllImport("__Internal")]
private static extern void _PressButton1 (string str1);
public static void ActivateButton1 (string str1)
{
if (Application.platform != RuntimePlatform.OSXEditor)
{
_PressButton1 (str1);
}
}
}
#import "LJViewController.h"
@interface LJViewController ()
@end
@implementation LJViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
void _PressButton0(char str[])
{
NSString * yy = [NSString stringWithCString:str encoding:NSUTF8StringEncoding];
NSLog(@"%@",yy);
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:yy];
[alert setMessage:@"你打开了第一个视图"];
[alert adonWithTitle:@"确定"];
[alert show];
}
void _PressButton1(char str1[])
{
NSString * yy = [NSString stringWithCString:str1 encoding:NSUTF8StringEncoding];
NSLog(@"%@",yy);
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:yy];
[alert setMessage:@"点击了第二个按钮"];
[alert adonWithTitle:@"确定"];
[alert show];
}
@end
现在我们部署到真机上就可以看到点击unity里面的按钮弹出ios里面的消息对话框了。
当然在这你可能会问那怎么打开一个新的视图,接下来我们就来实现这个功能,先新建一个类继承ViewController,命名为myViewController,在LJViewController.m文件里倒入我们刚刚创建的myViewController头文件,接下来我们找到我们刚刚写的void _PressButton0方法,加入如下代码
MyViewController *myview = [[MyViewController alloc]init];
[UnityGetGLViewController().self.view addSubview:myview.view];
到这我们就能打开新的视图了。