2013-07-04 71 views
-1

我需要通過單擊並拖動C#Unity3D來移動多維數據集。我的代碼目前通過按一個按鈕來創建多維數據集。鼠標在C#UNITY3D中拖動?

using UnityEngine; 
using System.Collections; 

public class CDraggable : MonoBehaviour 
{ 
    Texture btnimg; 

    // Use this for initialization 
    void Start() 
    { 
    } 

    // Update is called once per frame 
    Update() 
    { 
     //here to write mousedrag code. 
    } 

    void OnGUI() 
    { 
     if (GUI.Button(new Rect(400, 250, 50, 50), btnimg)) 
     { 
      //Debug.Log("Clicked the button with an image"); 
      GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); 
      cube.transform.position = new Vector3(-0.7F, 2, 0); 
     } 
    } 

} 
+0

你到目前爲止試過了什麼? SO不是你要求別人爲你做你的工作的論壇。 –

回答

3

添加腳本DragRigidbody.js到您的相機。它包含在統一的默認資產StandardAssets/Scripts/GeneralScripts/中,並且它完全符合您的要求。

+0

雖然這是OP想要的結果,但這不是他們想要的語言。 – MichaelHouse

+0

@ Byte56如果OP認爲這是值得的努力,我相信它可以輕鬆移植到C#。 Unity不介意混合和匹配。 –

+0

我同意,這就是爲什麼我upvoted。 – MichaelHouse

0

這可以幫助你.. :)

Vector2 screenPoint = Vector2.Zero ; 


void OnMouseDown() 
{ 
    screenPoint = Camera.main.WorldToScreenPoint(scanPos); 
    offset = scanPos - Camera.main.ScreenToWorldPoint(
     new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 
} 

Vector3 curScreenPoint = Vector3.Zero; 
Vector3 curPosition = Vector3.Zero; 
void OnMouseDrag() 
{ 
curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 

    curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset; 
    transform.position = curPosition; 
} 
0

也許這將幫助如果你的遊戲是2D的,我想。

void Update{ 
if (Input.GetMouseButton()) 
    { 
     transform.position = Input.mousePosition; 
    } 
}