Search Unity

Draggable area of sprite defined by 2D Polygon Collider?

Discussion in 'Scripting' started by kenmarold, Aug 11, 2015.

  1. kenmarold

    kenmarold

    Joined:
    Jun 11, 2015
    Posts:
    27
    I've got a draggable class the works great for dragging entire sprites but I need to modify it to register dragging only within a defined 2D polygon collider component of a sprite. How do I modify my script to work in this way?

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    using UnityEngine.EventSystems;
    using System.Collections.Generic;

    public class DragHandling : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler
    {
    public float partScale;

    [HideInInspector] public Transform placeholderParent = null;
    [HideInInspector] public Transform parentToReturnTo = null;
    [HideInInspector] public GameObject trashCan;
    [HideInInspector] public GameObject partsPanel;
    [HideInInspector] public GameObject partsWindow;
    [HideInInspector] public GameObject buildBoard;

    GameObject placeholder = null;
    GameObject dragLayer;
    Vector3 buildPanelScale;
    Vector3 partsPanelScale = new Vector3(1.0f, 1.0f, 1.0f);

    void Start ()
    {
    dragLayer = GameObject.FindGameObjectWithTag("DragLayer");
    buildBoard = GameObject.FindGameObjectWithTag("Board");
    partsPanel = GameObject.FindGameObjectWithTag("Parts");
    partsWindow = GameObject.FindGameObjectWithTag("PartsWindow");
    trashCan = GameObject.FindGameObjectWithTag("Trash");
    }

    #region IPointerClickHandler implementation

    public void OnPointerClick (PointerEventData eventData)
    {
    if(transform.parent.gameObject == buildBoard)
    transform.SetAsLastSibling();
    }

    #endregion

    #region IBeginDragHandler implementation

    public void OnBeginDrag (PointerEventData eventData)
    {
    // create placeholder gap and hold correct position in layout
    placeholder = new GameObject();
    placeholder.transform.SetParent(transform.parent);
    placeholder.transform.SetSiblingIndex(transform.GetSiblingIndex());

    parentToReturnTo = transform.parent; // store current parent location
    placeholderParent = parentToReturnTo; // set placeholder gameobject transform

    GetComponent<CanvasGroup>().blocksRaycasts = false; // turn off image raycasting when dragging image in order to see what's behind the image
    }

    #endregion

    #region IDragHandler implementation

    public void OnDrag (PointerEventData eventData)
    {
    Vector3 mousePosition = new Vector3(eventData.position.x, eventData.position.y, 0);
    transform.position = mousePosition; // set object coordinates to mouse coordinates

    if(transform.parent.gameObject == partsPanel)
    transform.SetParent(dragLayer.transform); // pop object to draglayer to move object out of parts Panel

    if(transform.parent.gameObject == buildBoard)
    transform.SetParent(dragLayer.transform);
    }

    #endregion

    #region IEndDragHandler implementation

    public void OnEndDrag (PointerEventData eventData)
    {
    transform.SetParent(parentToReturnTo); // Snaps object back to orginal parent if dropped outside of a dropzone
    transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex()); // Returns card back to placeholder location

    GetComponent<CanvasGroup>().blocksRaycasts = true; // turn Raycast back on
    Destroy(placeholder); // kill the placeholder if object hits a drop zone or returns to parts panel

    if(transform.parent.gameObject == buildBoard)
    {
    buildPanelScale = new Vector3(partScale, partScale, partScale);
    transform.localScale = buildPanelScale;
    transform.SetAsLastSibling(); // always place last piece on top
    }

    if(transform.parent.gameObject == partsPanel)
    transform.localScale = partsPanelScale;
    }

    #endregion

    }
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Couple if options.

    If it's an in game sprite use the physics raycaster instead of a graphics raycaster. This means you will have to pull it off of the canvas.

    If it must be part of the UI you could perhaps mess with the graphics raycaster to include checking against colliders? Maybe.