Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Have UI object not affected by Mask when dragging?

Discussion in 'UGUI & TextMesh Pro' started by Tekadept, Feb 12, 2015.

  1. Tekadept

    Tekadept

    Joined:
    Feb 12, 2015
    Posts:
    6
    Hi I am playing around with the new Unity 4.6 ui, and I have come across an issue I can't figure out.

    I have a panel which contains Inventory items with a scroll bar, as it is quite a large inventory I have added a UI Mask so that it looks clean and only shows what is there Withing ScrollWindow.

    I have added Drag drop functionality on the Inventory items, so I can mousdown and drag them around, and it works great whilst I am dragging within the Inventory Panel, however when I try and drag this object to the ground the icon it dissapears.

    To drag the item I am using the IdragHandler event and move the items via position.
    public void OnDrag (PointerEventData eventData)
    {
    transform.position = Input.mousePosition;
    }

    I Know why it is dissapearing, because of the UI Mask, so is there a way that I can in the OnDrag() have it so that the transform is not affected by the mask?

    If I disable the mask whilst doing the drag it makes the inventory panel look horrible until i finish the drop which Isn't what i am hoping for.
     
    Last edited: Feb 12, 2015
    MajidKhalili likes this.
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    You have to re-parent the object you are dragging outside of the masked hierarchy to prevent the mask to work on the dragged object.
    Best you save the parent transform on drag start and restore it on drag end.
     
    Dudey, MajidKhalili and Tekadept like this.
  3. Tekadept

    Tekadept

    Joined:
    Feb 12, 2015
    Posts:
    6
    Thankyou, I have done that, and that works but has introduced a side effect.

    My layout is
    Canvas
    --Panel
    ----Inventoryscrollrect(has mask)
    ------InventoryGrid (made up of itemImage with a grid layout)

    I have taken your advise and on drag parent the Itemimage to Panel instead of InventoryGrid, this gives the behaviour I am expecting, however to determine whether my mouse is over the GUI on the side when I release i am using
    eventsystem.IsPointerOverGameObject ()
    This works when the Itemimage is the original, however after setting the parent to Panel the eventsystem.IsPointerOverGameObject () always returns true, which means I can place objects behind the gui.

    Hope that makes sense?
     
  4. Tekadept

    Tekadept

    Joined:
    Feb 12, 2015
    Posts:
    6
    Actually was a silly thing, of course I was dragging a UI element so no matter where I was i was always hitting.... So i added a Canvasgroup, and on drag disabled block raycasts, then reenabled after placement.. perfect :)
     
  5. Hoorza

    Hoorza

    Joined:
    May 8, 2016
    Posts:
    40
    I have just stumbled upon the same problem. Using Unity 2020.1.4f1. Not sure about previous versions but you can mark your Image as not Maskable in the Images properties. The same goes for Text Mesh Pro. Open the "Extra Settings" tab in the inspector and you see option "Maskable". Untick it and the Parent's mask won't affect this Image/text object.
     
  6. Qwertinx

    Qwertinx

    Joined:
    Apr 23, 2022
    Posts:
    1
    Hoorza Thank you so much. It is very useful