0. Opening:
3D touch with iOS9 release, it is not a separate technology, but can be divided into pressure sensitivity,quick action and Peek&pop. in the official presentation, it was mentioned that a better experience could be given to the game, but actually personally feel that in addition to pressure sensitivity can change the way the game works,quick action and peek& Pop is really designed for the app.
1, the use of pressure sensitivity:
First, add a function in Unity's script to check if 3D touch is supported, which is essentially calling iOS code.
[DllImport ("__internal")] // return 1 When device was support 3d Touch Private Static extern int publicstaticint 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 response to the pressure change of the processing function, this time with the transfer function pointer to the OC code to do, of course, you can also use the Unitysendmessage method in iOS.
Private Delegate voidTouch_event_callback_delegate (floatForcefloatmaximumpossibleforce);Private Staticaction<float,float>Toucheventcallback; [DllImport ("__internal")]Private Static extern void_registtoucheventcallback (touch_event_callback_delegate func); Public Static voidRegisttoucheventcallback (action<float,float>func) {Toucheventcallback=func; _registtoucheventcallback (Executetoucheventcallback); }[monopinvokecallback (typeof(touch_event_callback_delegate))]Private Static voidExecutetoucheventcallback (floatForcefloatMaximumpossibleforce) {toucheventcallback (force, Maximumpossibleforce); }
The corresponding iOS code is
void (*registtoucheventcallbackfunc) (floatfloat); static Registtoucheventcallbackfunc toucheventcallback = nil; -(void) Registtoucheventcallback: (registtoucheventcallbackfunc) func{ = func;}
Unity-generated Xcode project has a unityview.mm file that needs to be modified in order to get pressure changes in 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 and The touchesendorcancelled is defined as:
/** * Real-time feedback pressure sense * * @param touches touch data*/+(void) Updateforce: (Nsset<uitouch *>*) touches{if(Issupport3dtouch && Toucheventcallback! =Nil) {Toucheventcallback (Touches.anyObject.force, Touches.anyObject.maximumPossibleForce); } }/** * processing when touchesended or touchescancelled is triggered*/+(void) touchesendorcancelled: (Nsset<uitouch *>*) touches{if(Issupport3dtouch && Toucheventcallback! =Nil) {Toucheventcallback (0, Touches.anyObject.maximumPossibleForce); }}
In fact, with Unitysendmessage is the simplest, in touchesendorcancelled force directly assigned to 0 because I found in the process of testing a quick click and left the screen sometimes get the force is not 0, This can be problematic when using the force in the game.
2. Quick Action Application
Now the idea is to quickly enter a game scene, or enter the game to open a UI directly, in short, the game is not helpful. What I did in the demo was to get into scene 2 quickly, and the default should be to enter scene 1. The first thing you need to do is set up 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 the Uiapplicationshortcutitemuserinfo, because the parameters we get are obtained from userinfo. There is very little programming in unity when using the quick action, primarily iOS programming.
The first thing you need to add in 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]); } returnhandled;}
This system method is used to process the use of quick action in screen to enter the game. Read a lot of other people write examples, in the didfinishlaunchingwithoptions will call Handleshortcutitem, and then return no, so as to avoid performactionforshortcutitem calls. But actually in the test it is found that it is not necessary to call Handleshortcutitem in Didfinishlaunchingwithoptions.
3, Peek&pop
There was no thought of how to use the game, and it was found that there was a vague mask layer in peek.
4, demo address: Https://github.com/klkucan/Unity_For3DTouch
The use of 3D touch in Unity3d