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 预览收件箱中 ...
随机推荐
- 用Python写爬虫爬取58同城二手交易数据
爬了14W数据,存入Mongodb,用Charts库展示统计结果,这里展示一个示意 模块1 获取分类url列表 from bs4 import BeautifulSoup import request ...
- Python 学习---------Day4
第十章 Python语句简介Python的代码书写要求,以及换行等语句可以扩越多行,只要将其封闭在圆括号内,方括号内或大括号内即可,可以使用分号终止.用\可以允许我们跨越多行一个简单的交互式循环whi ...
- 【开发环境】OFFICE 完全卸载工具(微软)
OFFICE没有正确安装,每次打开OFFICE都会提示: “The setup controller has encountered a problem during instll.Please re ...
- javascript中array常用属性方法
属性: length 表示一个无符号 32-bit 整数,返回一个数组中的元素个数. 截短数组..截短至长度2 则: .length = 2 方法: Array.from() 方法可以将一个类数 ...
- Microsoft Office 2010/2013安装组件预设
日常维护中,多台电脑需要安装Office,可是Office包含的组件又很多(Excel/Word/PPT/OUTLOOK/ACCESS等),有些是不需要的,默认情况下Office都默认安装,一个一个调 ...
- 第59讲:Scala中隐式转换初体验
今天学习了下隐式转换的内容.所谓隐式转换,就是说,一个实例拥用1 2 3方法,但是当它需要4方法的时候,它没有,但是却可以通过转换成另一种类型来调用4方法,而且这种转换是自动转换不需要人为干预的,这种 ...
- html5+css3 制作音乐播放器
//css// body , html{ margin:0; padding:0; font:12px Arial, Helvetica, sans-serif; } .Mus ...
- dataTables-使用详细说明整理
本文共四部分:官网 | 基本使用|遇到的问题|属性表 一:官方网站:[http://www.datatables.net/] 二:基本使用:[http://www.guoxk.com/node/jqu ...
- PCFG -- 基于统计方法生成语法树
语法树的作用 一棵语法树不仅包括了词性(part of speech), 还包括了短语(如名词短语, 动词短语)和结构化的信息(如主语, 谓语和宾语). 这些信息是进行机器翻译所必须的, 例如机器翻译 ...
- caffe在windows 下的配置及matlab接口编译(无GPU)
本人机子windows 10,matlab2015a,vs2013(官网使用的是vs2013) 1.首先去github上下载caffe的windows包,地址:https://github.com/B ...