C# 清除精灵图像周围的空间,以注册指针事件
我使用的是Unity 5,新的uGUI系统,我很难让指针事件忽略精灵周围的空白,只注册图像区域。我已经附上了图片和代码,以显示我在哪里。我不能让这个工作 最终,我试图实现的是让单击事件与图像交互,而不是与整个图像精灵(矩形边界框中的所有内容)交互 我的图像纹理类型是精灵(2D和UI)。我确保我的图像周围的空间确实是透明的,但是图像周围的清晰空间仍然记录着点击 我做错了什么?需要帮助C# 清除精灵图像周围的空间,以注册指针事件,c#,unity3d,click,C#,Unity3d,Click,我使用的是Unity 5,新的uGUI系统,我很难让指针事件忽略精灵周围的空白,只注册图像区域。我已经附上了图片和代码,以显示我在哪里。我不能让这个工作 最终,我试图实现的是让单击事件与图像交互,而不是与整个图像精灵(矩形边界框中的所有内容)交互 我的图像纹理类型是精灵(2D和UI)。我确保我的图像周围的空间确实是透明的,但是图像周围的清晰空间仍然记录着点击 我做错了什么?需要帮助 使用UnityEngine; 使用UnityEngine.UI; 使用系统集合; 使用UnityEngine.E
使用UnityEngine;
使用UnityEngine.UI;
使用系统集合;
使用UnityEngine.EventSystems;
使用System.Collections.Generic;
公共类拖动:MonoBehavior、IBeginDragHandler、IDragHandler、IENDragHandler、IPointerClickHandler
{
公众持股比例;
[HIDEININSECTT]公共转换占位符父项=null;
[HideInInstitt]公共转换parentToReturnTo=null;
[HideInInstitt]公共游戏对象垃圾桶;
[HideInInstit]公共游戏对象partsPanel;
[HideInInstit]公共游戏对象部分窗口;
[HideInInstitt]公共游戏对象构建板;
游戏对象占位符=null;
游戏对象dragLayer;
向量3-尺度;
Vector3零件尺寸=新Vector3(1.0f、1.0f、1.0f);
矢量3起始位置;
无效开始()
{
dragLayer=GameObject.FindGameObjectWithTag(“dragLayer”);
buildBoard=GameObject.FindGameObjectWithTag(“Board”);
partsPanel=GameObject.FindGameObjectWithTag(“Parts”);
partsWindow=GameObject.FindGameObjectWithTag(“partsWindow”);
垃圾桶=GameObject.FindGameObjectWithTag(“垃圾桶”);
}
#区域IPoClickHandler实现
公共void OnPointerClick(PointerEventData事件数据)
{
if(transform.parent.gameObject==buildBoard)
transform.SetAsLastSibling();
}
#端区
#区域IBeginDragHandler实现
公共void OnBeginDrag(PointerEventData事件数据)
{
//创建占位符间隙并在布局中保持正确位置
占位符=新游戏对象();
占位符.transform.SetParent(transform.parent);
placeholder.transform.SetSiblingIndex(transform.GetSiblingIndex());
parentToReturnTo=transform.parent;//存储原始父位置
placeholder Parent=parentToReturnTo;//设置占位符游戏对象转换
startPosition=transform.position;
GetComponent().blocksRaycasts=false;//拖动图像时关闭图像光线投射,以便查看图像后面的内容
}
#端区
#区域IDragHandler实现
public void OnDrag(PointerEventData事件数据)
{
Vector3 mousePosition=新的Vector3(eventData.position.x,eventData.position.y,0);
transform.position=Input.mousePosition;//将对象坐标设置为鼠标坐标
if(transform.parent.gameObject==partsPanel)
transform.SetParent(dragLayer.transform);//将对象弹出到dragLayer以将对象移出零件面板
if(transform.parent.gameObject==buildBoard)
SetParent(dragLayer.transform);
}
#端区
#区域IEndDragHandler实现
公共无效OnEndRag(PointerEventData事件数据)
{
transform.SetParent(parentToReturnTo);//如果将对象放在dropzone之外,则将对象捕捉回原始父对象
transform.SetSiblingIndex(占位符.transform.GetSiblingIndex());//将卡片返回占位符位置
GetComponent().blocksRaycasts=true;//重新启用光线投射
销毁(占位符);//如果对象碰到放置区域或返回到零件面板,则删除占位符
if(transform.parent.gameObject==buildBoard)
{
buildPanelScale=新矢量3(零件比例,零件比例,零件比例);
transform.localScale=buildPanelScale;
transform.SetAsLastSibling();//始终将最后一块放在顶部
}
if(transform.parent.gameObject==partsPanel)
transform.localScale=partsPanelScale;
}
#端区
}
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
}