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 c.zeta · Dec 02, 2011 at 02:59 PM · guibuttonitweenanimate

move a gui element

hi everybody, I'm trying to move vertically a simple gui button, but I can't figure it out. I'm not a programmer and my gui is very simple, I've tried to move a cube through animation and itween and it works, but how i can move the button? here's my code

 var controlTexture : Texture;
 var customSkin : GUISkin;
 
 function OnGUI () {
     
     GUI.Label (Rect (Screen.width/2-250, Screen.height/2-65, 500, 150), controlTexture);
     
     GUI.Box (Rect (0,0,150,100), "Top-left");
     GUI.Box (Rect (Screen.width - 150,0,150,100), "Top-right");
     GUI.Box (Rect (0,Screen.height - 100,150,100), "Bottom-left");
     GUI.Box (Rect (Screen.width - 150,Screen.height - 100,150,100), "Bottom-right");
     
     if (GUI.Button (Rect (Screen.width/2-250, Screen.height/2 - 170, 500, 100), "collocazione A")) {
         
         isLoading = true;
         Application.LoadLevel("level1"); // load the game level.
                 
         print ("you clicked collocazione A");
     }
     
     if (GUI.Button (Rect (Screen.width/2-250, Screen.height/2 + 70, 500, 100), "collocazione B")) {
         print ("you clicked collocazione B");
         
     }
     
 }

and another question: I used Application.LoadLevel to load a level with another gui, how can I simply load a new window or label when I click the button?

thank you!

Comment
Add comment · Show 2
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 ks13 · Dec 02, 2011 at 03:18 PM 0
Share

Actualy the GUI is reloaded with each frame, so if the only thing you want to change is the GUI you only have to make a new function for each additional GUI you need, and then call the corresponding function. As for your moving button, i don't quite undestand what you're trying to do here, even with the code. Can you explain it in more detail?

avatar image c.zeta · Dec 02, 2011 at 03:31 PM 0
Share

thank you for the answers. The code is just my actual gui, I want to move out of the camera view the button "collocazione A" when I click it. I've looked for a solution through the web and I've found that a solution could be itween but with my little knowledge of program$$anonymous$$g I can't use it on a gui.button, only on a cube.. do you know other ways to move a gui element? I've tried also simple keyframe animation but it still works only on the cube..

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cj_coimbra · Dec 02, 2011 at 03:38 PM

 private bool showButton = true;
     void OnGUI()
     {
         if (showButton)
         {
             if (GUI.Button(new Rect(100,100, 120, 25), "Sample button..."))
                 showButton = false;
         }
     }

Maybe this is what you are looking for? The only problem is that whenever you want your button back, you will need to find a suitable place to set showButton variable to true again.

Comment
Add comment · Show 5 · 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 c.zeta · Dec 02, 2011 at 03:54 PM 0
Share

thank you for the answer. It's not really what I'm looking for, because I would like to move the button, not simply "hide" it. I would like to create an animation, like a vertical sliding. e.g the "Animating UnityGUI (Label, Button, Box, TextArea, etc...)" example in http://itween.pixelplacement.com/examples.php#movingonpath

I think I'm gonna buy the example from this page, but then I would like to use it correctly! XD

avatar image cj_coimbra · Dec 02, 2011 at 04:12 PM 0
Share

Hmm ok. Try this (click to move out of screen, press return to move back into the screen space):

 void Start () 
 {
     buttonRect = new Rect(buttonX, buttonY, buttonWidth, buttonHeight);
 }
 
 void Update () {
     if (moveOut)
     {
         if (buttonRect.x != escapeX)
             buttonRect.x = $$anonymous$$athf.$$anonymous$$oveTowards(buttonRect.x, escapeX, buttonSpeed * Time.deltaTime);
         else
             moveOut = false;
     }
     if (moveIn)
     {
         if (buttonRect.x != originalX)
             buttonRect.x = $$anonymous$$athf.$$anonymous$$oveTowards(buttonRect.x, originalX, buttonSpeed * Time.deltaTime);
         else
             moveIn = false;
     }

     if (Input.Get$$anonymous$$ey("return"))
     {
         moveIn = true;
         moveOut = false;
     }
 }

 private Rect buttonRect;
 private float buttonSpeed = 500.0f;
 private float buttonX = 200;
 private float buttonY = 200;
 private float buttonWidth = 120;
 private float buttonHeight = 30;
 private float escapeX = -120;
 private float escapeY = 200;
 private float originalX = 200;
 private float originalY = 200;
 private bool moveOut = false;
 private bool moveIn = false;
 void OnGUI()
 {
     
     if (GUI.Button(buttonRect, "Sample button..."))
     {
         moveOut = true;
     }
 }
avatar image c.zeta · Dec 02, 2011 at 04:46 PM 0
Share

thanks man for your help, really appreciated.. but it's like just reading in japanese for me XD it's no js but cs right? because in both version (js and cs) I'm having error such "A namespace can only contain..", it should be a formatting error. I find easier to write in js, so I think (or it's just what I have undestood) that I have to declare a var for every value and then a moveOut method to trigger like in your previous example.. I'm looking around to solve, thank you again

PS. do you know any unity scripting tutorial for newbie??

avatar image cj_coimbra · Dec 02, 2011 at 04:57 PM 0
Share

The idea is to control your GUI element rectangle´s coordinates. If you click the button, it will set true the value of moveOut, so the function $$anonymous$$athf.$$anonymous$$oveTowards will move it towards the escapeX variable position. The same when you press return. In this case, moveIn will be set true and then it will move towards the originalX. You usualy put stuff that need to happen over time in the Update function. Those inner conditions are there to check if the GUI element rectangle is already at the target destination. That´s just an example. Perhaps if you describe your concrete case I can help you more on this issue.

avatar image c.zeta · Dec 02, 2011 at 05:43 PM 0
Share

I've successfully put your script and it works correctly. my idea is to create a simple augmented reality app, when the camera detects the marker I want the gui to appear (and this is the first animation, a simple scaling of a window). then when I click the button "collocazione A" or "collocazione B" I want to open other windows (and this is the second animation, the current windows slide away and other windows slide in)... the examples from itween fits exactly what I want to do, but I can't figure out how to do them.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Editing buttons created in 'for' loop. 0 Answers

GUI.Label positioning for many device resolutions 1 Answer

GUI, making main page lead into an instructions page 2 Answers

How to get Rect from scene Button ? 1 Answer

Unity 5 GUI system 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