Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Drag and Drop in 5.3

Discussion in 'Immediate Mode GUI (IMGUI)' started by Nakor, Jan 15, 2016.

  1. Nakor

    Nakor

    Joined:
    Sep 5, 2012
    Posts:
    31
    The page at http://docs.unity3d.com/ScriptReference/EventSystems.IDragHandler.html shows a script that doesn't not function (at least for me) in Unity 5.3. I could not find a fixed version anywhere so I fixed it as best as I can figure out and I thought I should share it here. If not correct, it at least functions for now.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. using UnityEngine.UI;
    4.  
    5. [RequireComponent(typeof(Image))]
    6. public class InventoryDragController : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
    7. {
    8.     public bool dragOnSurfaces = true;
    9.  
    10.     private GameObject m_DraggingIcon;
    11.     private RectTransform m_DraggingPlane;
    12.  
    13.     public void OnBeginDrag(PointerEventData eventData)
    14.     {
    15.         var canvas = FindInParents<Canvas>(gameObject);
    16.         if (canvas == null)
    17.             return;
    18.  
    19.         // We have clicked something that can be dragged.
    20.         // What we want to do is create an icon for this.
    21.         m_DraggingIcon = new GameObject("icon");
    22.  
    23.         m_DraggingIcon.transform.SetParent(canvas.transform, false);
    24.         m_DraggingIcon.transform.SetAsLastSibling();
    25.  
    26.         var image = m_DraggingIcon.AddComponent<Image>();
    27.         // The icon will be under the cursor.
    28.         // We want it to be ignored by the event system.
    29.         //m_DraggingIcon.AddComponent<IgnoreRaycast>();
    30.         image.raycastTarget = false;
    31.  
    32.         image.sprite = GetComponent<Image>().sprite;
    33.         image.SetNativeSize();
    34.  
    35.         if (dragOnSurfaces)
    36.             m_DraggingPlane = transform as RectTransform;
    37.         else
    38.             m_DraggingPlane = canvas.transform as RectTransform;
    39.  
    40.         SetDraggedPosition(eventData);
    41.     }
    42.  
    43.     public void OnDrag(PointerEventData data)
    44.     {
    45.         if (m_DraggingIcon != null)
    46.             SetDraggedPosition(data);
    47.     }
    48.  
    49.     private void SetDraggedPosition(PointerEventData data)
    50.     {
    51.         if (dragOnSurfaces && data.pointerEnter != null && data.pointerEnter.transform as RectTransform != null)
    52.             m_DraggingPlane = data.pointerEnter.transform as RectTransform;
    53.  
    54.         var rt = m_DraggingIcon.GetComponent<RectTransform>();
    55.         Vector3 globalMousePos;
    56.         if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane, data.position, data.pressEventCamera, out globalMousePos))
    57.         {
    58.             rt.position = globalMousePos;
    59.             rt.rotation = m_DraggingPlane.rotation;
    60.         }
    61.     }
    62.  
    63.     public void OnEndDrag(PointerEventData eventData)
    64.     {
    65.         if (m_DraggingIcon != null)
    66.             Destroy(m_DraggingIcon);
    67.     }
    68.  
    69.     static public T FindInParents<T>(GameObject go) where T : Component
    70.     {
    71.         if (go == null) return null;
    72.         var comp = go.GetComponent<T>();
    73.  
    74.         if (comp != null)
    75.             return comp;
    76.  
    77.         Transform t = go.transform.parent;
    78.         while (t != null && comp == null)
    79.         {
    80.             comp = t.gameObject.GetComponent<T>();
    81.             t = t.parent;
    82.         }
    83.         return comp;
    84.     }
    85. }
    86.  
    If someone else has already done this (probably) sorry. Hopefully it helps someone :)
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,223
    Hey. If you find a problem with the docs you can report it with the bug reporter so we can fix it ;)
     
  3. Nakor

    Nakor

    Joined:
    Sep 5, 2012
    Posts:
    31
    I tried figuring out how to do that on the site but I couldn't figure out how to post a bug. Odd that I never even considered trying to do it from within Unity. Makes sense now I suppose. Thanks, learn something new every day :)

    I expect you already submitted one or should I still do it?
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,223
    No I didn't.probably should have in hindsight. Can you submit one please :)
     
  5. Nakor

    Nakor

    Joined:
    Sep 5, 2012
    Posts:
    31
    Sure no problem.
     
    karl_jones likes this.