|
I am trying to make a button for Android Input.Touch. But it doesn't work. Anyone can help please? Here is the code:
(comments are locked)
|
|
You're mixing different creatures: GUITexture and HitTest belong to the old gui system, while GUI.DrawTexture belongs to the new one - these systems are incompatible with each other, thus you should stick with one or the other.
using UnityEngine;
using System.Collections;
public class AnInp : MonoBehaviour {
void Update () {
Touch touch1;
if(Input.touchCount > 0) {
touch1 = Input.touches[0];
if(touch1.phase == TouchPhase.Began && guiTexture.HitTest(touch1.position)) {
Application.Quit();
}
}
}
}
To use the new GUI system, just follow @J.D.'s suggestion: use GUI.Button instead of GUI.DrawTexture:
using UnityEngine;
using System.Collections;
public class AnInp : MonoBehaviour {
public Texture2D myButton;
void OnGUI() {
if (GUI.Button(new Rect(60,60,60,60), myButton){
Application.Quit();
}
}
}
Thanks for the advice but the button will not quit the app indeed. It will be a throttle button for a car and i need a multitouch script. I will try the new gui system. P.S: Sorry for my bad english.
Aug 18 '12 at 09:18 PM
alee26
I just tried the new gui system and the script. It was great man. Thanks a lot. I loved it. Now the question is can i put multiple guitextures from the gameobject/create menu and does it function multitouch(of coures until you answer me i will be testing:)). P.S: I am trying to make 4 touch buttons for accelaret, brake, left and right actions.
Aug 18 '12 at 09:45 PM
alee26
For multitouch, I guess you must use GUITextures. There's another function, GUILayer.HitTest, that can tell which GUITexture is at some specific point - maybe you could use it for each touch, like this (script attached to an empty object):
using UnityEngine;
using System.Collections;
public class AnInp : MonoBehaviour {
// drag the button GUITextures here:
GUITexture accel;
GUITexture brake;
GUITexture left;
GUITexture right;
void Update () {
if (Input.touchCount > 0){
foreach (Touch touch1 in Input.touches){
// find the element touched:
GUIElement button = GUILayer.HitTest(touch1.position);
// verify which of your buttons is it:
if (button == accel){
// accelerator touch
}
else
if (button == brake){
// brake touch
}
else
if (button == left){
// steer left touch
}
else
if (button == right){
// steer right touch
}
}
}
}
}
Aug 19 '12 at 01:13 AM
aldonaletto
I'm on it at the moment. I will write the result. Thanks for helping me in this project. I have made a good car working on pc but i want to run it on Android and i am working for 10 ten days and had no proper result until your help. You are great :)
Aug 19 '12 at 09:09 PM
alee26
I tried it right now and having this error: Assets/Scripts/AnInp.cs(16,38): error CS0120: An object reference is required to access non-static member `UnityEngine.GUILayer.HitTest(UnityEngine.Vector3)' I think (maybe i'm thinking wrong) GUILayer touch position has to be defined in vectors but how, or the "button" must be an object?
Aug 19 '12 at 09:26 PM
alee26
(comments are locked)
|
Any more suggestions???
Aug 31 '12 at 09:00 PM
alee26
I still need the answer, anyone can help please???
Sep 11 '12 at 08:58 PM
alee26
(comments are locked)
|

I'm not sure HitTest works in Android, since it has no MouseInput and HitTest is based on Mouse inputs. You should raycast your touch OR and much easier, use
if(GUI.Button(new rect(x,y,w,h),textureButton)){ Application.Quit(); }
p.s. this must of course be inside OnGUI() method.
Thanks for all. I need multitouch functions for a car and this will be the throttle in fact.