手机上的Unity拖放问题

时间:2017-12-09 09:06:11

标签: c# android unity3d drag-and-drop unityscript

我正在尝试为Android制作拖放游戏。 当我按下一个形状时,它会实例化一个具有该形状的对象,并且该对象跟随我的手指。在统一编辑器上,一切正常,但是当我在手机上尝试时,它有一些问题。如果我抬起手指,物体会以非常高的速度而不是正常速度下降。而且,如果我用跟随手指的物体击中另一个物体,它会穿过第二个物体。这只在我释放对象之前发生。如果我释放它然后我把手指放在它上面,那么它具有正确的重力并且它不能通过其他物体。 这是我的代码。

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Object_Spawm : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler 
{
    public float object_max_number;
    public Text object_number_text;
    public float object_number;
    //bool created = false;
    //public bool isOver = false;
    //public Transform object_to_spawn;

    public Transform prefab ;
    private Transform spawn;
    private bool isOver = false;
    public bool  created = false;


    public void OnPointerEnter(PointerEventData eventData)
    {
        if (spawn==null) {
            Debug.Log ("Mouse enter");
            isOver = true;
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("Mouse exit");
        isOver = false;
    }

    void  Update() 
    {
        //Debug.Log (created);
        if (Input.touchCount == 0) {
            //created = false;

        }
        if (Input.GetMouseButton(0) && spawn != null) {
            var pos = Input.mousePosition;
            pos.z = -Camera.main.transform.position.z;
            spawn.transform.position = Camera.main.ScreenToWorldPoint(pos);

        }

        if (Input.GetMouseButtonUp(0)) {
            spawn = null;
            created = false;
        }
    }

    void OnGUI() 
    {
        Event e = Event.current;
        if (object_number > 0)
        //if (e.type == EventType.MouseDown && isOver ==true && spawn==null ){ //if (e.type == EventType.MouseDown && isOver ==true ) 
        if(Input.touchCount == 1 && isOver == true && spawn==null){
            var pos = Input.mousePosition;
            pos.z = -Camera.main.transform.position.z;
            pos = Camera.main.ScreenToWorldPoint(pos);
            spawn = Instantiate(prefab, pos, Quaternion.identity) as Transform;

            object_number -= 1;
            UpdateText ();
            created = true;
        }
    }

    void Start()
    {
        object_number = object_max_number;
        UpdateText ();
    }


    void UpdateText()
    {
        object_number_text.text = object_number.ToString();
    }
}

0 个答案:

没有答案