2015-07-11 70 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 –