OnMouseDown

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
Line 17: Line 17:
 
}
 
}
 
</javascript>
 
</javascript>
 +
 +
-
 +
 +
Unity.js Updated for Unity3.0:
 +
<javascript>
 +
// OnTouchDown.js
 +
// Allows "OnMouseDown()" events to work on the iPhone.
 +
// Attack to the main camera.
 +
 +
#pragma strict
 +
#pragma implicit
 +
#pragma downcast
 +
 +
function Update () {
 +
// Code for OnMouseDown in the iPhone. Unquote to test.
 +
var hit : RaycastHit;
 +
for (var i = 0; i < Input.touchCount; ++i) {
 +
if (Input.GetTouch(i).phase == TouchPhase.Began) {
 +
// Construct a ray from the current touch coordinates
 +
var ray = camera.ScreenPointToRay (Input.GetTouch(i).position);
 +
if (Physics.Raycast (ray,hit)) {
 +
hit.transform.gameObject.SendMessage("OnMouseDown");
 +
      }
 +
  }
 +
  }
 +
}
 +
</javascript>
 +
 +
 +
C# Updated for Unity3.0:
 +
<csharp>
 +
// OnTouchDown.cs
 +
// Allows "OnMouseDown()" events to work on the iPhone.
 +
// Attack to the main camera.
 +
 +
using UnityEngine;
 +
using System.Collections;
 +
using System.Collections.Generic;
 +
 +
public class OnTouchDown : MonoBehaviour {
 +
 +
void Update () {
 +
// Code for OnMouseDown in the iPhone. Unquote to test.
 +
RaycastHit hit;
 +
for (var i = 0; i < Input.touchCount; ++i) {
 +
if (Input.GetTouch(i).phase == TouchPhase.Began) {
 +
// Construct a ray from the current touch coordinates
 +
var ray = camera.ScreenPointToRay (Input.GetTouch(i).position);
 +
if (Physics.Raycast (ray,hit)) {
 +
hit.transform.gameObject.SendMessage("OnMouseDown");
 +
      }
 +
  }
 +
  }
 +
}
 +
}
 +
</csharp>

Revision as of 09:05, 14 October 2010


This Javascript sends OnMouseDown messages based on iPhone 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.

<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");
     }
  }
  }

} </javascript>

-

Unity.js Updated for Unity3.0: <javascript> // OnTouchDown.js // Allows "OnMouseDown()" events to work on the iPhone. // Attack to the main camera.

  1. pragma strict
  2. pragma implicit
  3. pragma downcast

function Update () { // Code for OnMouseDown in the iPhone. Unquote to test. var hit : RaycastHit; for (var i = 0; i < Input.touchCount; ++i) { if (Input.GetTouch(i).phase == TouchPhase.Began) { // Construct a ray from the current touch coordinates var ray = camera.ScreenPointToRay (Input.GetTouch(i).position); if (Physics.Raycast (ray,hit)) { hit.transform.gameObject.SendMessage("OnMouseDown"); } }

  }

} </javascript>


C# Updated for Unity3.0: <csharp> // OnTouchDown.cs // Allows "OnMouseDown()" events to work on the iPhone. // Attack to the main camera.

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class OnTouchDown : MonoBehaviour {

void Update () { // Code for OnMouseDown in the iPhone. Unquote to test. RaycastHit hit; for (var i = 0; i < Input.touchCount; ++i) { if (Input.GetTouch(i).phase == TouchPhase.Began) { // Construct a ray from the current touch coordinates var ray = camera.ScreenPointToRay (Input.GetTouch(i).position); if (Physics.Raycast (ray,hit)) { hit.transform.gameObject.SendMessage("OnMouseDown"); } } } } } </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox