3D touch在Unity3D中的使用
0、开篇:
3D touch随着iOS9发布,它并不是一个单独的技术,而是可以分为pressure sensitivity、quick action以及peek&pop。在官方的介绍中提到可以给游戏更好的体验,但是实际上个人感觉除了pressure sensitivity能够改变游戏的操作方式外,quick action以及peek&pop真心是为APP设计的。
1、pressure sensitivity的使用:
首先在unity的脚本中添加检查是否支持3D touch的函数,这个函数本质是调用iOS代码的。
[DllImport("__Internal")]
// return 1 when device is support 3d touch
private static extern int _checkForceTouchCapability(); public static int CheckForceTouchCapability()
{
return _checkForceTouchCapability();
}
对应的iOS代码为
-(NSInteger)CheckForceTouchCapability
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {
isSupport3DTouch = NO;
return ;
}
if(self.rootViewController.view.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
{
isSupport3DTouch = YES;
return ;
} else {
isSupport3DTouch = NO;
return ;
}
}
下面是响应压力变化的处理函数,这次用传递函数指针到oc代码的方式来做,当然你也可以在iOS中使用UnitySendMessage方法。
private delegate void touch_event_callback_delegate(float force, float maximumPossibleForce); private static Action<float, float> touchEventCallback; [DllImport("__Internal")]
private static extern void _registTouchEventCallback(touch_event_callback_delegate func); public static void RegistTouchEventCallback(Action<float, float> func)
{
touchEventCallback = func;
_registTouchEventCallback(ExecuteTouchEventCallback);
} [MonoPInvokeCallback(typeof(touch_event_callback_delegate))]
private static void ExecuteTouchEventCallback(float force, float maximumPossibleForce)
{
touchEventCallback(force, maximumPossibleForce);
}
对应的iOS代码为
typedef void (*registTouchEventCallbackFunc)(float, float); static registTouchEventCallbackFunc touchEventCallback = nil; -(void)registTouchEventCallback:(registTouchEventCallbackFunc) func
{
touchEventCallback = func;
}
unity生成的Xcode工程中有个UnityView.mm文件,为了能够获取iOS中的压力变化,需要修改一下的代码
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UnitySendTouchesBegin(touches, event);
[UnityAppController UpdateForce:touches];
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
UnitySendTouchesEnded(touches, event);
[UnityAppController TouchesEndorCancelled:touches];
}
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
{
UnitySendTouchesCancelled(touches, event);
[UnityAppController TouchesEndorCancelled:touches];
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
UnitySendTouchesMoved(touches, event);
[UnityAppController UpdateForce:touches];
}
UpdateForce和TouchesEndorCancelled的定义为:
/**
* 实时反馈压感
*
* @param touches touch数据
*/
+(void)UpdateForce:(NSSet<UITouch *>*) touches
{
if (isSupport3DTouch && touchEventCallback != nil) {
touchEventCallback(touches.anyObject.force, touches.anyObject.maximumPossibleForce);
} } /**
* touchesEnded或者touchesCancelled触发时的处理
*/
+(void)TouchesEndorCancelled:(NSSet<UITouch *>*) touches
{
if (isSupport3DTouch && touchEventCallback != nil) {
touchEventCallback(, touches.anyObject.maximumPossibleForce);
}
}
其实用UnitySendMessage是最简单的,在TouchesEndorCancelled中force直接赋值为0的原因是我在测试的过程中发现快速的点击并且离开屏幕有时拿到的force不是0,这样在游戏中使用这个力的时候会有问题。
2、quick action的应用
目前想到的就是快速进入某个游戏场景吧,或者进入游戏后直接开启某个UI,总之对游戏性上没啥帮助。我在Demo中做的是快速进入场景2,默认应该是进入场景1。首先需要在info.plist中进行设置:
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypePlay</string>
<key>UIApplicationShortcutItemTitle</key>
<string>JUMP TO SCENE 2</string>
<key>UIApplicationShortcutItemType</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).action</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>scene</key>
<string>2</string>
</dict>
</dict>
</array>
核心是设置UIApplicationShortcutItemUserInfo,因为我们拿到的参数是从userinfo中拿到的。在使用quick action时unity中的编程非常少,主要是iOS编程。
首先需要在UnityAppcontroller.mm中添加:
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL succeeded)) completionHandler {
BOOL bHandledShortCutItem = [self handleShortCutItem:shortcutItem];
completionHandler(bHandledShortCutItem);
} -(BOOL)handleShortCutItem:(UIApplicationShortcutItem*) shortcutItem
{
BOOL handled = NO;
NSString *str = (NSString *)[shortcutItem.userInfo objectForKey:@"scene"];
if (str != nil) {
handled = YES;
UnitySendMessage("Interface", "ExecuteQuickAction", [str UTF8String]);
} return handled;
}
这个系统方法是用于处理在screen使用quick action进入游戏的。看了很多别人写的例子,在didFinishLaunchingWithOptions中会调用handleShortCutItem,然后返回NO,这样可以避免performActionForShortcutItem的调用。但是实际在测试中发现完全不需要在didFinishLaunchingWithOptions中会调用handleShortCutItem。
3、peek&pop
完全没有想到怎么用到游戏中,而且发现在peek时会有一个模糊的遮罩层。
4、Demo地址:https://github.com/klkucan/Unity_For3DTouch
3D touch在Unity3D中的使用的更多相关文章
- 在iOS9 中使用3D Touch
iOS9提供了四类API( Home Screen Quick Action . UIKit Peek & Pop . WebView Peek & Pop 和 UITouch For ...
- 3D touch 的 应用 --备用
在iPhone 6s和iPhone 6s Plus中Apple引入了3D Touch技术.3D Touch的触控技术,被苹果称为新一代多点触控技术.其实,就是此前在Apple Watch上采用的For ...
- 你想知道的3D Touch开发全在这里了
前言 iPhone 6s和iPhone 6s Plus为多点触摸界面带来了强大的3D触摸新维度.这项新技术可以感知用户按下显示屏的深度,让他们比以往任何时候都更能使用你的应用程序和游戏.更多关于3D ...
- 【Unity3d游戏开发】Unity3D中的3D数学基础---向量
向量是2D.3D数学研究的标准工具,在3D游戏中向量是基础.因此掌握好向量的一些基本概念以及属性和常用运算方法就显得尤为重要.在本篇博客中,马三就来和大家一起回顾和学习一下Unity3D中那些常用的3 ...
- Unity3d中3D Text对模型的穿透显示
昨晚,好友在电话里问我在Unity3d中使用3D Text,不想让其穿透模型显示,即想让场景中的3D Text与模型有正确的遮挡关系,怎么解? 今早谷歌上查了查,明白了原因,因为3D Text的默认材 ...
- iOS 3D Touch实践
本文主要讲解3DTouch各种场景下的开发方法,开发主屏幕应用icon上的快捷选项标签(Home Screen Quick Actions),静态设置 UIApplicationShortcutIte ...
- 初学3D Touch
引言 With iOS 9, new iPhone models add a third dimension to the user interface. A user can now press y ...
- iOS 3D Touch 适配开发
3D Touch的主要应用 文档给出的应用介绍主要有两块: 1.A user can now press your Home screen icon to immediately access fun ...
- 从3D Touch 看 原生快速开发
全新的按压方式苹果继续为我们带来革命性的交互:Peek和Pop,Peek 和 Pop 让你能够预览所有类型的内容,甚至可对内容进行操作,却不必真的打开它们.例如,轻按屏幕,可用 Peek 预览收件箱中 ...
随机推荐
- iOS 字符串转son json转字符串
+ (NSString*)dictionaryToJson:(NSDictionary *)dic { NSError *parseError = nil; NSData *jsonDa ...
- Android最大可运行内存
int maxMemory = (int) Runtime.getRuntime().maxMemory();
- 前端学习之本地储存与cookie
今天主要的学习内容是cookie与本地储存的知识, 在HTML5中,本地存储是一个window的属性,包括localStorage和sessionStorage,从名字应该可以很清楚的辨认二者的区别, ...
- 写字节流转换String 代码示例
public void pushEmployee(EmpPushToTianFangRequest request){ try { StringWriter sw = new StringWriter ...
- RHEL6p5下ntfs分区的挂载及自动挂载
No.1: #uname -a //查看你的Linux内核版本的命令 No.2:去http://www.atrpms.net/dist/el5/fuse/下载与内核接近的三个东西 fuse-libs- ...
- java和android及IOS对接RSA加密经验
1.网上找的java生成RSA密钥对的例子,产生的字附串实际上是hax后和密钥串 你可以将他们当成静态字附串存在java代码里 2.android和java可以代码复用,IOS对接比较麻烦 3.IOS ...
- HTML5-布局的使用
DIV布局: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UT ...
- linux-11 基本命令之 -工作期目录切换命令-pwd,cd,
pwd 命令用于显示当前的工作目录,格式为:pwd[选项] @1.查看当前的工作路径: [root@localhost /]# pwd cd 命令用于切换工作路径 格式为:"cd 目录名称& ...
- java HashMap那点事
集合类的整体架构 比较重要的集合类图如下: 有序否 允许元素重复否 Collection 否 是 List 是 是 Set AbstractSet 否 否 HashSet TreeSet 是(用二 ...
- cefsharp开发实例1
做了几年.NET开发,基本都是搞WEB居多,以前也搞过一个winform项目,虽然很把界面拼接出来了,但是感觉有点痛苦,改动的时候又要改动一大堆代码.最近又要搞个桌面软件,试着搜索了下html做界面方 ...