Skip to content

Instantly share code, notes, and snippets.

@olokobayusuf
Last active June 5, 2017 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olokobayusuf/099ce16a319e9ba01d5852d5d89ea041 to your computer and use it in GitHub Desktop.
Save olokobayusuf/099ce16a319e9ba01d5852d5d89ea041 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using Lean.Touch;
public class LeanFocuser : MonoBehaviour {
public FocusMode focusMode = FocusMode.AutoFocus | FocusMode.TapToFocus;
public bool IsTracking {get; private set;}
#region --Client API--
/// <summary>
/// Start tracking focus gestures on the UI panel that this is attached to
/// </summary>
public void StartTracking () {
// On initialise l'hybrid focus ici
var f = FocusMode.AutoFocus | FocusMode.TapToFocus;
StartTracking(f);
}
/// <summary>
/// Start tracking focus gestures on the UI panel that this is attached to
/// </summary>
/// <param name="focusMode">Focus mode to apply to the camera. Note that this must have
/// the FocusMode.TapToFocus bit set for tap to focus to work</param>
public void StartTracking (FocusMode focusMode) {
if (!NatCam.Camera) return;
this.focusMode = (NatCam.Camera.FocusMode = focusMode);
this.IsTracking = true;
}
/// <summary>
/// Stop tracking focus gestures
/// </summary>
public void StopTracking () {
IsTracking = false;
}
#endregion
protected virtual void OnEnable () {
// Hook into the events we need
LeanTouch.OnFingerDown += OnFingerDown;
}
protected virtual void OnDisable () {
// Unhook the events
LeanTouch.OnFingerDown -= OnFingerDown;
}
private void OnFingerDown (LeanFinger finger) {
// Focus
if (IsTracking && NatCam.Camera) NatCam.Camera.SetFocus(Camera.main.ScreenToViewportPoint(finger.ScreenPosition));
}
}
using UnityEngine;
using Core;
using Lean.Touch;
public class PinchCam : NatCamBehaviour {
[HideInInspector] public bool
// Ignore fingers with StartedOverGui?
IgnoreGuiFingers = true,
// Limitation du zoom ?
ZoomClamp = true;
[HideInInspector] public float
// Le zoom de démarrage :
Zoom = 1.0f,
// Zoom Min
ZoomMin = 1.0f,
// Zoom Max
ZoomMax = 2.0f;
// Allows you to force rotation with a specific amount of fingers (0 = any)
[HideInInspector] public int RequiredFingerCount;
// If you want the mouse wheel to simulate pinching then set the strength of it here
[Range(-1.0f, 1.0f)] public float WheelSensitivity;
protected virtual void LateUpdate () {
// Camera checking
if (!NatCam.Camera) return;
// Get the fingers we want to use
var fingers = LeanTouch.GetFingers(IgnoreGuiFingers, RequiredFingerCount);
// Get the pinch ratio of these fingers
var pinchRatio = LeanGesture.GetPinchRatio(fingers, WheelSensitivity);
// Modify the zoom value
Zoom /= pinchRatio; //En espacant les doigts, on agrandit
// Clamp zoom
if (ZoomClamp) Zoom = Mathf.Clamp(Zoom, ZoomMin, ZoomMax);
// Set the new zoom
NatCam.Camera.ZoomRatio = current;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment