Unity for iOS 调用UIDatePicker
2016-03-01 12:01
1866 查看
在这里简单,实现一个 Unity for iOS UIDatePicker来输入日期日间的功能。主要有两个方面:1、unity 与 ios的交互、ios下的objective-c的相关api。具体的看代码吧,有些注释。
以后有时间,再弄一个android版的上来。
以后有时间,再弄一个android版的上来。
一,首上一张图:
二,上一个主要的oc代码,基它的可以到demo中找到
// // IOSDatePicker.m // Unity-iPhone // // Created by lzh on 16/2/29. // // #import "IOSDatePicker.h" @implementation IOSDatePicker //-(void) showDatePicker //{ // UIDatePicker *_dataPicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-216, [UIScreen mainScreen].bounds.size.width, 216)]; // _dataPicker.datePickerMode = UIDatePickerModeDate; // _dataPicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]; // [_dataPicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged]; // [self.view addSubview:_dataPicker]; //} // //-(void) dateChanged:(id)sender //{ // NSLog(@"ios: dataChanged"); // NSDate *date = [((UIDatePicker*)sender) date]; // NSDateFormatter *dateFormatter =[[NSDateFormatter alloc]init]; // [dateFormatter setDateFormat:@"yyyy-MM-dd"]; // NSString *strDate = [dateFormatter stringFromDate:date]; // UnitySendMessage( "Main Camera", "iosCallback_DatePickerChanged", strDate.UTF8String); //} static IOSDatePicker * na_sharedInstance; + (id)sharedInstance { if (na_sharedInstance == nil) { na_sharedInstance = [[self alloc] init]; } return na_sharedInstance; } UIDatePicker *datePicker; -(void) showDatePicker { if (datePicker!=nil) { [self DP_removeViews:nil]; } UIViewController *vc = UnityGetGLViewController(); CGRect toolbarTargetFrame = CGRectMake(0, vc.view.bounds.size.height-216-44, [self GetW], 44); CGRect datePickerTargetFrame = CGRectMake(0, vc.view.bounds.size.height-216, [self GetW], 216); CGRect darkViewTargetFrame = CGRectMake(0, vc.view.bounds.size.height-216-44, [self GetW], 260); UIView *darkView = [[UIView alloc] initWithFrame:CGRectMake(0, vc.view.bounds.size.height, [self GetW], 260)]; darkView.alpha = 1; darkView.backgroundColor = [UIColor whiteColor]; darkView.tag = 9; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DP_dismissDatePicker:)]; [darkView addGestureRecognizer:tapGesture]; [vc.view addSubview:darkView]; datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, vc.view.bounds.size.height+44, [self GetW], 216)]; datePicker.tag = 10; [datePicker addTarget:self action:@selector(DP_changeDate:) forControlEvents:UIControlEventValueChanged]; datePicker.datePickerMode = UIDatePickerModeDate; [vc.view addSubview:datePicker]; UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, vc.view.bounds.size.height, [self GetW], 44)]; toolBar.tag = 11; toolBar.barStyle = UIBarStyleDefault; UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DP_dismissDatePicker:)]; [toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]]; [vc.view addSubview:toolBar]; [UIView beginAnimations:@"MoveIn" context:nil]; toolBar.frame = toolbarTargetFrame; datePicker.frame = datePickerTargetFrame; darkView.frame = darkViewTargetFrame; [UIView commitAnimations]; } - (void)DP_changeDate:(UIDatePicker *)sender { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @"yyyy-MM-dd"]; NSString *dateString = [dateFormatter stringFromDate:sender.date]; NSLog(@"--- DateChangedEvent: %@", dateString); UnitySendMessage("IOSToUnityMessageObject", "ios_callback_DatePickerChanged", dateString.UTF8String); } - (void)DP_dismissDatePicker:(id)sender { UIViewController *vc = UnityGetGLViewController(); [self DP_PickerClosed:datePicker]; CGRect toolbarTargetFrame = CGRectMake(0, vc.view.bounds.size.height, [self GetW], 44); CGRect datePickerTargetFrame = CGRectMake(0, vc.view.bounds.size.height+44, [self GetW], 216); CGRect darkViewTargetFrame = CGRectMake(0, vc.view.bounds.size.height, [self GetW], 260); [UIView beginAnimations:@"MoveOut" context:nil]; [vc.view viewWithTag:9].frame = darkViewTargetFrame; [vc.view viewWithTag:10].frame = datePickerTargetFrame; [vc.view viewWithTag:11].frame = toolbarTargetFrame; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(DP_removeViews:)]; [UIView commitAnimations]; } -(void) DP_PickerClosed:(UIDatePicker *)sender { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @"yyyy-MM-dd"]; NSString *dateString = [dateFormatter stringFromDate:sender.date]; NSLog(@"--- DateChangedEvent: %@", dateString); UnitySendMessage("IOSToUnityMessageObject", "ios_callback_DatePickerChanged", dateString.UTF8String); } - (void)DP_removeViews:(id)object { if(datePicker==nil) { return; } UIViewController *vc = UnityGetGLViewController(); [[vc.view viewWithTag:9] removeFromSuperview]; [[vc.view viewWithTag:10] removeFromSuperview]; [[vc.view viewWithTag:11] removeFromSuperview]; } -(CGFloat) GetW { UIViewController *vc = UnityGetGLViewController(); bool IsLandscape; UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { IsLandscape = true; } else { IsLandscape = false; } CGFloat w; if(IsLandscape) { w = vc.view.frame.size.height; } else { w = vc.view.frame.size.width; } //NSLog(@"--- System Version: %@", [UIDevice currentDevice].systemVersion); NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."]; if ([[vComp objectAtIndex:0] intValue] >= 8) { w = vc.view.frame.size.width; } return w; } @end //------------- called by unity -----begin----------------- #if defined (__cplusplus) extern "C" { #endif // // 创建一个data picker // void _iosOpenDatePicker_Date() // { // UIViewController *vc = UnityGetGLViewController(); // IOSDatePicker *myController = [[IOSDatePicker alloc] init]; // [vc.view addSubview: myController.view]; // // [myController showDatePicker]; // } // void _iosOpenDatePicker_Date() // { // UIViewController *vc = UnityGetGLViewController(); // // UIDatePicker *_dataPicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(-50, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-216, 216)]; // _dataPicker.datePickerMode = UIDatePickerModeDate; // _dataPicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]; // [_dataPicker addTarget:vc action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged]; // [vc.view addSubview:_dataPicker]; // } void _iosOpenDatePicker_Date() { [[IOSDatePicker sharedInstance] showDatePicker]; } #if defined (__cplusplus) } #endif //------------- called by unity -----end-----------------
三、u3d demo
下载相关文章推荐
- unity 外设输入
- 游戏引擎中三大及时光照渲染方法介绍(以unity3d为例)
- unity shaderlab Blend操作
- unity shaderlab Blend操作
- 【Unity Shaders】Mobile Shader Adjustment—— 什么是高效的Shader
- Unity物理系统
- 《Unity_API解析》 第五章 Mathf类
- 碰撞检测之Ray-Plane检测
- 项目记录24--unity-tolua框架 View01---UIBase.lua
- Unity5 弹力球的制作
- Unity中UGUI的部分控件的使用
- [Unity实战]屏幕追踪显示目标
- 关于AlphaTest
- Unity3D学习笔记(1)
- Unity2D多分辨率屏幕适配方案
- Unity3D导入图片大小被改变
- Unity简易数据库之Sqlite
- Unity NGUI实现 点击弹窗跟随鼠标点击位置
- 利用Unity播放视频剪影!BadApple
- Unity调用Android