/* * Method that will be called by native end if ForceTouch state changes. You must * subscribe to that event by calling AddCallback method. For this method to be called, * InputManager component has to be in hierarchy. * See AddCallbackMethod in ForceTouchPlugin.cs for more info */ public void UpdateForceTouch(string message) { if (GetComponent <StatusUI>()) //this is only required for StatusUI. { GetComponent <StatusUI>().callbackMessage = message; } forceTouchState = ForceTouchPlugin.GetForceTouchState(Int32.Parse(message)); }
public void Start() { //during awake of first scene force touch state is not yet available. If InputManager is initialized later it's ok to call this in Awake. //For platforms other than iOS ForceTouchState.IncompatibleOS will be returned. forceTouchState = ForceTouchPlugin.GetForceTouchState(); //if (forceTouchState != ForceTouchState.IncompatibleOS) // AddCallback (); }
public void Awake() { instance = this; Application.targetFrameRate = 60; //iOS by default runs 30fps //retrieve screen scale (iOS operates in lowDPI mode) nativeTouchScale = ForceTouchPlugin.GetScaleFactor(); //checks whether current device supports TouchRadius (iOS 8 and above) supportsTouchRadius = ForceTouchPlugin.SupportsTouchRadius(); //prepare to store touches touches = new List <NativeTouch> (); }
public void Start() { //during awake of first scene force touch state is not yet available. If InputManager is initialized later it's ok to call this in Awake. //For platforms other than iOS ForceTouchState.IncompatibleOS will be returned. forceTouchState = ForceTouchPlugin.GetForceTouchState(); //it would be best to only start tracking if forceTouchState is Available or if supportsTouchRadius is true. //If native touches are used on old ios devices or devices that have force touch disabled it's a performance penalty that could be avoided. //if (forceTouchState == ForceTouchState.Available) { ForceTouchPlugin.StartTracking(); //won't be executed on anything but iOS so no need to add compiler conditionals //AddCallback(); } }
void Update() { //the code below is only executed on devices supporting 3d touch if (!supportsForceTouch) { return; } //if down and touch is not mouse if (down && (touchId != -1)) { //use scale to provide visual feedback. //this is not crucial, because popup provides better visual feedback var t = ForceTouchPlugin.GetTouchExtraData(touchId); //is the same as Input.GetTouch(touchId).GetExtraData(); this.gameObject.transform.localScale = Vector3.one * Mathf.Lerp(1f, 1.2f, Mathf.Clamp(t.force / (t.maxforce / 2f), 0f, 1f)); //code above could be replaced with this as well, but above call has fewer function calls and is JUST A BIT more efficient //var t = Input.GetTouch (touchId); //this.gameObject.transform.localScale = Vector3.one * Mathf.Lerp (1f, 1.2f, Mathf.Clamp (t.GetForce() / (t.GetMaxForce() / 2f), 0f, 1f)); popup.SetForce(t.force, t.maxforce); } }
void OnGUI() { var nativeTouchScale = LegacyInputManager.instance.nativeTouchScale; StringBuilder output = new StringBuilder(); switch (currentInput) { case 1: output.AppendLine("<b>Input:</b> Native Touch"); break; case 2: output.AppendLine("<b>Input:</b> Unity Touch"); break; case 3: output.AppendLine("<b>Input:</b> Mouse"); break; default: output.AppendLine("<b>Input:</b> Undefined (Mouse / Unity Touch)"); break; } output.AppendLine("<b>Supports Touch Radius:</b> " + LegacyInputManager.instance.supportsTouchRadius); switch (LegacyInputManager.instance.forceTouchState) { case ForceTouchState.Available: output.AppendLine("<b>Force Touch State:</b> Available"); break; case ForceTouchState.Unknown: output.AppendLine("<b>Force Touch State:</b> Unknown"); break; case ForceTouchState.IncompatibleOS: output.AppendLine("<b>Force Touch State:</b> Incompatible OS"); break; case ForceTouchState.Unavailable: output.AppendLine("<b>Force Touch State:</b> Unavailable"); break; } output.AppendLine("<b>Callback Message:</b> " + callbackMessage); if (LegacyInputManager.instance.touches.Count > 0) { output.AppendLine("<b>Touches:</b>"); for (int i = 0; i < LegacyInputManager.instance.touches.Count; i++) { output.AppendLine(LegacyInputManager.instance.touches [i].ToString()); } } else { output.AppendLine("<b>Touches:</b> No touches available"); } GUI.skin.label.alignment = TextAnchor.UpperLeft; GUI.color = Color.black; GUI.Label(new Rect(10 * nativeTouchScale, 10 * nativeTouchScale, Screen.width - 20 * nativeTouchScale, Screen.height - 20 * nativeTouchScale), "<size=" + 16 * nativeTouchScale + ">" + output.ToString() + "</size>"); GUI.color = Color.white; if (LegacyInputManager.instance.useNativeTouches) { if (GUI.Button(new Rect(10 * nativeTouchScale, Screen.height - ((10 + 40) * nativeTouchScale), Screen.width / 2 - 15 * nativeTouchScale, 40 * nativeTouchScale), "<size=" + 16 * nativeTouchScale + ">Switch Unity Input</size>")) { LegacyInputManager.instance.useNativeTouches = false; } } else { if (GUI.Button(new Rect(10 * nativeTouchScale, Screen.height - ((10 + 40) * nativeTouchScale), Screen.width / 2 - 15 * nativeTouchScale, 40 * nativeTouchScale), "<size=" + 16 * nativeTouchScale + ">Switch Native Input</size>")) { LegacyInputManager.instance.useNativeTouches = true; } } if (GUI.Button(new Rect(Screen.width / 2 + 5 * nativeTouchScale, Screen.height - ((10 + 40) * nativeTouchScale), Screen.width / 2 - 15 * nativeTouchScale, 40 * nativeTouchScale), "<size=" + 16 * nativeTouchScale + ">Request FT State</size>")) { LegacyInputManager.instance.forceTouchState = ForceTouchPlugin.GetForceTouchState(); } if (callbackAdded) { if (GUI.Button(new Rect(10 * nativeTouchScale, Screen.height - (100 * nativeTouchScale), Screen.width - 20 * nativeTouchScale, 40 * nativeTouchScale), "<size=" + 16 * nativeTouchScale + ">Remove FT State Change Callback</size>")) { callbackMessage = "No message received"; LegacyInputManager.instance.RemoveCallback(); callbackAdded = false; } } else { if (GUI.Button(new Rect(10 * nativeTouchScale, Screen.height - (100 * nativeTouchScale), Screen.width - 20 * nativeTouchScale, 40 * nativeTouchScale), "<size=" + 16 * nativeTouchScale + ">Add FT State Change Callback</size>")) { callbackMessage = "No message received"; LegacyInputManager.instance.AddCallback(); callbackAdded = true; } } }
/* * Removes this component as event handler for ForceTouch state change event. */ public void RemoveCallback() { ForceTouchPlugin.RemoveCallbackMethod(); }
/* * Method used to subscribe this component as event handler for ForceTouch state change (use car disable/enable ForceTouch/3D touch in accessibility settings). */ public void AddCallback() { ForceTouchPlugin.SetCallbackMethod(this.name, "UpdateForceTouch"); //won't be executed on anything but iOS so no need to add compiler conditionals }
/* * Refresh touch data. */ public void Update() { if (GetComponent <LegacyStatusUI>()) //this is only required for StatusUI. { GetComponent <LegacyStatusUI>().currentInput = -1; } if (useNativeTouches) //this will give false for all platforms except iOS. On iOS it will give true if StartTracking was called. { /* * If you don't need to handle touch state, you can simply replace the code bellow with the following code: * touches.Clear(); * * touches.AddRange(nativeTouches); * * if (GetComponent<StatusUI>()) //this is only required for StatusUI. * GetComponent<StatusUI>().currentInput = 1; */ UpdateNativeTouches(); var nativeTouches = ForceTouchPlugin.GetNativeTouches(); for (var i = 0; i < nativeTouches.Count; i++) { HandleNativeInput(nativeTouches[i]); } if (GetComponent <LegacyStatusUI>()) //this is only required for StatusUI. { GetComponent <LegacyStatusUI>().currentInput = 1; } } else if (Input.touchCount > 0) //if native touch isn't available fallback to unity touch { touches.Clear(); for (int i = 0; i < Input.touchCount; i++) { var t = Input.touches [i]; HandleInput(t.fingerId, t.phase, t.position, t.deltaPosition, t.GetForce(), t.GetMaxForce(), t.GetRadius(), t.GetRadiusTolerance()); } if (GetComponent <LegacyStatusUI>()) //this is only required for StatusUI. { GetComponent <LegacyStatusUI>().currentInput = 2; } } else //if unity touch is unavailable fallback to mouse input { touches.Clear(); var temp = new NativeTouchExtraData(); var mousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y); if (Input.GetMouseButtonDown(0)) { HandleInput(mouseId, TouchPhase.Began, mousePos, Vector2.zero, temp.force, temp.maxforce, temp.radius, temp.radiusTolerance); } else if (Input.GetMouseButtonUp(0)) { HandleInput(mouseId, TouchPhase.Ended, mousePos, mousePos - lastMousePos, temp.force, temp.maxforce, temp.radius, temp.radiusTolerance); } else if (Input.GetMouseButton(0)) { var delta = mousePos - lastMousePos; if (delta != Vector2.zero) { HandleInput(mouseId, TouchPhase.Moved, mousePos, delta, temp.force, temp.maxforce, temp.radius, temp.radiusTolerance); } else { HandleInput(mouseId, TouchPhase.Stationary, mousePos, delta, temp.force, temp.maxforce, temp.radius, temp.radiusTolerance); } } else { return; } lastMousePos = Input.mousePosition; if (GetComponent <LegacyStatusUI>()) //this is only required for StatusUI. { GetComponent <LegacyStatusUI>().currentInput = 3; } } }
/// <summary> /// Extension method for Touch class. Used to retrieve radius tolerance of this touch. Radius tolerance is differs among devices. /// </summary> /// <returns>Touch radius tolerance.</returns> /// <b>Example</b> /// <code> /// Input.touches[0].GetRadiusTolerance(); /// </code> public static float GetRadiusTolerance(this Touch touch) { return(ForceTouchPlugin.GetTouchExtraData(touch.fingerId).radiusTolerance); }
/// <summary> /// Extension method for Touch class. Used to retrieve maximum possible force of this touch. MaxForce might be different for different devices. /// </summary> /// <returns>Maximum possible force of this touch.</returns> /// <b>Example</b> /// <code> /// Input.touches[0].GetMaxForce(); /// </code> public static float GetMaxForce(this Touch touch) { return(ForceTouchPlugin.GetTouchExtraData(touch.fingerId).maxforce); }
/// <summary> /// Extension method for Touch class. Used to retrieve force and radius data. /// </summary> /// <returns>NativeTouchExtraData object for this touch containing force and radius data</returns> /// <b>Example</b> /// <code> /// Input.touches[0].GetExtraData(); /// </code> public static NativeTouchExtraData GetExtraData(this Touch touch) { return(ForceTouchPlugin.GetTouchExtraData(touch.fingerId)); }
public void Start() { //this is important to differentiate behavior supportsForceTouch = ForceTouchPlugin.SupportsForceTouch(); }