- Home /
Key for GUI.Button
I'm so frustrated so i hope you good people in the community can help :P I want to make my two GUI.Buttons from the code below to activate with keys and i've done it before in simpler scripts but it just won't work here! Any help? Thanks!
var Pressed : boolean = false;
var charPressed : boolean = false;
var guiRatio : float;
var sWidth : float;
var GUIsF : Vector3;
var cube : GameObject;
var check : boolean = false;
function Awake(){
sWidth = Screen.width;
guiRatio = sWidth / 1440;
GUIsF = new Vector3(guiRatio,guiRatio,1);
}
function Update(){
if(cube == null){
check = true;
}
}
function OnGUI(){
if(check){
GUI.matrix = Matrix4x4.TRS(new Vector3(Screen.width - 100*GUIsF.x,Screen.height - 60*GUIsF.y,0),Quaternion.identity,GUIsF);
if(GUI.Button(new Rect(-20,-20,100,60),"Deer")){
gameObject.GetComponent(CharacterMotor).jumping.baseHeight = gameObject.GetComponent(CharacterMotor).jumping.baseHeight + 3;
Pressed = true;
if(gameObject.GetComponent(CharacterMotor).jumping.baseHeight >= 4){
gameObject.GetComponent(CharacterMotor).jumping.baseHeight = 4;
}
}
}
if(Pressed){
GUI.matrix = Matrix4x4.TRS(new Vector3(Screen.width - 100*GUIsF.x,Screen.height - 60*GUIsF.y,0),Quaternion.identity,GUIsF);
if(GUI.Button(new Rect(-20,-100,100,60),"Stop")){
gameObject.GetComponent(CharacterMotor).jumping.baseHeight = 1;
Pressed = !Pressed;
}
}
}
Answer by premier · Dec 12, 2013 at 01:44 PM
On GUI.Button you have option to set a tooltip (good for hover effects) or set ID parameter! You can use ID for any button you have.
In my scripts every time i click in gui.button i send a message for all scripts who extends my other script, like this:
Events.ToList().ForEach(evt => evt.OnClickUIButton(ID));
Then in other script i have:
public void OnClickUIButton(string ID) {
switch(ID) {
case "PlayGame":
// do same action
break;
}
}
Answer by WesterlyCarrot9 · Dec 12, 2013 at 01:48 PM
Nevermind guys i just figured out what need to be done :P Thanks for your replies anyway :P
You really should have accepted the answer by premier, or at least detailed your solution. This is a community site for archiving answers.
Your answer
Follow this Question
Related Questions
Assign a hotkey to a GUI button? 3 Answers
Gui Button Solid 2 Answers
Use a key to open a GUI? 2 Answers
On Clicked, On Released GUI Button ? 2 Answers