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 KnightRiderGuy · Dec 18, 2014 at 01:51 PM · gui-buttonbuttonstatesmousedowngui texture

GUI Testure Button Swap on MouseDown

I have this script I'm trying to get to work for an oxygen timer, it works fine but I'm trying to get a series of two Texture GUI buttons to swap out a different button texture on mouse down and swap back on mouseUp, but I'm not having a lot of luck figuring it out. I have it where the 2nd button state stays which is not what I want. here is my somewhat messy code... lol slowly getting messier the more I muck about with it.

  var beep1 : AudioClip;
  var beep2 : AudioClip;
  var clockIsPaused : boolean = false;
  var startTime : float; // (in sec)
  var timeRemaining : float; // (in sec)
  var percent : float;
  var clockBG : Texture2D;
  var clockFG : Texture2D;
  var buttonTexture: Texture2D;
  var miniButtonTexture : Texture2D;
  var miniButtonTexture2 : Texture2D;
  var clockBGMaxWidth : float; // the starting width of the foreground bar
  var TextureButton1: Texture;
  var TextureButton2: Texture;
  
  
  function Start() {
      timeRemaining = startTime;
  }
  
  function Awake()
  {
       TextureButton1 = miniButtonTexture;
       TextureButton2 = miniButtonTexture;
      //startTime = 20.0;
      clockBGMaxWidth = clockFG.width * 1;
  }
  
  function Update () 
  {
      if(!clockIsPaused)
      {
         // make sure the timer is not paused
         DOCountdown();
      }
  }
  
  function OnGUI()
  {
      
      var newBarWidth : float = (percent/105) * clockBGMaxWidth; // this is the width that the foreground bar should be
       
          GUI.BeginGroup (new Rect(0, 120, newBarWidth, clockBG.height));
         
         GUI.DrawTexture (Rect (118, 0, clockBG.width * 1, clockBG.height), clockBG);
         GUI.EndGroup ();
         
         
              GUI.BeginGroup (new Rect(0, 0, clockFG.width * 1, clockFG.height));
              GUI.DrawTexture (Rect (0, 0, clockFG.width * 1, clockFG.height), clockFG);
              GUI.EndGroup();
                  
                  if (GUI.Button(Rect(735,20,50,50), buttonTexture, "")){
                 Application.LoadLevel("ThreeD_Overview");
                 
                  }
                 if (GUI.Button(Rect(345,427,64,32), TextureButton1, "")){
                 //Pause Timer
                 clockIsPaused = !clockIsPaused;
                 audio.PlayOneShot(beep1);
                   TextureButton1 = miniButtonTexture2;   //Swap to Texture2
                   
                  }
                  
                  if (GUI.Button(Rect(420,427,64,32), TextureButton2, "")){
                 //Reset Timer
                 audio.PlayOneShot(beep2);
                 TextureButton2 = miniButtonTexture2;   //Swap to Texture2
                 
                 timeRemaining = startTime;
                 
                  }
  }
  
  function DOCountdown()
  {
      //timeRemaining = startTime -Time.time;
      timeRemaining -= Time.deltaTime;
  
      percent = timeRemaining/startTime * 100;
      if (timeRemaining < 0)
      {
         timeRemaining = 0;
         clockIsPaused = true;
         //TimeIsUp();
         Debug.Log("Time is up!");
         Application.LoadLevel ("KnightSpinningLogo_NoKITTintro");
         
      }
      //ShowTime();
  }
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 rob5300 · Dec 18, 2014 at 03:15 PM 0
Share

$$anonymous$$aybe to make your life easier, you should start using the new 4.6 GUI system. This will allow you with great ease to have the effect you want, and can all be done in editor with $$anonymous$$imal code.

avatar image KnightRiderGuy · Dec 18, 2014 at 04:10 PM 0
Share

I would but I have difficulty trying to figure out how to make the new UI work with existing code like this.

avatar image rob5300 · Dec 18, 2014 at 05:24 PM 0
Share

It is really worth learning, the OnGUI system is very outdated. Check the unity documentation and lessons on the UI to learn. http://unity3d.com/learn/tutorials/modules/beginner/ui Start on the Canvas lesson.

0 Replies

· Add your reply
  • Sort: 

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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How Do I Make GUI ON Off Button with 2 Button States 3 Answers

Using a GUI button to stop a gameobject movement 3 Answers

Button Script issue, works in game mode not in build and run 1 Answer

calling buttons in triggers 1 Answer

GUIButton won't appear Scripting problem, help? 2 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