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 Vumi · Jan 03, 2013 at 01:51 PM · guidragbox

Applying a script to multiple gui boxes

Hi, I have written the following script that draws a gui box then allows the user to drag it around the screen. However, I need to be able to add boxes dynamically and be able to drag them all. How would I modify my script to be able to accommodate for multiple boxes?

    private var progress: int;
    var mySpeed: int;
    var pos: Vector2; //position of box
    var size: Vector2; // size of box
    var within: boolean = false; //checks if clicked inside box
    var mousePos: Vector2; // current mouse positon
    var xOffset = 0; //used to calculate where in the box user clicked
    var yOffset = 0; //used to calculate where in the box user clicked
 
    function OnGUI() {
        // Constrain all drawing to be within a pixel area .
        GUI.BeginGroup(new Rect(pos.x, pos.y, size.x, size.y));
 
 
        GUI.Box(Rect(0, 0, size.x, size.y), "Hi"); //draw the box everyframe
 
        // Always match BeginGroup calls with an EndGroup call
        GUI.EndGroup();
    }
 
    function Update() { //within every frame
 
        if (Input.GetMouseButtonDown(0)) //if user presses the left mouse
 
        {
            mousePos = Input.mousePosition; //get current position of mouse
            mousePos.y = Screen.height - mousePos.y; //used to invert the y value of mousePosition
            if (mousePos.x > pos.x && mousePos.x < (pos.x + size.x)) //if user has clicked with the box's X co-ords
            {
                if (mousePos.y > pos.y && mousePos.y < (pos.y + size.y)) //if user has clicked with the box's Y co-ords
                {
                    within = true; //set within to true to let the script know
                    xOffset = mousePos.x - pos.x; //set where in teh box the user has clicked
                    yOffset = mousePos.y - pos.y; //set where in teh box the user has clicked
                }
            }
        }
 
 
 
        if (Input.GetMouseButton(0)) //if left mouse button is held down
 
        {
 
            if (within == true) //and user has clicked within the box
            {
                mousePos = Input.mousePosition; //get mouse position
                mousePos.y = Screen.height - mousePos.y; //invert the Y coords
 
 
                pos.x = mousePos.x - xOffset; // move the box with the mouse
                pos.y = mousePos.y - yOffset; // move the box with the mouse
            }
 
        }
 
 
        if (Input.GetMouseButtonUp(0)) //if user releases the mouse button
 
        {
            within = false; //set within to false to confirm the user is not clicking anymore
 
        }
    }
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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Drawing a GUI box by specifying the two corners 1 Answer

GUI Note taking box 0 Answers

Selection box not working 0 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