好久没来了,最近发现了一些好玩的玩意,也是项目中需要unity中要调用Native,之前也一直没接触过,后来通过查找资料一步步的打包测试终于也算是把这个问题解决了,话不多说,上码!
就以unity中调用iOS的datePicker为例
1、在xcode中写好OC的UIDatePicker为后期调用做准备
static iOSDatePicker *shareInstance;
+ (id)shareInstance {
if (shareInstance == nil) {
shareInstance = [[self alloc] init];
}
return shareInstance;
}
static UIDatePicker *datePicker;
- (void)showDatePicker {
if (datePicker != nil) {
[self removeViews:nil];
}
UIViewController *vc = UnityGetGLViewController();
CGRect toolbarTargetFrame = CGRectMake(0, vc.view.bounds.size.height-216-44, vc.view.frame.size.width, 44);
CGRect datePickerTargetFrame = CGRectMake(0, vc.view.bounds.size.height-216, vc.view.frame.size.width, 216);
CGRect darkViewTargetFrame = CGRectMake(0, vc.view.bounds.size.height-216-44, vc.view.frame.size.width, 260);
UIView *darkView = [[UIView alloc] initWithFrame:CGRectMake(0, vc.view.bounds.size.height, vc.view.frame.size.width, 260)];
darkView.alpha = 1;
darkView.backgroundColor = [UIColor whiteColor];
darkView.tag = 1001;
[vc.view addSubview:darkView];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, vc.view.bounds.size.height+44, vc.view.frame.size.width, 216)];
datePicker.tag = 1002;
[datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
datePicker.datePickerMode = UIDatePickerModeDate;
[vc.view addSubview:datePicker];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, vc.view.bounds.size.height, vc.view.frame.size.width, 44)];
toolBar.tag = 1003;
toolBar.barStyle = UIBarStyleDefault;
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)];
[toolBar setItems:[NSArray arrayWithObjects: space, doneButton, nil]];
[vc.view addSubview:toolBar];
[UIView beginAnimations:@"MoveIn" context:nil];
toolBar.frame = toolbarTargetFrame;
datePicker.frame = datePickerTargetFrame;
darkView.frame = darkViewTargetFrame;
[UIView commitAnimations];
}
- (void)dismissDatePicker:(UIDatePicker *)datePicker {
UIViewController *vc = UnityGetGLViewController();
CGRect toolbarTargetFrame = CGRectMake(0, vc.view.bounds.size.height, vc.view.frame.size.width, 44);
CGRect datePickerTargetFrame = CGRectMake(0, vc.view.bounds.size.height+44, vc.view.frame.size.width, 216);
CGRect darkViewTargetFrame = CGRectMake(0, vc.view.bounds.size.height, vc.view.frame.size.width, 260);
[UIView beginAnimations:@"MoveOut" context:nil];
[vc.view viewWithTag:1001].frame = darkViewTargetFrame;
[vc.view viewWithTag:1002].frame = datePickerTargetFrame;
[vc.view viewWithTag:1003].frame = toolbarTargetFrame;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(removeViews:)];
[UIView commitAnimations];
}
- (void)dateChanged:(UIDatePicker *)datePicker {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: @"yyyy-MM-dd"];
NSString *dateString = [dateFormatter stringFromDate:datePicker.date];
NSLog(@"--- DateChangedEvent: %@", dateString);
UnitySendMessage("PluginsObject", "ios_callback_DatePickerChanged", dateString.UTF8String);
}
- (void)removeViews:(id)object {
if (datePicker == nil) {
return;
}
UIViewController *vc = UnityGetGLViewController();
[[vc.view viewWithTag:1001] removeFromSuperview];
[[vc.view viewWithTag:1002] removeFromSuperview];
[[vc.view viewWithTag:1003] removeFromSuperview];
}
在这有一个细节一定要注意: UIViewController *vc = UnityGetGLViewController(); 前期这个你在xcode中是敲不出来的 ,可以直接用self代替,就以正常的调用方法来写
2、将 .h 跟 .m 文件拖入unity的文件夹
按照这个路径来拖.h .m 文件,要不后期build在xcode中会找不到,在这个文件夹中你会发现有很多的.meta 文件,这个你不用管理,拖入之后会自定生成对应的.meta文件
3、unity的iOS文件夹中打开.m 文件
#if defined (__cplusplus)
extern "C" {
#endif
void _iOSDatePicker_Date () {
[[iOSDatePicker shareInstance] showDatePicker];
}
void _iOSDatePicker_Dismiss () {
[[iOSDatePicker shareInstance] removeViews:nil];
}
#if defined (__cplusplus)
}
#endif
这里的方法是你后期再unity中将要调用的,包括 UIViewController *vc = UnityGetGLViewController(); 现在可以直接敲出来,跟你之前的self做一个替换,到这里iOS的代码就结束了,当然后期build之后你会发现xcode中全是.h 跟.mm 文件,没有.m ,除了自己拖入的文件之外
4、找到你要方法传递的对象PluginsObject,然后开始编写脚本
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class iOSDatePicker : MonoBehaviour
{
#if UNITY_IPHONE
[DllImport ("__Internal")]
private static extern void _iOSDatePicker_Date();
[DllImport ("__Internal")]
private static extern void _iOSDatePicker_Dismiss();
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
public static void iOSDatePicker_Date () {
if (Application.platform == RuntimePlatform.IPhonePlayer) {
_iOSDatePicker_Date ();
}
}
public static void iOSDatePicker_Dismiss () {
if (Application.platform == RuntimePlatform.IPhonePlayer) {
_iOSDatePicker_Dismiss ();
}
}
private static iOSDatePicker _instance;
public static iOSDatePicker Instance{get{ return _instance; }}
void Awake() {
if (_instance != null) {
DestroyImmediate(this);
return;
}
_instance = this;
}
public System.Action<string> callback_datePickerChanged;
void ios_callback_DatePickerChanged (string date_str)
{
if(callback_datePickerChanged != null)
{
callback_datePickerChanged(date_str);
}
}
#endif
}
在这里注意就是你定义的方法与对回调,一定注意#if 与 #endif 判断,以及using System.Runtime.InteropServices;的使用,要不下边的[DllImport (“__Internal”)] 你是直接敲不出来的,这里我的理解就是一个桥接文件,方法名称一定要保持一致要不会报出这个错误:
到这里基本就结束了,接下里的点击事件,就要看你在哪里来调用了,关于打包编译,直接在unity中操作:
关于以上这些说简单点就是两句话:
1、UnitySendMessage(“PluginsObject”, “ios_callback_DatePickerChanged”, dateString.UTF8String);
参数1:发送消息对象
参数2:回调方法
参数3:要传递的参数
2、
叨叨那么久,个人感觉已经够详细了,有什么问题欢迎大家一起学习!!!
1万+

被折叠的 条评论
为什么被折叠?



