- Home /
guitexture to act as a button on the keyboard or mouse
i ve been looking all over the net and i cant find anything. i ve tried everything and cant get it to work
i want a gui Texture to do say Fire1 or Jump or anything that i have in my InputManager.
i m using gui.buttons and they work fine but i dont want to have to adjust each button on the different resolutions of phones. i find gui textures much easier
this is the gui.button script
#pragma strict
var input : InputItem;
var position : Vector2; //position of button
var dimensions : Vector2; //size of button
var label : String; //text in button
var toggle : boolean = false; //is this button a toggle?
@HideInInspector
var toggled : boolean = false; //are we currently toggled on?
var showInStore : boolean = false;
private var used : boolean = false;
private var touched : boolean = false; //had we already touched the button
private var touching : boolean = false; //are we currently touching the button
@HideInInspector
var curTouch : int = -1; //what touch id is this using?
var useUpdate : boolean = true;
function Update () {
if(useUpdate)
UpdateFunction();
}
function UpdateInput() {
if(!useUpdate)
UpdateFunction();
}
function UpdateFunction () {
//are we touching the button this frame?
if(Input.touches.Length > 0){
for(var touch : Touch in Input.touches){ //for each touch
//Is this touch within our button?
touching = Within(touch.position, Rect(position.x, position.y, dimensions.x,dimensions.y));
if(touching){
curTouch = touch.fingerId; //save which touch we are using
break;
}
}
} else {
touching = false;
}
if(toggle){ //Toggle button
input.got = toggled;
if(touching){
if(!touched){ //first frame touching the button
touched = true;
input.up = toggled;
toggled = !toggled; //invert the toggle
input.down = toggled;
} else {
input.down = false;
input.up = false;
}
} else {
input.down = false;
input.up = false;
touched = false;
curTouch = -1;
}
} else { //Normal Button
if (touching){ //We are touching
input.got = true; //the button is down
input.up = false; //the button is not up
if(!touched){// we hadn't already touched the button (first frame holding it)
input.down = true; //the button was got
touched = true; //we have touched
} else {
input.down = false; //it isn't down because this isn't the first fram holding it
}
} else { //We are not touching
curTouch = -1;
if (touched) {
input.up = true; //if we were holding the button last fram, then up is true because this is the frame it was released
} else {
input.up = false;
}
touched = false;
input.got = false;
input.down = false;
}
}
}
function OnGUI () {
if(!DBStoreController.inStore || showInStore)
GUI.Button(Rect(position.x, position.y, dimensions.x,dimensions.y),label);
}
function Within (pos : Vector2, bounds : Rect) : boolean {
pos.y = Screen.height - pos.y;
return (pos.x > bounds.x && pos.x < (bounds.x + bounds.width) && pos.y > bounds.y && pos.y < (bounds.y + bounds.height));
}
i would place the Fire1 gameobject as inputitem and it works i want the same thing for the gui.texture
please help been looking for an answer for days.
I don't understand your title. To use a GUITexture as a button, you can use GUITexture.HitTest() to text the finger or mouse position to see if it is a hit.
http://docs.unity3d.com/Documentation/ScriptReference/GUIElement.HitTest.html
for example in the editor when pressed play. i would press f to say switch on a flashlight. now on a mobile device there is no keyboard i would want now a gui texture to replace the function on the f key( in other words pressing the gui texture would be the same as pressing the f key)
So inside your for loop that cycles through the touches, you can have something like:
if (touch.phase == TouchPhase.Begin && guiTextFlashlight.HitTest(touch.position) {
// Do whatever to turn on/off the flashlight
}
Where 'guiTextureFlashlight' is a reference to the GUITexture button that you want to use to control the flashlight.
Your answer
Follow this Question
Related Questions
Door Problem 1 Answer
Health Script not working 1 Answer
How to Make A GUI Keypad Login With Texture Buttons 1 Answer
GUI.Texture Over GUI.Window 1 Answer
How do I adjust a GUITextures width, from the left? 1 Answer