惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
博客园 - 司徒正美
D
Darknet – Hacking Tools, Hacker News & Cyber Security
量子位
N
News | PayPal Newsroom
S
Secure Thoughts
酷 壳 – CoolShell
酷 壳 – CoolShell
L
LINUX DO - 热门话题
有赞技术团队
有赞技术团队
V
Visual Studio Blog
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Project Zero
Project Zero
B
Blog RSS Feed
J
Java Code Geeks
Google Online Security Blog
Google Online Security Blog
Last Week in AI
Last Week in AI
Cyberwarzone
Cyberwarzone
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
小众软件
小众软件
博客园 - 【当耐特】
Latest news
Latest news
T
Threat Research - Cisco Blogs
aimingoo的专栏
aimingoo的专栏
博客园_首页
博客园 - 三生石上(FineUI控件)
Engineering at Meta
Engineering at Meta
D
Docker
Forbes - Security
Forbes - Security
Help Net Security
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V2EX - 技术
V2EX - 技术
N
Netflix TechBlog - Medium
The Last Watchdog
The Last Watchdog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threatpost
Cloudbric
Cloudbric
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
Webroot Blog
Webroot Blog

博客园 - 鱼丸粗面

《Unity預計算即時GI》笔记:三、Clusters和总结 《Unity預計算即時GI》笔记:二、光照图 - 鱼丸粗面 - 博客园 《Unity預計算即時GI》笔记:一、基本概念与一些设置 - 鱼丸粗面 - 博客园 我所了解的法线贴图 - 鱼丸粗面 - 博客园 游戏设计模式读书笔记:命令模式 - 鱼丸粗面 - 博客园 游戏设计模式读书笔记:架构、性能、游戏 一个美术需求引发的Custom Inspector 代码生成AnimatorController unity生成的WP8.1工程的Title本地化实现 unity生成WP工程后ExtendedSplashImage显示不正确的问题 Unity5.x在WP8.1中无法使用Reflection API的解决方法 使用uGUI制作游戏内2D动画 Unity3D Editor模式下批量修改prefab Unity中的协程是什么? Unity3D脚本调用Objective C代码实现游戏内购买 WindowsPhone8拍照功能实现简介 WindowsPhone App如何扩展能够使用的内存 prism关键概念: objective-c:继承
3D touch在Unity3D中的使用
鱼丸粗面 · 2015-10-24 · via 博客园 - 鱼丸粗面

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 0;
    }
    if(self.rootViewController.view.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
    {
        isSupport3DTouch = YES;
        return 1;
    } else {
        isSupport3DTouch = NO;
        return 0;
    }
}

下面是响应压力变化的处理函数,这次用传递函数指针到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(0, 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