您的位置:首页 > 移动开发 > Unity3D

Unity for iOS 调用UIDatePicker

2016-03-01 12:01 1866 查看
在这里简单,实现一个 Unity for iOS UIDatePicker来输入日期日间的功能。主要有两个方面:1、unity 与 ios的交互、ios下的objective-c的相关api。具体的看代码吧,有些注释。

以后有时间,再弄一个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

下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: