【发布时间】:2016-05-17 04:27:51
【问题描述】:
我有一个 Canvas 设置,它位于屏幕空间 - 叠加层中,我几乎找到了我想要的东西,但结果有点偏离。稍微偏离我的意思是当我单击并按住以从我的库存中拖动某些东西时,它并不完全是鼠标所在的位置,但它很接近。现在拖动不起作用,但是当我完成拖动时,我的游戏对象会正确返回到各自的位置。
我的代码:
void Awake(){
rectTrans = GetComponent<RectTransform> ();
}
void Start(){
localRectTrans = rectTrans.localPosition;
}
public void OnPointerUp(PointerEventData data){
// IF we are dragging an item
if(itemBeingDragged != null){
itemBeingDragged = null;
// return this gameobject to its original location.
rectTrans.localPosition = localRectTrans;
}
}
public void OnBeginDrag(PointerEventData data){
// IF we have an item to drag.
if(isItem){
// Set the itemBeingDragged to this gameobject.
itemBeingDragged = gameObject;
// Get the starting mouse location for dragging.
startMousePos = Input.mousePosition;
}
}
public void OnDrag(PointerEventData data){
// IF we have an item to drag in this inventory slot.
if(isItem){
// Get the location of the mouse.
Vector2 curMousePosition = Input.mousePosition;
// Get the difference in position from when the mouse was first clicked to where it is currently being dragged.
Vector2 diffInPos = curMousePosition - startMousePos;
// The current position based on the difference in position with the mouse and the local position of this inventory slot.
Vector2 curPos = localRectTrans + diffInPos;
transform.localPosition = curPos;
}
}
我怎样才能让它实际上正好是我的鼠标光标所在的位置?我觉得我很接近但只是保持空白。
【问题讨论】: