OnMouseDown
From Unify Community Wiki
(Difference between revisions)
m (add link to spin off OnTouch) |
Isaiah Kelly (Talk | contribs) (updated example code) |
||
Line 3: | Line 3: | ||
Spin Off: [[OnTouch]] | Spin Off: [[OnTouch]] | ||
− | This | + | This script sends OnMouseDown messages based on taps, so you can use OnMouseDown callbacks. Attach the script to the camera that is rendering the clickable (tappable) objects. Add a layer argument to the Raycast call if you need to avoid unnecessary intersections. A similar script can be implemented for OnMouseUp (an exercise for the reader). This is only for 3D objects with colliders (e.g. this is used for the 3D menus in HyperBowl), not for GUIText and GUITexture. |
+ | == C Sharp (C#) Script == | ||
+ | <syntaxhighlight lang="csharp"> | ||
+ | using UnityEngine; | ||
+ | |||
+ | /// <summary> | ||
+ | // Allows "OnMouseDown()" events to work on touch devices. | ||
+ | /// </summary> | ||
+ | public class MouseDownTouch : MonoBehaviour | ||
+ | { | ||
+ | private void Update () | ||
+ | { | ||
+ | var hit = new RaycastHit (); | ||
+ | |||
+ | for (int i = 0; i < Input.touchCount; ++i) | ||
+ | { | ||
+ | if (Input.GetTouch (i).phase.Equals (TouchPhase.Began)) | ||
+ | { | ||
+ | // Construct a ray from the current touch coordinates. | ||
+ | Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (i).position); | ||
+ | |||
+ | if (Physics.Raycast (ray, out hit)) | ||
+ | { | ||
+ | hit.transform.gameObject.SendMessage ("OnMouseDown"); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == UnityScript / JavaScript == | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
function Update () { | function Update () { | ||
Line 18: | Line 49: | ||
} | } | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 15:59, 6 September 2018
Spin Off: OnTouch
This script sends OnMouseDown messages based on taps, so you can use OnMouseDown callbacks. Attach the script to the camera that is rendering the clickable (tappable) objects. Add a layer argument to the Raycast call if you need to avoid unnecessary intersections. A similar script can be implemented for OnMouseUp (an exercise for the reader). This is only for 3D objects with colliders (e.g. this is used for the 3D menus in HyperBowl), not for GUIText and GUITexture.
[edit] C Sharp (C#) Script
using UnityEngine; /// <summary> // Allows "OnMouseDown()" events to work on touch devices. /// </summary> public class MouseDownTouch : MonoBehaviour { private void Update () { var hit = new RaycastHit (); for (int i = 0; i < Input.touchCount; ++i) { if (Input.GetTouch (i).phase.Equals (TouchPhase.Began)) { // Construct a ray from the current touch coordinates. Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (i).position); if (Physics.Raycast (ray, out hit)) { hit.transform.gameObject.SendMessage ("OnMouseDown"); } } } } }
[edit] UnityScript / JavaScript
function Update () { var hit : RaycastHit; for (var i = 0; i < iPhoneInput.touchCount; ++i) { if (iPhoneInput.GetTouch(i).phase == iPhoneTouchPhase.Began) { // Construct a ray from the current touch coordinates var ray = camera.ScreenPointToRay (iPhoneInput.GetTouch(i).position); if (Physics.Raycast (ray,hit)) { hit.transform.gameObject.SendMessage("OnMouseDown"); } } } }