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 Christian.Tucker · Aug 29, 2013 at 04:06 AM ·

Selecting Units within GUI.Box

So, I've got a script that creates a GUI.Box when I drag the Mouse. Although, I'm trying to figure out how to select all of the units inside of that GUI.Box, and I'm having a problem figuring it out.

I know this has been asked before, but the answers just didn't really explain it for me.

Here's the code.

 using UnityEngine;
 using System.Collections;
 
 public class SelectionTool : MonoBehaviour {
     
     Vector2 lmbDown;
     Vector2 lmbUp;
     bool     lmbDrag;
     
     float raycastLength = 200f;
     
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetButtonDown ("Fire1")) {
             lmbDown = Input.mousePosition;
         }
         
         if(Input.GetButtonUp ("Fire1")) {
             lmbUp = Input.mousePosition;
             lmbDrag = false;
         }
         
         if(Input.GetButton("Fire1")) {
             selectionBox (Input.mousePosition);
         }
     }
     
     
     void selectionBox(Vector2 screenPosition ) {
         if (screenPosition != lmbDown) {
             lmbDrag = true;
             lmbUp = screenPosition;  
         }
     }
         
     
     void OnGUI() {
         if (lmbDrag) {
             float width = lmbUp.x - lmbDown.x;
             float height = (Screen.height - lmbUp.y) - (Screen.height - lmbDown.y);
             Rect rect = new Rect(lmbDown.x, Screen.height - lmbDown.y, width, height);
             GUI.Box (rect, "");
         }
     }
 }
 
Comment
Add comment · Show 4
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 getyour411 · Aug 29, 2013 at 04:13 AM 0
Share

Thought I would comment that Input.mousePosition is a Vector3, I made the same mistake thinking it would just be a Vector2 (as you've declared lmbDown and lmbUp). What happens when you run this?

avatar image Christian.Tucker · Aug 29, 2013 at 04:58 AM 0
Share

@getyour411 - It works perfectly fine, I dont even see how mousePosition being a Vector3 is even possible, as it only holds and X and Y value, so Z would always be 0 if that was the case.

This does what I want it to (Creates the GUI.Box where i drag the mouse, and deletes in when I let go of the button), Im just trying to figure out how to select units inside of it, and add it to an Array of GameObjects.

avatar image getyour411 · Aug 29, 2013 at 06:00 AM 0
Share

I guess that's what happens to Z (link). Try this link for RTS style selection of items in the box

http://answers.unity3d.com/questions/168680/view.html

avatar image Christian.Tucker · Aug 29, 2013 at 06:09 AM 0
Share

Either way, I've gotten it working, thanks for the comments. The link that you provided basically did it the same what that I did

1 Reply

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

Answer by graslany · Aug 29, 2013 at 06:21 AM

What are your "units" ? Other GUI elements ? GameObjects from your scene "behind" the GUI layer ? Something else ?

In the former case I would recommend to compute whever there is an intersection between your selection rectangle and the bounding rectangles of any other GUI element.

In the latter case, you need a way to determine whever your game objects are included in the frustum of world space that projects to your 2D rectangle in screen space. In other "words", with a schematic, you want to find the red units and therefore you need to compute which units are in the red frustum :

alt text

There is no unique answer to this question, it all depends on the assumptions that you can make on you scene and the precision you want to achieve. For instance, you will not write the same code if you want that a unit becomes selected when a single pixel of it is in the selection rectangle or if "half of it roughly is inside". But to make it simple and stupid in the second case, I think I'd project every unit's transform position on the screen plane, then check whether the projected position lies in my selection rectangle.

EDIT : getyour411 suggested exactly the same thing in his comment to the original post while I was writing mine, the linked page contains a relevant code snippet.


g3925.png (44.9 kB)
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 Christian.Tucker · Aug 29, 2013 at 08:11 AM 0
Share

That is detailed. Thanks for the information.

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

Cannot implicitly convert type `System.Action' to `bool' 1 Answer

Create squares and draw them on terrain 1 Answer

Plane grid system resource demanding 1 Answer

Is Unity Basic Allow to publish application on App Store? 1 Answer

How to convert TexturePacker's pixel unit in Unity 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