2015-07-11 49 views
0

希望有人对此有一个快速的回答,因为我一直无法弄清楚这一点。而不是让图像跳转到鼠标光标的中心,我希望能够从图像上的任何位置拖动图像而不用跳转。我知道这与将图像的鼠标位置引用到图像或将图像的原点重新设置到鼠标位置有关,但我不知道如何对其进行编码。有没有人已经这样做了?使用C#。用鼠标拖动时图像跳转到中心点

Vector3 partsPanelScale; 
public Vector3 buildPanelScale; 

public Transform placeholderParent = null; 
public Transform parentToReturnTo = null; 

GameObject placeholder = null; 

public GameObject animalPart; 

public GameObject trashCan; 
public GameObject partsPanel; 
public GameObject partsWindow; 
GameObject buildBoard; 
GameObject dragLayer; 

private float _mX; // holds current eventData.position.x 
private float _mY; // holds current eventData.position.y 
private float _pmX;// holds previous eventData.position.x 
private float _pmY;// holds previous eventData.position.y 

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) 
{ 
    // each frame updates the current position of the mouse. 
    _mX = eventData.position.x; 
    _mY = eventData.position.y; 

    // create placeholder gap and hold correct position in layout 
    placeholder = new GameObject(); 
    placeholder.transform.SetParent(transform.parent); 
    placeholder.transform.SetSiblingIndex(transform.GetSiblingIndex()); 

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

    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 

float distance = 0; 



public void OnDrag (PointerEventData eventData) 
{ 

    // Divided the difference by 6 to reduce the speed of dragging. 
    transform.position = new Vector3 
     (
      (_pmX - _mX)/6 + transform.position.x, 
      (_pmY - _mY)/6 + transform.position.y, 
      distance 
      ); 

    // Vector3 mousePosition = new Vector3(eventData.position.x, eventData.position.y, distance); 
    // Vector3 objPosition = Camera.main.ViewportToScreenPoint(mousePosition); 
    // 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 partsPnael 
    } 

    if(transform.parent.gameObject == buildBoard) 
    { 
     // Constrain drag to boundaries of buildBoard Code 
    } 
} 

#endregion 

#region IEndDragHandler implementation 

public void OnEndDrag (PointerEventData eventData) 
{ 
    // end of the drag. set the previous position. 
    _pmX = _mX; 
    _pmY = _mY; 

    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) 
    { 
     // Debug.Log ("Your sprite is now on the " + transform.parent.name); 

     transform.localScale = buildPanelScale; 
     transform.SetAsLastSibling();          // always place last piece on top 
    } 

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

#endregion 

回答

1

要正确拖动您需要的鼠标都以前当前位置。

因为每次移动鼠标时,图像都应该从它以前的位置移动一点(在x和y坐标上)。

如果您只是使用当前位置并将其直接设置为图像,resault将跳过屏幕。

您必须通过获取之前和当前位置的差异(x2 - x1y2 - y1)来变换图像变换。然后将最终变换设置为图像变换。

您还需要MouseOver事件来更新职位。 (或MouseMove

private double _mX; // holds current eventData.position.x 
private double _mY; // holds current eventData.position.y 
private double _pmX;// holds previous eventData.position.x 
private double _pmY;// holds previous eventData.position.y 

// each frame updates the current position of the mouse. 
private void MouseOver(PointerEventData eventData) 
{ 
    _mX = eventData.position.x; 
    _mY = eventData.position.y; 
} 

public void OnDrag (PointerEventData eventData) 
{ 
    transform.position = new Vector3D((_pmX - _mX)/6 + transform.position.x, 
             (_pmY - _mY)/6 + transform.position.y, distance); 
    // Divided the difference by 6 to reduce the speed of dragging. 

    //... 

    // end of the drag. set the previous position. 
    _pmX = _mX; 
    _pmY = _mY; 
} 

注意我用的是类名和事件可能从unity3d不同。

如果您从右向左拖动但图像从左向右替换_pmX - mX_mX - _pmX

如果您从上往下拖动但图像从下往上取代_pmY - mY_mY - _pmY

+0

感谢您的输入!尽管如此,我仍然遇到问题。我已更新该帖子以反映您所做的代码更改。我还包括整个班级,看看一切都是如何设计的。我确信我只是错过了一些愚蠢的事情,但我真的可以用这些帮助来解决这个问题。谢谢! – greyBow

+0

嗯。它似乎我无法找到你的问题在这里。但对于你的情况,图像跳转到中心的问题是关于转换对象。你可以在这里问你的问题[Unity问一个问题](http://answers.unity3d.com/questions/ask.html)。它的团结和非常肯定你会得到你的答案。 @KenMarold –