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 预览收件箱中 ...
随机推荐
- Mini projects #8–RiceRocks
课程全名:An Introduction to Interactive Programming in Python,来自 Rice University 授课教授:Joe Warren, Scott ...
- java7笔记
1 switch case 增加字符串类型,原理根据字符串hashcode以及string.equal比较2 数值字面量改进 新增二进制数值字面量 0b000004,在数值中间新增下划线方便阅读200 ...
- Windows & Office完美结合,助力办公
虚拟桌面——休闲工作分开来 Windows 10最令我欣愉的是加入了虚拟桌面的功能. 作为一名拖延症晚期患者,早已病入膏肓.每次工作时总会不知不觉地将实现转移到已经打开的浏览器及聊天工具上,时间就这样 ...
- 第57讲:Scala中Dependency Injection实战详解
本讲我们来学习下依赖注入.让我们从代码出发: package scala.learn trait Logger {def log (msg:String)}trait Auth { auth:Log ...
- 由XML解析学习工厂模式
代码段1: startupData = new StartupData(); /* 设定自定义的MyHandler给XMLReader */ StartupXMLHandler startupData ...
- Spring 使用JSTL标签显示后台数据
1. 先上项目结构图,其中config包下的代码文件参见前一篇博客 http://www.cnblogs.com/njust-ycc/p/6123505.html 引包: 2. 主要代码 (1)U ...
- 如何编译MongoDB?
本文将在Linux环境下编译Mongodb. 您可以选择已经编译好的版本直接使用,也可以尝试自己编译.https://www.mongodb.org/downloads#production 官方 ...
- java jdb命令详解
jdb - Java debugger 功能描述: 通过简单的命令行程序,对本地或远程jvm进程进行调试. 开启jdb会话: 有多种方式可以开启jdb会话. (1)常见的方式是采用Jdb命令打开一个新 ...
- iOS开发-UINavigationBar透明设置
导航条最近需要设置成透明的形式,最开始想通过颜色clearColor设置,设置透明度,结果发现UINavigationItem无法显示显示,后来通过setBackgroundImage设置成功,不过会 ...
- myrocks之事务处理
前言 mysql目前支持的事务引擎有innodb,tokudb. rocksdb加入mysql阵营后,mysql支持的事务引擎增长至3个.myrocks目前支持的事务隔离级别有read-committ ...