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 /
This question was closed Mar 07, 2015 at 10:08 PM by KnightRiderGuy for the following reason:

Solved by PMMmmpies in the comments.

avatar image
0
Question by KnightRiderGuy · Feb 14, 2015 at 05:33 PM · uibuttonbuttonsonguigui-button

UI Button Instead of OnGUI Button?

How would I make this to use the new UI buttons instead of the OnGUI buttons?

 function OnGUI () {
      var guiTime = time;
      
      var minutes : int = guiTime / 60; //Divide the guiTime by sixty to get the minutes.
      var seconds : int = guiTime % 60;//Use the euclidean division for the seconds.
      var fraction : int = (guiTime * 100) % 100;
   
      textTime = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction); 
      //text.Time is the time that will be displayed.
      GetComponent(GUIText).text = textTime;
  
      if (GUI.Button(Rect(500,55,50,30), buttonTexture, buttonText)){
          timerOn = !timerOn;
          if(timerOn) buttonText = "Stop";
          else buttonText = "Start";
          audio.PlayOneShot(beep1);
      }
  
      if (GUI.Button(Rect(555,55,50,30), buttonTexture, "Reset")){
         time = 0;
         audio.PlayOneShot(beep2);
      }
  }
Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0

Answer by DiegoSLTS · Feb 14, 2015 at 06:21 PM

I'll try to tell you my approach, but it's hard to explain only with words. Also, some information is missing (you obviously have more than 2 buttons, so I'll assume you also have a Text object somewhere to display the actual time)

I'd make 2 scripts, one for each button.

For the Start/Stop button:

  1. Add a reference to the Text object that should display the formatted time.

  2. Add a public property to store the elapsed time.

  3. The lines where you create a string with the current time expressed as hh:mm:ss would be inside a "if (timerOn) { .. }" block inside the Update method. After getting the string, instead of searchin for a GUIText component, use the Text object's reference.

  4. The lines that change the state of the timer and the text on the button ("Stop" or "Start") would be on a public method called "OnClick" or something like that.

  5. Set the "On Click" callback on the inspector to call the OnClick method.

For the Reset button:

  1. Add a reference to the Stop/Start button.

  2. Add an "OnClick" method that sets the StopStart's elapsed time property to 0 and plays the sound.

  3. In the inspector, set the On Click for this button to that OnClick method.

Comment
Add comment · Show 6 · 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 KnightRiderGuy · Feb 14, 2015 at 06:58 PM 0
Share

Thanks DiegoSLTS, Nope there are just the two buttons, and there is a text object. I think I follow what you mean by making two scripts, just not sure how they should be made. It's not over complicated but I have trouble understanding the differences between the new UI method VS the old OnGUI stuff? This is the only think missing from my code snippet:

 #pragma strict
  private var time : float;
  var textTime : String;
  var timerOn : boolean;
  var buttonText : String;
  var beep1 : AudioClip;
  var beep2 : AudioClip;
  var buttonTexture: Texture2D;
   
  function Start() {
      timerOn = false;
      buttonText = "Start";
  }
   
  function Update(){
      if(timerOn)
          time += Time.deltaTime;
  }
avatar image Mmmpies · Feb 14, 2015 at 07:06 PM 0
Share

$$anonymous$$y JS is poor but create a public function for each buttons actions. Drag the script onto one of the buttons, in that buttons inspector go to the OnClick area and click + and a slot appears, drag the button with the script on it to the slot and from the drop down to the right select $$anonymous$$yScriptName -> $$anonymous$$yFunctionName.

You don't really need two script just two functions, you don't even need the script on both buttons you just need to be able to put the script on a UI element/GameObject so you can drag it across to that slot and select whichever function applies to that button.

avatar image KnightRiderGuy · Feb 14, 2015 at 08:02 PM 0
Share

Thanks $$anonymous$$mmpies, But how would I write those function from all of this on GUI stuff?

avatar image Mmmpies · Feb 14, 2015 at 09:05 PM 1
Share

Well my JS is really very poor but...

 public function TimerOnOff()
 {
     // do your timer on/off stuff
 }
 
 public function TimerReset()
 {
     // do your timer reset stuff
 }

I think all JS variables are public unless stated otherwise. So set a GameObject var for the Text (call it $$anonymous$$yText) and drag that Text UI item onto the script slot. Then use GetComponent to find the UI Text component and set its value.

Ahh if it were C# I could be more use.

avatar image KnightRiderGuy · Feb 14, 2015 at 09:44 PM 0
Share

Thanks $$anonymous$$mmpies, I'll see if I can make heads or tails of this, $$anonymous$$y Code guy would much prefer if this was C# as that's what he knows best, just sucks that the one I'm using is Java, it works but I need it to work with the UI buttons and not OnGUI buttons ;)

Show more comments

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing Color of Material with Bool and Unity UI 1 Answer

Runtime UI button creation: can't make an "OnClick" where I call a function from another script while sending a parameter. 2 Answers

button.onClick.AddListener(method); NOT Working 1 Answer

Change mouse button click in existing code to ui button click.,Change the existing code from mouse click to ui buttons. 1 Answer

UI button not working 1 Answer


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