我需要通过在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 :(得分:3)
将脚本DragRigidbody.js
添加到相机中。它包含在StandardAssets/Scripts/GeneralScripts/
的unity的默认资产中,它完全符合您的要求。
答案 1 :(得分:1)
这可能对你有帮助.. :))
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;
}
答案 2 :(得分:0)
如果您的游戏是2D游戏,我认为这可能会有所帮助。
void Update{
if (Input.GetMouseButton())
{
transform.position = Input.mousePosition;
}
}