| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
TouchScript comes with many example scenes, each of them demonstrates how to use various framework features.
/Scene GameObject has the main logic and 3 TouchScript components:
private void OnEnable()
{
TwoFingerMoveGesture.Transformed += twoFingerTransformHandler;
ManipulationGesture.Transformed += manipulationTransformedHandler;
}
private void OnDisable()
{
TwoFingerMoveGesture.Transformed -= twoFingerTransformHandler;
ManipulationGesture.Transformed -= manipulationTransformedHandler;
}
private void manipulationTransformedHandler(object sender, System.EventArgs e)
{
var rotation = Quaternion.Euler(ManipulationGesture.DeltaPosition.y/Screen.height*RotationSpeed,
-ManipulationGesture.DeltaPosition.x/Screen.width*RotationSpeed,
ManipulationGesture.DeltaRotation);
pivot.localRotation *= rotation;
cam.transform.localPosition += Vector3.forward*(ManipulationGesture.DeltaScale - 1f)*ZoomSpeed;
}
private void twoFingerTransformHandler(object sender, System.EventArgs e)
{
pivot.localPosition += pivot.rotation*TwoFingerMoveGesture.DeltaPosition*PanSpeed;
}/Scene/Camera GameObject has a StandardLayer with all options disabled but Hit 3D Objects in Hit test options property to raycast only for 3D objects in the scene, since this scene doesn't have any 2D or UI objects.
/Scenes/Board GameObject has two components attached:
private void OnEnable()
{
gesture = GetComponent<PinnedTransformGesture>();
gesture.Transformed += transformedHandler;
}
private void OnDisable()
{
gesture.Transformed -= transformedHandler;
}
private void transformedHandler(object sender, System.EventArgs e)
{
transform.localRotation *= Quaternion.AngleAxis(gesture.DeltaRotation, gesture.RotationAxis);
}/Scene/Board/Container/CheckerDark GameObjects have the following components attached:
/scene GameObject has Exclusive component attached to it which implements IGestureDelegate interface and assigns itself to GestureManager.Instance.GlobalGestureDelegate. When a user presses Space this component prevents all gestures starting except the ones on Target GameObject using this code:
public bool ShouldBegin(Gesture gesture)
{
if (exclusive) return gesture == Target;
return true;
}
public bool ShouldReceivePointer(Gesture gesture, Pointer pointer)
{
if (exclusive) return gesture == Target;
return true;
}/Scene/Camera GameObject has a StandardLayer with all options disabled but Hit 2D Objects in Hit test options property to raycast only for 2D objects in the scene, since this scene doesn't have any 3D or UI objects.
Circle prefab has the following components attached:
GetComponent<TransformGesture>().Cancel(true, true);(true, true) means that the pointers should be returned to the system, like they were just pressed. At this point cancelled pointers are caught by the new (bigger) circle and its TransformGesture so the user can continue moving and resizing the new circle.
Notice how you are touching the Cube but are able to move the image far away in the scene.
This example shows how extensible TouchScript is. There's a cube in the scene which has a RenderTexture from an offscreen camera assigned to its material. But you can still move and resize this image with your fingers.
/Scene/Scene Camera/Image GameObject has TransformGesture and Transformer components to move it around. This is the image you see on the Cube.
/Scene/Cube GameObject has the following components attached:
private void pointerPressedHandler(object sender, MetaGestureEventArgs metaGestureEventArgs)
{
var pointer = metaGestureEventArgs.Pointer;
if (pointer.InputSource == (IInputSource)this) return;If this pointer is generated by this script, stop processing it.
var newPointer = PointerFactory.Create(pointer.Type, this);
newPointer.CopyFrom(pointer);
newPointer.Position = processCoords(pointer.GetPressData().RaycastHit.textureCoord);
newPointer.Flags = pointer.Flags | Pointer.FLAG_ARTIFICIAL | Pointer.FLAG_INTERNAL;
addPointer(newPointer);
pressPointer(newPointer);
map.Add(pointer.Id, newPointer);
}Next, the code creates a copy pointer, adds FLAG_INTERNAL to hide it from Cursor Visualizer, then adds and presses it. At this point the touch is injected into the system.
/Scene GameObject has the following components attached:
This is the code inside LayerDelegate:
public RedirectInput Source;
public TouchLayer RenderTextureLayer;
public bool ShouldReceivePointer(TouchLayer layer, IPointer pointer)
{
if (layer == RenderTextureLayer)
return pointer.InputSource == (IInputSource)Source;
return pointer.InputSource != (IInputSource)Source;
}Which basically says: "The layer on RenderTexture Camera should receive fake pointers, while others don't".
The scene contains 5 layers. Check TouchManager GameObject with TouchManager script attached which has a list of layers. The layers must be sorted in the specific order: 2D, Right and Left 3D Camera, Right and Left Fullscreen layers. This is because layers are iterated from top to bottom when trying to determine what object a pointer hits.
/Scene/2D/Unity_Logo GameObjects have TapGesture on them which are used by Logo component to change color on tap.
/Scene/Left/Board and /Scene/Right/Board are the same as in Checkers example.
/Scene/World Space Plane/World Space Canvas/Field/Image # GameObjects have the following components attached:
/Scene/World Space Plane/World Space Canvas/Field/Image #/Close is just a UI Button which TouchScript sends pointer input.
/Scene/ScreenSpace Canvas/Panel/Button is just a set of UI Buttons which interact with TouchScript. No special components or code is required.
This example demonstrates how to do the following:
/Game Canvas/Spawner# GameObjects have the following components attached:
var target = Instantiate(Prefab, Position.parent);
target.position = Position.position;
LayerManager.Instance.SetExclusive(target);
press.Cancel(true, true);
LayerManager.Instance.ClearExclusive();This code creates an instance of Prefab and sets it exclusive so no other object can receive pointers. After that the code cancels the gesture and returns its pointers to the system which immediately go our object.
/Scene/Camera/Stuff/Vortex has a script attached which calls planet.Fall(); on every planet which comes close. This method basically does the following:
var gesture = GetComponent<TransformGesture>();
if (gesture != null) gesture.Cancel();It cancels the gesture but doesn't cancel its pointers, so the planes just fall into the vortex if the user doesn't catch them quickly.
This example shows how to use raw pointer input with no gesture logic. /Scene/Camera has Spawner script attached which spawns prefab instances using this code:
private void OnEnable()
{
if (TouchManager.Instance != null)
TouchManager.Instance.PointersPressed += pointersPressedHandler;
}
private void OnDisable()
{
if (TouchManager.Instance != null)
TouchManager.Instance.PointersPressed -= pointersPressedHandler;
}
private void pointersPressedHandler(object sender, PointerEventArgs e)
{
foreach (var pointer in e.Pointers)
spawnPrefabAt(pointer.Position);
}/Scene/Container/Cube GameObjects have the following components attached:
longPressGesture.StateChanged += longPressedHandler;
private void longPressedHandler(object sender, GestureStateChangeEventArgs e)
{
if (e.State == Gesture.GestureState.Recognized)
{
...
}
else if (e.State == Gesture.GestureState.Failed)
{
stopGrowing();
}
}We use StateChanged event here instead of simpler LongPressed because we need to handle GestureState.Failed.
/Scene/table_and_chair GameObject has a TapGesture component attached with Number of Taps Required set to Two and Limit Time to 1 sec. This means that we want a double-tab with 1 sec max interval between taps. Spawn component uses the TapGesture to spawn new cubes.
This example shows how to write and use custom gestures.
It is available as a separate tutorial.
| Back | FazBrowse Home | New Git URL |