Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 GreenLimeLight · Jul 19, 2011 at 01:27 PM · guibuttonwindowdragwindow

Dragging and Locking Buttons.

Hey, good evening everyone on UnityAnswers! :]

I'm currently stuck on a problem that I've been trying to fix since morning, and I haven't found a solution since so I thought I'd pop by and post it up here. Let me post up the codes first:

  class ActionButt
  {
      var toggle: String; //Name of custom style in GUI Skin
      var isSelected: boolean; //Is the Toggle button On or Off
      var label: String;
      var position: Rect; //Where to draw this button on the screen
  }
  //Variables
  var bt1: boolean = false;
  var bt2: boolean = false;
  var bt3: boolean = false;
  //var bt4: boolean = false;
  var bt5: boolean = false;
  var bt6: boolean = false;
  var bt7: boolean = false;
  var btGUI: boolean = false;    //Boolean to call on the different GUI
  var btnTexture: Texture;
  var buttonList: ActionButt [];
  var toolSkin: GUISkin;
  var windowRect : Rect = Rect (20, 20, 120, 50);
  var dragWindow : boolean;
  var toggleDrag : String;
  //Functions
  function Start () {
      toggleDrag = "Enable";
      dragWindow = false;
  }
  function setMeOnly(): boolean {
      bt1 = bt2 = bt3 = bt5 = bt6 = bt7 = false;
      return true;
  }
  function OnGUI() {
      //First GUI
      if (!btGUI) {
          GUI.skin = toolSkin;
          //Top Left Button
          bt1 = GUI.Toggle (Rect(50,50,100,100), bt1, "");
          if (bt1) bt1 = setMeOnly();
          //Bottom Right Button
          bt2 = GUI.Toggle (Rect(50,275,100,100), bt2, "");
          if (bt2) bt2 = setMeOnly();
          //Top Button
          bt3 = GUI.Toggle (Rect(50,500,100,100), bt3, "");
          if (bt3) bt3 = setMeOnly();
          //Middle Button
          //bt4 = GUI.Toggle (Rect(575,275,100,100), bt4, "");
          //if (bt4) bt4 = setMeOnly();
          //Top Right Button
          bt5 = GUI.Toggle (Rect(1125,50,100,100), bt5, "");
          if (bt5) bt5 = setMeOnly();
          //Bottom Button
          bt6 = GUI.Toggle (Rect(1125,275,100,100), bt6, "");
          if (bt6) bt6 = setMeOnly();
          //Bottom Left Button
          bt7 = GUI.Toggle (Rect(1125,500,100,100), bt7, "");
          if (bt7) bt7 = setMeOnly();
          //Button to Call
          //if (!btnTexture) {
              //Debug.LogError("Please assign a texture on the inspector.");
              //return;
          //}
      }
      if(GUI.Button (Rect(575,275,100,100), btnTexture, "Button")) {
          if (btGUI)
          {
              print("Call the second GUI.");
              btGUI = false;
          }
          else if (!btGUI)
          {
              print("Remain in the first GUI.");
              btGUI = true;
          }
      }
      //Second GUI
      if (btGUI) {
          GUI.skin = toolSkin;
          for (var i = 0; i < buttonList.length; i++) {
              var bt = buttonList[i];
              bt.isSelected = GUI.Toggle(bt.position, bt.isSelected, bt.label);
              if (bt.isSelected)
              {
                  print("This butt is turned on." + bt.label);
                  resetOtherButtons(i);
              }
                windowRect = GUI.Window (0, windowRect, DoMyWindow, btnTexture, toggleDrag);
        if (GUI.Button(Rect(80,380,100,30),toggleDrag)) {
            if (dragWindow) {
                dragWindow = false;
                toggleDrag = "Enable";
            }
            else if (!dragWindow) {
                dragWindow = true;
                toggleDrag = "Disable";
            }
        }
          }
      }
  }
  function resetOtherButtons (ignoreThisNumber)
  {
      for (var i = 0; i < buttonList.length; i++) {
          if (i !=ignoreThisNumber) {
              buttonList[i].isSelected = false;
          }
      }
  }
  // Make the contents of the window
  function DoMyWindow (windowID : int) {
      // Make a very long rect that is 20 pixels tall. 
      // This will make the window be resizable by the top
      // title bar - no matter how wide it gets.
      if(dragWindow) {
          GUI.DragWindow (Rect (0,0, 10000, 10002));   }
      else {
      }
  }

Firstly, I have two GUI(s) which I could switch in between them. The second GUI contain 7 buttons that are formed in a hexagon manner, which I have done so from the Inspector Menu.

Next, I have a GUIWindow inside the second GUI which I referenced from the Documentations, and can only been seen inside the second GUI, and not the first GUI. There is an Enable/Disable to lock the GUIWindow from moving.

What I'm trying to achieve is to implement this feature upon my hexagon-patterned button so that when the enable/disable button is clicked, it will be able to either move or be locked respectively. Any ideas how I can achieve that?

Thank you for taking your time off to read this! :]

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

· Add your reply
  • Sort: 
avatar image
0

Answer by SilverTabby · Jul 19, 2011 at 04:23 PM

Here is how I would implement the ability to drag GUI.Buttons around the screen:

  • Don't hard code the rects into the GUI function. Store them (in an array?) at the top of the file.

  • In Update/LateUpdate, check if the user pressed the mouse down, and then go through the rects that you want to drag and see if any of the rects.Contains(Input.mousePosition)

  • Then track the change in the mouse position and feed that change into the first 2 values of the rect you are dragging

  • Stop updating the rect's position when the user releases the mouse button.

Now just feed the rects into your buttons and they should move around the screen as you desire.

Hope this helped

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 GreenLimeLight · Jul 20, 2011 at 02:45 AM 0
Share

For the first pointer, do I create two arrays for the two GUI(s) since I have to switch in between them? What I've done thus far is creating two arrays for the two GUI(s), but still leaving the GUI.Button (the button that switches between the two GUI) as its own. Is that correct?

For the second pointer, does Update/LateUpdate refer to the function OnGUI, or do I have to set up a new function?

Thanks for the answer, btw! :]

avatar image SilverTabby · Jul 20, 2011 at 04:48 AM 0
Share

-It looks cleaner if you have 2 arrays for 2 GUIS

-You CAN put that in the OnGUI, but it's not optimal. OnGUI is called at least twice per frame while Update/LateUpdate is called only once per frame. It comes down to a matter of preference and/or optimization.

avatar image GreenLimeLight · Jul 20, 2011 at 06:40 AM 0
Share

Hmm. The original second GUI is considered an array, right? So all I have to do is re-implement the exact same codes right?

avatar image SilverTabby · Jul 20, 2011 at 06:23 PM 0
Share

"The granule of reuse is the granule of release."

Just make sure you're not changing the first gui twice when you re-implement it ;)

avatar image GreenLimeLight · Jul 25, 2011 at 08:20 AM 0
Share

Sorry for the awfully late reply! What do you mean changing the first GUI twice? It's only been a month since I started using Unity, and I'm really slow at grasping things.

And I realize an array is useful, but what if I had buttons with different symbols in them. Six buttons with six different symbols to be exact. Won't the first GUI be more useful ins$$anonymous$$d?

Show more comments

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

How do I draw controls in a GUI Window - based on screen dimensions? 2 Answers

Add EditorUI elements when a button is pressed 0 Answers

GUI Button and Window 1 Answer

A few GUI related questions. 1 Answer

Easy way to make buttons in-accessible when under a pop-up window? 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