用多个手指同时拖动不同的对象

时间:2017-05-29 06:58:51

标签: c# unity3d touch multi-touch

我遵循本教程 https://www.youtube.com/watch?v=SrCUO46jcxk

对于多点触控,它可以工作。并尝试使用多个手指同时在每个对象上添加拖动功能。  这是我的按钮代码我添加了一些代码,使它可以同时拖动每个对象,但它只适用于单个对象。我尝试了所有相关的帖子和​​教程,但没能表演,请帮忙??

interm(i)

此代码适用于输入触控功能

using System.Collections;
using UnityEngine;

public class Button : MonoBehaviour{

 public Color defaultColour;
 public Color selectedColour;
 private Material mat;

 private Vector3 screenPoint;
 private Vector3 offset;


 void Start(){
  mat = GetComponent<Renderer> ().material;
 }

 void OnTouchDown(){
  mat.color = selectedColour;
  Debug.Log ("Touch Down");

  screenPoint = Camera.main.WorldToScreenPoint (transform.position);
  offset = transform.position - Camera.main.ScreenToWorldPoint (new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));


  }

 void OnTouchUp(){
  mat.color = defaultColour;
  Debug.Log ("Touch Up");
 }

 void OnTouchStay(){
  mat.color = selectedColour;
  Debug.Log ("Touch Stay");

  Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint) + offset;
  transform.position = curPosition;


 }

 void OnTouchExit(){
  mat.color = defaultColour;
  Debug.Log ("Touch Exit");
 }
}

1 个答案:

答案 0 :(得分:1)

现在终于执行这个过程,我可以用两种方式执行这个过程

  1. 使用touchScript库,它具有丰富的功能,我们可以一次拖动,旋转,平移和缩放多个对象。 https://www.youtube.com/watch?v=HHLBJ1Ss2S0

    注意:这是较旧的视频,现在它会发生变化,但几乎相同。 [TouchScript在资产商店中免费提供]

  2. 其他选项,在空游戏对象或相机中添加以下代码(添加标签层)

    使用System.Collections; 使用System.Collections.Generic; 使用UnityEngine; 使用UnityEngine.UI;

    public struct objectst {     public Vector3 screenPoint;     public Vector3 offset; }

    public class TouchInput:MonoBehaviour {

    public LayerMask touchInputMask;
    private static List<GameObject> touchList = new List<GameObject>();
    public static Dictionary<int,objectst> touchobjects = new Dictionary<int,objectst>();
    
    private GameObject[] touchesOld;
    private RaycastHit hit;
    public Text Count, IndexLift;
    
    private Vector3 targetPos;
    
    
    
    
    
    void Update () {
    
    
        int nbTouches = Input.touchCount;
    
        if(nbTouches > 0)
        {
            nbTouches = 5;
    
            print(nbTouches + " touch(es) detected");
    
            touchesOld = new GameObject[touchList.Count];
            touchList.CopyTo(touchesOld);
            touchList.Clear();
    
            for (int i = 0; i < nbTouches ; i++)
            {
    
    
                Touch touch = Input.GetTouch(i);
    
    
                print("Touch index " + touch.fingerId + " detected at position " + touch.position);
    
                Ray ray = Camera.main.ScreenPointToRay (touch.position);
    
    
                if (Physics.Raycast(ray, out hit, touchInputMask)){
                    GameObject recipient = hit.transform.gameObject;
                    touchList.Add(recipient);
    
                    //recipient.;
    
                    if (touch.phase == TouchPhase.Began){
    
                        objectst tempobj = new objectst ();
                        tempobj.screenPoint = Camera.main.WorldToScreenPoint (recipient.transform.position);
                        tempobj.offset = recipient.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (touch.position.x, touch.position.y, tempobj.screenPoint.z));           
    
                        touchobjects.Add(touch.fingerId,tempobj);
    
    
    
    
                    }
    
                    if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved){
    
    
                        Vector3 curScreenPoint = new Vector3 (touch.position.x, touch.position.y,
                            touchobjects[touch.fingerId].screenPoint.z);
                        Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint) + touchobjects[touch.fingerId].offset;
                        recipient.transform.position = curPosition;
    
    
    
                    }
                    if (touch.phase == TouchPhase.Ended){
    
                        Vector3 curScreenPoint = new Vector3 (touch.position.x, touch.position.y,
                            touchobjects[touch.fingerId].screenPoint.z);
                        Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint) - touchobjects[touch.fingerId].offset;
                        recipient.transform.position = curPosition; 
    
                    }
                    if (touch.phase == TouchPhase.Canceled){
    
                    }
                }
    
    
            }
            foreach (GameObject g in touchesOld){
              if (!touchList.Contains(g)){
                    g.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
            }
    
        }
        }
    
    }
    

    }