- Home /
Creating my own DrawButton method but am stuck
In my game instead of using if (GUI.Button(new Rect(10, 70, 50, 30), "Click")) {} to draw my buttons I want to have a method which does that for me; in my game I set the size of the text, make a custom texture etc. However I've been running into a few problems. I've gotten the top code to draw my button but am having issues making code run when button is pressed - bottom code.
GetComponent<DrawMethods>().DrawButton(...);
if (GetComponent<DrawMethods>().DrawButton(...))
{
NextTurn();
}
I thought maybe since Gui.Button used the if statement like that to capture a user pressing the button I could do the same with my method, but I was mistaken. So my question is, is there a way I can alter that to make it work? Or if not, can anyone tell me how I can convert strings to be usable code? I noticed that if I have the if (Gui.Button...) code in the DrawButton method I can make the button be usable upon calling method, however I want to be able to send my code into that if statement. I'm trying to make a script which can be usable across different projects.
And also, I've spent multiple days Googling for ways to solve my problem but that's gotten me nowhere. The code people put up, or answers, to convert strings into code either didn't make sense in my situation or just didn't work when I put it into my game and altered it. And the if (GetComponent().DrawButton(...)) {NextTurn();} solution is hard to know what to Google and get the answer you want.
What does your code look like? Specifically DrawButton()
This is my current code. It doesn't like calling the DrawButton method within the if statement.
public class PlayersTurn : $$anonymous$$onoBehaviour
{
//Creates and handles GUI Text
void OnGUI()
{
if (GetComponent<Draw$$anonymous$$ethods>().DrawButton (Color.white, 100, Color.black, new Rect (0, 0, 200, 200), "Skip Turn"))
{
//The turn will change
NextTurn();
}
}
public class Draw$$anonymous$$ethods : $$anonymous$$onoBehaviour
{
//Draws the button you want when called
public void DrawButton (Color colButtonColor, int intTextSize, Color colTextColor, Rect rctButtonPosScale, string strText)
{
//Helps with the creation of the button
GUIContent gcoButton = new GUIContent(); //Creates variable which stores text and texture
GUI.depth = 0; //Sets it to be above all GUI labels
Texture2D t2dActiveButton = new Texture2D(1, 1); //Creates the texture variable
t2dActiveButton.SetPixel(0, 0, colButtonColor); //Sets the colour
t2dActiveButton.Apply(); //Applies the above
GUI.skin.box.normal.background = t2dActiveButton; //Converts texture to GUI.skin
GUIStyle gstButton = GUI.skin.button; //Creates the style variable for the text
gstButton.fontSize = intTextSize; //Sets the font size
GUI.contentColor = colTextColor; //Sets the colour of the text
gcoButton.image = (Texture2D)t2dActiveButton; //Sets image as the texture generated
GUI.skin.button.normal.background = (Texture2D)gcoButton.image; //Normal button is this image
GUI.skin.button.hover.background = (Texture2D)gcoButton.image; //Hover button is this image
GUI.skin.button.active.background = (Texture2D)gcoButton.image; //Active button is this image
//Creates button with supplied position, scale, text etc.
GUI.Button (rctButtonPosScale, new GUIContent (strText, t2dActiveButton), gstButton);
}
}
Console will output an error, explaining what's wrong. If statements won't work with void return types. You'll need DrawButton to return a bool that GUI.Button provides.
public bool DrawButton( // Returns the bool from GUI.Button for the If
return GUI.Button( // This will return a bool for DrawButton
Answer by Jessespike · Feb 19, 2015 at 09:21 PM
I'm going to guess that you're missing the return. But without seeing the code, I have no idea what the problem is.
public bool DrawButton(Rect rect, string text)
{
return GUI.Button(rect, text);
}
void OnGUI ()
{
if (DrawButton( new Rect(100f, 100f, 100f, 100f), "test button"))
{
Debug.Log("button clicked");
}
}
@Jessespike Thank you so much. I tried doing similar to what you said, turning the void to be a bool and had issues. I guess this was how I was meant to do that. Didn't realise making it work would be that easy :D
Your answer
