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 marisa3d · Mar 11, 2013 at 08:44 PM · guidrag-and-droplabelmatching

How can I make a drag-and-drop list of buttons fed using an array?

In my scene I have a number of items that are to be labeled by the user. I've created a script that makes a list of RepeatButtons based on the names of the items as I've assigned them in the inspector. I want the user to drag the button to the right item in my scene. So far, the buttons don't drag.

Here is what I have:

 var refObj : GameObject[];
 private var i : int;
 private var buttonWidth : int = 0;
 var currentPositionX = new Array();
 var currentPositionY = new Array();
 var label;
 var isDragging: boolean;
 var subNum : int;
 
 
 function Start()
 {
     Shuffle(refObj);
     for (var i = 0; i < refObj.Length; ++i)
     {
         if (buttonWidth < refObj[i].name.Length * 7)
         {
             buttonWidth = refObj[i].name.Length * 7;
         }
     }
         
 }
 
 function OnGUI () 
 {
     label = new Rect [refObj.Length];
     for (var i = 0; i < refObj.Length; ++i)
     {
         currentPositionX[i] = 10;
         currentPositionY[i] = 70 + (i * 30);
         label[i] = Rect (currentPositionX[i], currentPositionY[i], buttonWidth, 30);
 
         
            if(GUI.RepeatButton(Rect(label[i]), refObj[i].name))
         {
             if(Input.GetMouseButton(0))
             {
                 isDragging = true;
                 subNum = i;
                 
             }
         }
         
     }
     if (isDragging)
         {
             label[subNum].x = Event.current.mousePosition.x;
             label[subNum].y = Event.current.mousePosition.y;
         }
     
 }
 
 function Update()
 {
     if (Input.GetMouseButtonUp(0))
     isDragging = false;
 }
 
 function Shuffle(refObj : GameObject[])
 {
     for (var i = 0; i < refObj.Length; ++i)
     {
         var r = Random.Range(0, refObj.Length);
         var tmp = refObj[i];
         refObj[i] = refObj[r];
         refObj[r] = tmp;
     }
 }
Comment
Add comment · Show 1
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 thesfid · Mar 13, 2013 at 06:01 PM 0
Share

Not sure if this will help, but try putting the...

 if(Input.Get$$anonymous$$ouseButton(0))
          {
           isDragging = true;
           subNum = i;
  
          }

...inside function Update()

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by VoxelBoy · Mar 15, 2013 at 05:21 PM

Below is the fixed code.

The problem in the original code was that in OnGUI, when isDragging is true, label[subNum] is assigned the current mousePosition values but the next time OnGUI is called, those values are overwritten in line 31.

So I moved lines 29,30,31 into the for-loop in Start, as those values only need to be initialized once. I also moved line 26, which initializes the label array, into Start.

 #pragma strict
 
 var refObj : GameObject[];
 private var i : int;
 private var buttonWidth : int = 0;
 var currentPositionX = new Array();
 var currentPositionY = new Array();
 var label : Rect[];
 var isDragging: boolean;
 var subNum : int;
  
  
 function Start()
 {
     Shuffle(refObj);
     
     label = new Rect [refObj.Length];
     
     for (var i = 0; i < refObj.Length; ++i)
     {
        if (buttonWidth < refObj[i].name.Length * 7)
        {
          buttonWidth = refObj[i].name.Length * 7;
        }
        
        currentPositionX[i] = 10;
        currentPositionY[i] = 70 + (i * 30);
        label[i] = Rect (currentPositionX[i], currentPositionY[i], buttonWidth, 30);
     }
  
 }
  
 function OnGUI () 
 {
     for (var i = 0; i < refObj.Length; ++i)
     {
            if(GUI.RepeatButton(Rect(label[i]), refObj[i].name))
        {
          if(Input.GetMouseButton(0))
          {
           isDragging = true;
           subNum = i;
  
          }
        }
  
     }
     if (isDragging)
         {
             label[subNum].x = Event.current.mousePosition.x;
          label[subNum].y = Event.current.mousePosition.y;
        }
  
 }
  
 function Update()
 {
     if (Input.GetMouseButtonUp(0))
     isDragging = false;
 }
  
 function Shuffle(refObj : GameObject[])
 {
     for (var i = 0; i < refObj.Length; ++i)
     {
         var r = Random.Range(0, refObj.Length);
         var tmp = refObj[i];
         refObj[i] = refObj[r];
         refObj[r] = tmp;
     }
 }
Comment
Add comment · Show 1 · 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 marisa3d · Apr 08, 2013 at 04:04 PM 0
Share

It didn't work for me, VoxelBoy.

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

12 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

Related Questions

Score display not working 1 Answer

How to change how meany decimals are in a GUI sound slider 2 Answers

Randomize GUI Position 2 Answers

GUI.Lable slowdown problem 0 Answers

Gui label and gui button 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