繁体   English   中英

在C#UNITY3D中拖动鼠标?

[英]Mouse Drag in 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);
        }
    } 

}

将脚本DragRigidbody.js添加到相机。 它包含在unity的默认资产中,位于StandardAssets/Scripts/GeneralScripts/ ,并且可以完全满足您的要求。

这可能会帮助您.. :)

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;
}

我认为,如果您的游戏是2D的,这可能会有所帮助。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM