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中的使用的更多相关文章

  1. 在iOS9 中使用3D Touch

    iOS9提供了四类API( Home Screen Quick Action . UIKit Peek & Pop . WebView Peek & Pop 和 UITouch For ...

  2. 3D touch 的 应用 --备用

    在iPhone 6s和iPhone 6s Plus中Apple引入了3D Touch技术.3D Touch的触控技术,被苹果称为新一代多点触控技术.其实,就是此前在Apple Watch上采用的For ...

  3. 你想知道的3D Touch开发全在这里了

    前言 iPhone 6s和iPhone 6s Plus为多点触摸界面带来了强大的3D触摸新维度.这项新技术可以感知用户按下显示屏的深度,让他们比以往任何时候都更能使用你的应用程序和游戏.更多关于3D ...

  4. 【Unity3d游戏开发】Unity3D中的3D数学基础---向量

    向量是2D.3D数学研究的标准工具,在3D游戏中向量是基础.因此掌握好向量的一些基本概念以及属性和常用运算方法就显得尤为重要.在本篇博客中,马三就来和大家一起回顾和学习一下Unity3D中那些常用的3 ...

  5. Unity3d中3D Text对模型的穿透显示

    昨晚,好友在电话里问我在Unity3d中使用3D Text,不想让其穿透模型显示,即想让场景中的3D Text与模型有正确的遮挡关系,怎么解? 今早谷歌上查了查,明白了原因,因为3D Text的默认材 ...

  6. iOS 3D Touch实践

    本文主要讲解3DTouch各种场景下的开发方法,开发主屏幕应用icon上的快捷选项标签(Home Screen Quick Actions),静态设置 UIApplicationShortcutIte ...

  7. 初学3D Touch

    引言 With iOS 9, new iPhone models add a third dimension to the user interface. A user can now press y ...

  8. iOS 3D Touch 适配开发

    3D Touch的主要应用 文档给出的应用介绍主要有两块: 1.A user can now press your Home screen icon to immediately access fun ...

  9. 从3D Touch 看 原生快速开发

    全新的按压方式苹果继续为我们带来革命性的交互:Peek和Pop,Peek 和 Pop 让你能够预览所有类型的内容,甚至可对内容进行操作,却不必真的打开它们.例如,轻按屏幕,可用 Peek 预览收件箱中 ...

随机推荐

  1. 安装文件制作工具Wix概念快速入门

    前言 Wix==Windows installer XML 顾名思议. 用于制作WINDOWS安装文件的XML格式的描述文件. 因为其实现方式为基于声明的方式,而非命令的方式. 特整理一下其相关的概念 ...

  2. HP iLo2 试用序列号

    HP iLo2 试用序列号 2 条回复 32Q8W-GKHTR-NPDKY-5CD79-T525H hp的ilo2功能实在太有用了,不用往那个恶劣的机房跑了,系统重装也直接远程完成. 这个试用序列号用 ...

  3. C#is运算符的作用

    is运算符的含义:检查对象是不是给定类型,或者是否可以转换为给定类型,如果是,这个运算符就返回True. is运算符的语法:<operand> is <type> 这个表达式的 ...

  4. 做WP程序时遇到的一些问题及解决方法

    问题1:Type 'JDBYSJ.Data.NewsChannel' cannot be serialized. Consider marking it with the DataContractAt ...

  5. JavaWeb 命名规则

    命名规范命名规范命名规范命名规范 本规范主要针对java开发制定的规范项目命名项目命名项目命名项目命名 项目创建,名称所有字母均小写,组合方式为:com.company.projectName.com ...

  6. poj 1737 Connected Graph

    // poj 1737 Connected Graph // // 题目大意: // // 带标号的连通分量计数 // // 解题思路: // // 设f(n)为连通图的数量,g(n)为非连通图的数量 ...

  7. iOS7界面的兼容性调整之一

      if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {   self.edgesForExtendedLayout ...

  8. 关于Apache日志的统计

    统计apache日志文件里访问量前十的ip并按从多到少排列 五月 31, 2012 by FandLR   Filed under Linux Leave a comment 解法1: cat acc ...

  9. zabbix3.x web设置手册(2)

    在浏览器中输入:http://10.50.32.48/zabbix/setup.php 如下图: 点击Next step: 如上图,右侧全为ok,则点击Next step:若右侧有fail的情况,需要 ...

  10. 循环处理--sqlserver

    alter PROCEDURE [dbo].[sp_gongzi] @gongzi_yf varchar(7) as DECLARE  @input_id varchar(20)DECLARE  @s ...