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 /
  • Help Room /
avatar image
0
Question by DJGhostViper · Nov 13, 2016 at 01:31 AM · guicanvasrtsselectionbox

RTS Click Drag Selection box not working with GUI canvas

I need help with a drag select box I've made; the problem is when I click and drag with no GUI canvas obstructing the camera the click drag works perfectly but when I click and drag with the GUI canvas in the way it doesn't work at all. How could I get the click drag box to ignore the GUI canvas and still work even though the GUI canvas is still on? Thanks!

This is the Selecting Unit (Drag box) script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class SelectUnit : MonoBehaviour {
 
     public GameObject selectedunit;
     public List<GameObject> selectedunits = new List<GameObject>();
     RaycastHit hit;
     private Vector3 MouseDownPoint, CurrentDownPoint;
     public bool IsDragging;
     private float BoxWidth, BoxHeight, BoxLeft, BoxTop;
     private Vector2 BoxStart, BoxFinish;
     public List<GameObject> UnitsOnScreenSpace = new List<GameObject>();
     public List<GameObject> UnitInDrag = new List<GameObject>();
 
     void OnGUI()
     {
         if(IsDragging)
         {
             GUI.Box(new Rect(BoxLeft, BoxTop, BoxWidth, BoxHeight), "");
         }
     }
     void LateUpdate()
     {
         UnitInDrag.Clear();
         if(IsDragging && UnitsOnScreenSpace.Count > 0)
         {
             selectedunit = null;
             for (int i = 0; i < UnitsOnScreenSpace.Count; i++)
             {
                 GameObject UnitObj = UnitsOnScreenSpace[i] as GameObject;
                 Unit PosScript = UnitObj.transform.GetComponent<Unit>();
                 GameObject selectmarker = UnitObj.transform.Find("Marker").gameObject;
                 if(!UnitInDrag.Contains(UnitObj))
                 {
                     if (UnitWithinDrag(PosScript.ScreenPos))
                     {
                         selectmarker.SetActive(true);
                         UnitInDrag.Add(UnitObj);
                     }
                     else
                     {
                         if (!UnitInDrag.Contains(UnitObj))
                             selectmarker.SetActive(false);
                     }
                 }
             }
         }
     }
 
     void Update () {
         if (Input.GetMouseButton(0))
         {
             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
             {
                 if(hit.transform.tag != "SelectableUnit")
                 {
                     if(CheckIfMouseIsDragging())
                     {
                         IsDragging = true;
                     }
                 }
             }
         }
         if(Input.GetMouseButtonUp(0))
         {
             PutUnitsFromDragIntoSelectedUnits();
             IsDragging = false;
         }
         if (selectedunit == null)
         {
             if (Input.GetMouseButtonDown(0))
             {
                 if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
                 {
                     if (hit.transform.tag == "SelectableUnit")
                     {
                         selectedunit = hit.transform.gameObject;
                         selectedunit.transform.FindChild("Marker").gameObject.SetActive(true);
                         for (int i = 0; i < selectedunits.Count; i++)
                         {
                             selectedunits[i].transform.FindChild("Marker").gameObject.SetActive(false);
                         }
                         selectedunits.Clear();
                     }
                     if(hit.transform.tag == "Floor")
                     {
                         for (int i = 0; i < selectedunits.Count; i++)
                         {
                             selectedunits[i].transform.FindChild("Marker").gameObject.SetActive(false);
                         }
                         selectedunits.Clear();
                     }
                 }
             }
         }
         else
         { 
             if (Input.GetMouseButtonDown(0) && !Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.RightShift))
             {
                 if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
                 {
                     if (hit.transform.tag == "SelectableUnit")
                     {
                         selectedunit.transform.FindChild("Marker").gameObject.SetActive(false);
                         selectedunit = null;
                         selectedunit = hit.transform.gameObject;
                         selectedunit.transform.FindChild("Marker").gameObject.SetActive(true);
 
                     }
                     if (hit.transform.tag == "Floor")
                     {
 
 
                         selectedunit.transform.FindChild("Marker").gameObject.SetActive(false);
                         selectedunit = null;
                     }
                 }
             }
         }
 
         //ADD If shift is down already do this later..
         if (Input.GetMouseButtonDown(0) && Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
         {
             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
             {
                 if (hit.transform.tag == "SelectableUnit")
                 {
                     if(selectedunit != null)
                     {
                         selectedunits.Add(selectedunit);
                         selectedunit = null;
                     }
                     selectedunits.Add(hit.transform.gameObject);
                     for (int i = 0; i < selectedunits.Count; i++)
                     {
                         selectedunits[i].transform.FindChild("Marker").gameObject.SetActive(true);
                     }
 
                 }
             }
         }
         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
             CurrentDownPoint = hit.point;
         if(Input.GetMouseButtonDown(0))
         {
             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
                 MouseDownPoint = hit.point;
         }
         if(IsDragging)
         {
             Debug.Log ("Dragging");
 
             BoxWidth = Camera.main.WorldToScreenPoint(MouseDownPoint).x - Camera.main.WorldToScreenPoint(CurrentDownPoint).x;
             BoxHeight = Camera.main.WorldToScreenPoint(MouseDownPoint).y - Camera.main.WorldToScreenPoint(CurrentDownPoint).y;
             BoxLeft = Input.mousePosition.x;
             BoxTop = (Screen.height - Input.mousePosition.y) - BoxHeight;
 
             if(BoxWidth > 0f && BoxHeight < 0f)
             {
                 BoxStart = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
             }
             else if (BoxWidth > 0f && BoxHeight > 0f)
             {
                 BoxStart = new Vector2(Input.mousePosition.x, Input.mousePosition.y + BoxHeight);
             }
             else if (BoxWidth < 0f && BoxHeight < 0f)
             {
                 BoxStart = new Vector2(Input.mousePosition.x + BoxWidth, Input.mousePosition.y);
             }
             else if (BoxWidth < 0f && BoxHeight > 0f)
             {
                 BoxStart = new Vector2(Input.mousePosition.x + BoxWidth, Input.mousePosition.y + BoxHeight);
             }
             BoxFinish = new Vector2(BoxStart.x + Unsigned(BoxWidth), BoxStart.y - Unsigned(BoxHeight));
         }
     }
     float Unsigned(float val)
     {
         if (val < 0f)
             val *= -1;
         return val;
     }
     private bool CheckIfMouseIsDragging()
     {
         if (CurrentDownPoint.x - 2 >= MouseDownPoint.x || CurrentDownPoint.y - 2 >= MouseDownPoint.y || CurrentDownPoint.z - 2 >= MouseDownPoint.z ||
             CurrentDownPoint.x < MouseDownPoint.x - 2 || CurrentDownPoint.y < MouseDownPoint.y - 2 || CurrentDownPoint.z < MouseDownPoint.z - 2)
             return true;
         else
             return false;
     }
     public bool UnitWithinScreenSpace(Vector2 UnitScreenPos)
     {
         if ((UnitScreenPos.x < Screen.width && UnitScreenPos.y < Screen.height) && (UnitScreenPos.x > 0f && UnitScreenPos.y > 0f))
             return true;
         else
             return false;
     }
     public bool UnitWithinDrag(Vector2 UnitScreenPos)
     {
         if ((UnitScreenPos.x > BoxStart.x && UnitScreenPos.y < BoxStart.y) && (UnitScreenPos.x < BoxFinish.x && UnitScreenPos.y > BoxFinish.y))
             return true;
         else
             return false;
     }
     public void PutUnitsFromDragIntoSelectedUnits()
     {
         if(UnitInDrag.Count > 0)
         {
             for (int i = 0; i < UnitInDrag.Count; i++)
             {
                 if (!selectedunits.Contains(UnitInDrag[i]))
                     selectedunits.Add(UnitInDrag[i]);
             }
         }
         UnitInDrag.Clear();
     }
 }







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
Best Answer

Answer by Aluxxi · Nov 13, 2016 at 02:44 AM

Have you considered using a Canvas Group?

https://docs.unity3d.com/460/Documentation/Manual/class-CanvasGroup.html

It would give you access to a variable called blocksRaycasts

https://docs.unity3d.com/460/Documentation/ScriptReference/CanvasGroup.html https://docs.unity3d.com/460/Documentation/ScriptReference/CanvasGroup-blocksRaycasts.html

That way you could set the Canvas to ignore raycasts so they still hit the ground.

 public bool blocksRaycasts;
Comment
Add comment · 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

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

93 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Ignoring image on screen overlay canvas for raycasting 0 Answers

Having constant trouble with Unity's inbuilt UI system. 2 Answers

Unity CanvasGroup's alpha is set to 0 when Instantiate 0 Answers

Unity 5 buttons not interacting within a nested canvas 1 Answer

How can I organize menus as a tree hierarchy? 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