如何用鼠标单击并拖动游戏对象? [英] How can i click and drag a gameobject with the mouse?

查看:105
本文介绍了如何用鼠标单击并拖动游戏对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建了新脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class mouseDrag : MonoBehaviour
{
    float distance = 10;

        void OnMouseDrag()
        {
            Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
            Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);

            transform.position = objPosition;
        }
    }

然后在Start函数的另一个脚本中,我创建4个多维数据集并将mouseDrag添加到每个多维数据集.但是,在运行,单击和拖动时,什么也没有发生.我没有任何错误或异常.

Then in another script in the Start function i'm creating 4 cubes and add the mouseDrag to each cube. But when running and clicking and dragging nothing happen. I'm not getting any errors or exceptions.

void Start()
    {
        lp = gameObjectToRaise.transform.localPosition;
        ls = gameObjectToRaise.transform.localScale;

        List<GameObject> cubes = new List<GameObject>();

        GameObject cube = Cube.CreatePrimitive(Cube.CubePivotPoint.UPLEFT);
        GameObject cube1 = Cube.CreatePrimitive(Cube.CubePivotPoint.UPRIGHT);
        GameObject cube2 = Cube.CreatePrimitive(Cube.CubePivotPoint.BACKUP);
        GameObject cube3 = Cube.CreatePrimitive(Cube.CubePivotPoint.UPRIGHT);

        cubes.Add(cube);
        cubes.Add(cube1);
        cubes.Add(cube2);
        cubes.Add(cube3);

        cube.GetComponentInChildren<Renderer>().material.color = Color.blue;
        cube1.GetComponentInChildren<Renderer>().material.color = Color.red;
        cube2.GetComponentInChildren<Renderer>().material.color = Color.green;
        cube3.GetComponentInChildren<Renderer>().material.color = Color.yellow;

        cube.transform.position = new Vector3(lp.x, lp.y, lp.z - 0.5f);
        cube1.transform.position = new Vector3(lp.x, lp.y, lp.z);
        cube2.transform.position = new Vector3(lp.x, lp.y, lp.z + 5);
        cube3.transform.position = new Vector3(lp.x + 5, lp.y, lp.z);

        cube1.transform.Rotate(0, 90, 0);
        cube3.transform.Rotate(0, 90, 0);

        StartCoroutine(scaleCube(cube.transform));
        StartCoroutine(scaleCube(cube1.transform));
        StartCoroutine(scaleCube(cube2.transform));
        StartCoroutine(scaleCube(cube3.transform));

        foreach (GameObject go in cubes)
        {
            go.AddComponent<mouseDrag>();
        }
    }

    IEnumerator scaleCube(Transform trans)
    {
        while (raiseAmount < raiseTotal)
        {
            raiseAmount += 1f;
            trans.localScale += new Vector3(speed * Time.deltaTime, speed * Time.deltaTime, 0);
            yield return null;
        }
    }
}

推荐答案

我现在刚刚尝试添加一个用于测试它的单个立方体游戏对象 并且工作正常.但是当我使用多维数据集使用多维数据集时 类帮助程序不起作用.

I just tried now with a single cube gameobject i added for testing it and it's working fine. But when i'm using the cubes using the CUBE class helper it's not working.

在这种情况下,应将mouseDrag脚本附加到子多维数据集(而不是CubeHolder对象).此后,应移动多维数据集的父级,即"CubeHolder"

In this case, the mouseDrag script should be attached to the child cube(not the CubeHolder object).After that, you should be moving the parent of the cube which is "CubeHolder".

如果移动子多维数据集,则将断开枢轴点.

If you ever move the child cube, you will break the pivot point.

只需更改

transform.position = objPosition;

transform.parent.position = objPosition;

然后将mouseDrag脚本附加到子多维数据集而不是"CubeHolder" .

then attach the mouseDrag script to the child cube not the "CubeHolder".

也许问题在于它会将脚本附加到 parentObject"CubeHolder"而不是创建的多维数据集?

Maybe the problem is that it's attaching the script to the parentObject "CubeHolder" and not to the created cubes ?

是的.只有将Collider附加到对象,并且子多维数据集是带有Collider的唯一对象时,才会调用OnMouseDrag.父对象仅用作枢轴点特征.

Yes. OnMouseDrag will only be called if attached to an Object with a Collider and the child cube is the only object with the Collider. The parent object is only there to be used as a pivot point feature.

注意:

您不应为此使用OnMouseDrag.您应该使用新的UI事件拖动多维数据集/对象.

You should not use OnMouseDrag for this. You should be dragging the cube/Object with the new UI event. There are many of the callback functions listed in this answer.

下面的脚本是您应该使用的脚本.将CubeDrag sctipt附加到子多维数据集,然后将表示transform.position的所有内容更改为transform.parent.position.

The script below is something you should be using. Attach the CubeDrag sctipt to the child cube then change everything that says transform.position to transform.parent.position.

using UnityEngine;
using UnityEngine.EventSystems;
public class CubeDrag: MonoBehaviour, IPointerDownHandler, IDragHandler, IEndDragHandler
{
    Camera mainCamera;
    float zAxis = 0;
    Vector3 clickOffset = Vector3.zero;

    // Use this for initialization
    void Start()
    {
        addEventSystem();
        zAxis = transform.position.z;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        Vector3 tempPos = eventData.position;
        tempPos.z = Vector3.Distance(transform.position, Camera.main.transform.position);
        clickOffset = transform.position - mainCamera.ScreenToWorldPoint(tempPos);
        Debug.Log("Mouse Down");
    }

    public void OnDrag(PointerEventData eventData)
    {
        Vector3 tempPos = eventData.position;
        tempPos.z = Vector3.Distance(transform.position, Camera.main.transform.position);
        Vector3 tempVec = mainCamera.ScreenToWorldPoint(tempPos) + clickOffset;
        tempVec.z = zAxis;

        transform.position = tempVec;
        Debug.Log("Dragging Cube");
    }

    public void OnEndDrag(PointerEventData eventData)
    {

    }

    void addEventSystem()
    {
        mainCamera = Camera.main;
        if (mainCamera.GetComponent<PhysicsRaycaster>() == null)
            mainCamera.gameObject.AddComponent<PhysicsRaycaster>();

        EventSystem eveSys = GameObject.FindObjectOfType(typeof(EventSystem)) as EventSystem;
        if (eveSys == null)
        {
            GameObject tempObj = new GameObject("EventSystem");
            eveSys = tempObj.AddComponent<EventSystem>();
        }

        StandaloneInputModule stdIM = GameObject.FindObjectOfType(typeof(StandaloneInputModule)) as StandaloneInputModule;
        if (stdIM == null)
            stdIM = eveSys.gameObject.AddComponent<StandaloneInputModule>();
    }
}

这篇关于如何用鼠标单击并拖动游戏对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆