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 Sobe459 · Dec 01, 2013 at 11:13 PM · mouseinventorymouse-dragdraganddrop

Drag and Drop GUI.Button not working in for loop.

I'm trying to make my inventory drag and drop. But when i use a for loop to create my buttons it does not work. I'm just trying to get the buttons draggable at this moment Please pardon my messy code.

 using UnityEngine;
 
 public class TestClass : MonoBehaviour
 {
     bool buttonPressed = false;
 
     // square width of each button in pixels
     public int buttonSize = 35; 
     
     // number of pixels between the buttons
     private int buttonSpacing = 1; 
     
     // top-left position of the button grid
     private int xOffset = (int)((Screen.width / 3) * 2.55f);
     private int yOffset = (int)((Screen.height / 2) * 1.48f);
     
     // number of columns
     private int numCols = 5;
     // number of buttons
     int numButtons = 25;//inventory.Count; 
     
     public Rect r = new Rect(0,0,35, 35);
 
     void OnGUI()
     {
         if(r.Contains(Event.current.mousePosition))
         {
             if(Event.current.type == EventType.MouseDown)
             {
                 buttonPressed = true;
             }
             
             if(Event.current.type == EventType.MouseUp)
             {
                 buttonPressed = false;
             }
         }
         
         #region invetorybutton
 
         
         for (int i = 0; i < numButtons; i++)
         {
             
             r.x = xOffset + (i % numCols) * (buttonSize + buttonSpacing); // column position
             r.y = yOffset + (int)Mathf.Floor((float)i/numCols) * (buttonSize + buttonSpacing); // row position
             
             if (GUI.Button(r, "" + i)) 
             {
                 // button was clicked
                 Debug.Log ("Button " + i + " was pressed.");
             }
         }
 
         if(buttonPressed && Event.current.type == EventType.MouseDrag)
         {
             r.x += Event.current.delta.x;
             r.y += Event.current.delta.y;
         }
 
         #endregion
     }
 }
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 ffxz7ff · Dec 01, 2013 at 11:16 PM 0
Share

What does not work? Do you get any errors? Unexpected behaviour?

avatar image Sobe459 · Dec 01, 2013 at 11:21 PM 0
Share

This is what I've come up with after fighting it all night. I can now drag the buttons kinda... I can drag the first button but every button after that moves it and the button that came before it. $$anonymous$$y attempt was trying to store the button positions in an array. I've probably done something funky along the way. Please help. Thank you!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class HUD2 : $$anonymous$$onoBehaviour {
 
     public int counter = 0;
     public Rect buttonRect = new Rect(10, 10, 35, 35);
     private bool buttonPressed = false;
     
     public ItemClass[] loot;
 
     public List<ItemClass> actionBar = new List<ItemClass>();   //List of actionbar items
     public List<ItemClass> inventory = new List<ItemClass>();   //List of inventory items
     public List<Rect> buttonPOS = new List<Rect>();
 
 
     void Start() {
 
     }
 
     void AddLoot() {
         for(int x = 0; x < loot.Length; x++) {
             if(inventory.Count < 25) {
                 inventory.Add(loot[x]);
             }
             else {
                 return;
             }
         }
     }
 
     void OnGUI()
     {
 
         //Additems
         if(GUI.Button(new Rect(300,30,120,80),"Add Items")) {
             AddLoot();
         }
 
 
 
         for(int  h = 0; h < inventory.Count; h++){
 
             //buttonRect.x += + h * 36;
 
             if(buttonPOS.Count < 25) {
                 buttonPOS.Add(buttonRect);
             }
 
             if(buttonPOS[h].Contains(Event.current.mousePosition))
             {
                 if(Event.current.type == EventType.$$anonymous$$ouseDown)
                 {
                     buttonPressed = true;
                 }
                 
                 if(Event.current.type == EventType.$$anonymous$$ouseUp)
                 {
                     buttonPressed = false;
                 }
             }
             
             if(buttonPressed && Event.current.type == EventType.$$anonymous$$ouseDrag)
             {
                 Rect v = buttonPOS[h];
                 v.x += Event.current.delta.x;
                 v.y += Event.current.delta.y;
                 buttonPOS[h] = v;
 
                 //buttonPOS[h].x += Rect buttonRect = new Rect(0, 0, 35, 35);   //Event.current.delta.x;
                 //buttonPOS[h].y += Event.current.delta.y;
             }
 
             GUI.Button(buttonPOS[h], "Draggable Button");
         }
     }
     
 }

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

17 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

Related Questions

Problem with dragging objects 1 Answer

Snapping object to .25 grid 0 Answers

Need Help, Rotating a wheel with touch or using the mouse 2 Answers

Update ScreenToWorldPosition for Moving Camera 1 Answer

Trying to trigger multiple objects by holding down the mouse with OnMouseDown 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