TouchKit icon indicating copy to clipboard operation
TouchKit copied to clipboard

Help with custom script using Touchkit

Open cstobler opened this issue 8 years ago • 1 comments

I am creating a 2D game, and I am wanting to use touch screen gestures to change game cameras. I don't know if this is something that can be implemented easily. I have been working on this all afternoon, and I need some outside help. I am a complete noob to C#, so I may have a number of issues that would be problematic. I was thinking that I could use some code kinda like this to make this work:

public Camera camorig;
public Camera camleft;
public Camera camright;

void Update () {

		CameraTransition cameraTransition = GameObject.FindObjectOfType<CameraTransition>();
		if (cameraTransition == null)
			Debug.LogWarning(@"CameraTransition not found.");

		var recognizer = new TKSwipeRecognizer();
		recognizer.gestureRecognizedEvent += ( r ) =>
		{
			Debug.Log( "swipe recognizer fired: " + r );
		} ;
		TouchKit.addGestureRecognizer( recognizer );

		if (state == TKGestureRecognizerState.Recognized) {
			
			if (completedSwipeDirection == TKSwipeDirection.Left) {

				cameraTransition.DoTransition (CameraTransitionEffects.Fold, camorig, camleft, 1.0f);
				 
			} else if (completedSwipeDirection == TKSwipeDirection.Right) {

				cameraTransition.DoTransition (CameraTransitionEffects.Fold, camorig, camright, 1.0f);

			} else {

				Debug.Log (@"Swipe not successful");
			}
		}

		else {

			Debug.Log (@"Gesture not recognized");
		}
	}

You can see that I am using another addon to facilitate transitions between cameras. Would something like this work, or am I completely off? I didn't know if I could use variables like "state" and "completedSwipeDirection" from the swipe recognizer, so that might be an issue. Because of that, I duplicated the recognizer and simply added this code to it, intending to attach the recognizer to my game object, but I wasn't able to attach it because it didn't derive from MonoDevelop. Any help would be much appreciated!

cstobler

cstobler avatar Aug 31 '17 21:08 cstobler

I wouldn't put this code in an Update loop. Your calling FindObjectOfType which is performance intensive.

When you call "var recognizer = new TKSwipeRecognizer();" and "TouchKit.addGestureRecognizer( recognizer );" you are effectively creating a new instance of the swipe recognizer. This should only be done once. Once it is made you should do what you've done above and create a listener for the event "gestureRecognizedEvent". Once you are listening for a swipe with the TKSwipeRecognizer and a swipe has been completed, the TKSwipeRecognizer will make a callback. The code for this callback (with respect to your code) is inside of this:

recognizer.gestureRecognizedEvent += ( r ) => { Debug.Log( "swipe recognizer fired: " + r ); } ;

the "r" argument that is being passed in through the system event method, is the TKSwipeRecognizer instance. You can access what kind of swipe it is from that variable, specifically the property completedSwipeDirection is the TKSwipeDirection.

ddempseyCard avatar Oct 19 '17 17:10 ddempseyCard