- Home /
Question by
ppddgg · Jul 11, 2012 at 08:48 PM ·
c#inputtouchscreen
C# Touch Script - Fix GameObject touch.
Hi. I have this script (is not created by me). It only works with GUITexture. How can I do to make it work even with GameObject in the Game
using UnityEngine;
[RequireComponent(typeof(GUITexture))]
public class OtherObjectTouch : MonoBehaviour
{
private GUITexture _gui;
public Vector2 MousePosition;
private bool _buttonEnabled;//This means this button can only be 'pressed' once a frame.
void Start()
{
//transform.localScale = Vector3.zero;//for some retarded reason gui elements scale from 0 not 1.
_gui = GetComponent<GUITexture>();
//gui.pixelInset = new Rect(_gui.pixelInset.x, _gui.pixelInset.y, _gui.texture.width, _gui.texture.height);//make sure it's pixel perfect sized.
}
void Update()
{
_buttonEnabled = false;//We reset the button.
foreach (Touch touch in Input.touches)
{
if (_buttonEnabled) return;//don't go any further if this button is pressed.
if (!_gui.HitTest(touch.position)) continue;//do hit test, we 'press' it??
switch (touch.phase)
{
case TouchPhase.Began:
case TouchPhase.Moved:
case TouchPhase.Stationary:
ButtonPressed();//We hit the button!
return;//Return completely out of the update.
}
}
//NOTE: PC TESTING
if (_buttonEnabled) return;//don't go any further if this button is pressed.
MousePosition = Input.mousePosition;
if (!Input.GetMouseButton(0)) return;//Bugger off if no mouse press.
if (!_gui.HitTest(MousePosition)) return;
ButtonPressed(); //We hit the button!
}
private void ButtonPressed()
{
buttonEnabled = true;//This button has been hit.
//Debug.Log("Do incremental action here.");
Application.LoadLevel("Menu");
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613074045im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multi - touch not registering during update. 0 Answers
Multiple Cars not working 1 Answer
When i press a button on the game scene its starts the game, how i can solve it ? 0 Answers
Distribute terrain in zones 3 Answers
Click to move issue. 2 Answers