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 预览收件箱中 ...
随机推荐
- Oracle 服务手动启动关闭
在windows7中安装完Oracle11g之后会出现一下七种服务:可通过运行->services.msc查看. 其中各个服务名称中的ORCL或orcl为SID即System IDentifie ...
- python数据挖掘领域工具包
原文:http://qxde01.blog.163.com/blog/static/67335744201368101922991/ Python在科学计算领域,有两个重要的扩展模块:Numpy和Sc ...
- 汇总常用的jQuery操作Table tr td方法
虽然现在DIV+CSS进行页的布局大行其道,但是很多地方使用table还是有很多优势,用table展示数据是比较方便的,下面汇总了jQuery操作Table tr td常用的方法,熟记这些操作技巧,下 ...
- POJ 2352 Stars(树状数组)
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30496 Accepted: 13316 Descripti ...
- BZOJ4170 极光(CDQ分治 或 树套树)
传送门 BZOJ上的题目没有题面-- [样例输入] 3 5 2 4 3 Query 2 2 Modify 1 3 Query 2 2 Modify 1 2 Query 1 1 [样例输出] 2 3 3 ...
- 一个好用的C#类型转换器
public static object ChangeType(object value, Type targetType) { if (targetType.IsGenericType && ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 随笔SublimeText Theme安装
2015-12-31日记 在更换SublimeText颜色的时候没有及时的备份这个文件.导致浪费了半个 小时来处理这个问题 处理问题需要冷静歘平慢一些, 关键在于不出错. 当时有一个想法就是这个东西不 ...
- Android--短信
1.Android 提供一系列 API,可以是我们在自己的程序中发送和接收短信: 2.接收短信: 1)当手机接收到一条短信时,系统会发出一条值为 android.provider.Telephony. ...
- 2013-08-12【随笔2】-Roy
最近总是因为一下小事情,就会变得闹心,就会自己胡思乱想,自己吓唬自己,自己给自己找烦恼. 是因为每天无所事事,日子过得没有了重点,然后每天人心惶惶,有点杞人忧天了. 还是因为这样平淡的日子,消磨了我们 ...