Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ferotion · Feb 18, 2015 at 10:56 PM · convertgui.button

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessespike · Feb 19, 2015 at 09:17 PM 0
Share

What does your code look like? Specifically DrawButton()

avatar image Ferotion · Feb 21, 2015 at 05:46 AM 0
Share

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);
     }
 }
avatar image Jessespike · Feb 21, 2015 at 08:47 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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");
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ferotion · Feb 22, 2015 at 09:56 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Touch on GUI.Button HELP!!!! 3 Answers

Change GUILayout.Button fontsize 0 Answers

GUI button texture different color when script is on Instantiated object 0 Answers

The Unity move - converting application from C++ to C# 3 Answers

software that makes scripts for you 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges