精灵图像周围的空白空间注册指针事件

时间:2015-08-11 18:50:14

标签: c# unity3d click

我使用Unity 5,使用新的uGUI系统,并且我很难获得指针事件来忽略我的精灵周围的空白空间并仅注册图像区域。我附上了图片和代码,以显示我所处的位置。我不能让这个工作。

最终,我想要实现的是让click事件与图像交互,而不是整个图像sprite(矩形边界框中的所有内容)。

我的图像纹理类型是Sprite(2D和UI)。我确保图像周围的空间确实是透明的,但图像周围的空白区域仍然会记录下来。

我在这里做错了什么?需要帮助!

enter image description here

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);
    Vector3 startPosition;

    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 original parent location
        placeholderParent = parentToReturnTo;                                   // set placeholder gameobject transform

        startPosition = transform.position;

        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 = Input.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

}

0 个答案:

没有答案