Use of 3D touch in Unity3D, touchunity3d

Source: Internet
Author: User

Use of 3D touch in Unity3D, touchunity3d

0. Opening:

With the release of iOS9, 3D touch is not a separate technology, but can be divided into pressure sensiti.pdf, quick action, and peek & pop. In the official introduction, we mentioned that it can provide a better gaming experience, but in fact, I personally feel that in addition to pressure sensiti.pdf, quick action and peek & pop are designed for apps.

1. Use of pressure sensiti.pdf:

First, add a function in the unity script to check whether 3D touch is supported. In essence, this function calls iOS code.

 [DllImport("__Internal")]    // return 1 when device is support 3d touch    private static extern int _checkForceTouchCapability(); public static int CheckForceTouchCapability()    {        return _checkForceTouchCapability();    }

The corresponding iOS code is

-(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;    }}

The following is a processing function that responds to pressure changes. This time, the function pointer is passed to the oc code. Of course, you can also use the UnitySendMessage method in iOS.

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);    }

The corresponding iOS code is

typedef void (*registTouchEventCallbackFunc)(float, float);static registTouchEventCallbackFunc touchEventCallback = nil;-(void)registTouchEventCallback:(registTouchEventCallbackFunc) func{    touchEventCallback = func;}

The Xcode project generated by unity contains an UnityView. mm file. To obtain pressure changes in iOS, You need to modify the code.

- (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 and TouchesEndorCancelled are defined:
/*** Real-time feedback pressure ** @ param touches touch data */+ (void) UpdateForce :( NSSet <UITouch *> *) touches {if (issupp3d3dtouch & touchEventCallback! = Nil) {touchEventCallback (touches. anyObject. force, touches. anyObject. maximumPossibleForce) ;}}/*** processing when touchesEnded or touchesCancelled is triggered */+ (void) handle :( NSSet <UITouch *> *) touches {if (issupp3d3dtouch & touchEventCallback! = Nil) {touchEventCallback (0, touches. anyObject. maximumPossibleForce );}}

In fact, UnitySendMessage is the simplest. In TouchesEndorCancelled, force is directly assigned 0 because I found that the force obtained when I clicked quickly and left the screen is sometimes not 0, this will cause problems when using this force in the game.

2. quick action application

Now, I want to quickly enter a game scenario, or directly enable a UI after entering the game. In short, it is not helpful for gameplay. In the Demo, I quickly enter scenario 2. The default value is scenario 1. You must first set it in 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>

The core is to set UIApplicationShortcutItemUserInfo, because the parameters we get are obtained from userinfo. When using quick action, unity has very few programming skills, mainly iOS programming.

First, add the following content to 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;}

This system method is used to process the entry into the game using quick action on screen. I have read many examples written by others. In didfinishlaunchingwitexceptions, handle‑cutitem will be called and NO will be returned. This will avoid calling javasmactionfor‑cutitem. However, handle‑cutitem is not required in didFinishLaunchingWithOptions.

 

3. peek & pop

I had no idea how to use it in the game, and found that there was a fuzzy mask layer in peek.

4, Demo address: https://github.com/klkucan/Unity_For3DTouch

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.