3D touch在Unity3D中的使用,touchunity3d

來源:互聯網
上載者:User

3D touch在Unity3D中的使用,touchunity3d

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

 

 

 

 

     

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.