本文学习怎么在unity里返回消息到iOS,现实功能(点击unity里面的按钮传递一个字符串到ios显示在消息对话框上,并且打开一个新的VeiwContoller)。言归正传
首先我们创建一个unity工程,创建一个MainScript脚本,代码如下:
using UnityEngine;
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("泰戈尔");
}
}
}
代码创建完成了,把脚本挂在Main Camera上,在这里重点讲解一下的是上面代码中使用到的SDK这个类,
这个类我们看作它是一个管理类,它不赋值在任意对象身上,只接受调用管理,点击两个按钮后将分别调用下面方法中的_ActivateButton0() 与 _ActivateButton1(),而这两个方法则是去调用xcode 我们自己实现的方法_pressButton0() 与 _pressButton1(), 前提上须下面代码中的注册,这样子导出项目的时候xcode会帮我们 生成注册信息,我们只须要实现这两个方法就可以了。代码如下:
using UnityEngine;
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);
}
}
}
这里需要注意的是一定要引用
using
System
.
Runtime
.
InteropServices
;这个命名空间。unity的操作到此就完成来,截图看看最终工程。
好,接下来我们把unity工程部署到xcode里面。部署的时候默认操作就行了。
打开刚刚我们部署的项目,如图:
编译一下项目,没有问题,接下来在classes文件夹里创建一个我们自己的ViewController(当然你直接创建一个基于nsobject的类也是可以的),命名为LJ
ViewController,
用它来接收Unity3D 回馈回来的消息,_PressButton0 与 _PressButton1 这两个方法在Unity3D中已经注册过,所以在这个类中我们须要对它进行Xcode中的实现
。
在
ViewController.m写如下代码
#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];
到这我们就能打开新的视图了。