我的头衔很好地解释了我的问题。我将滑动脚本附加到2个预制件上。它们还附带相同的Move脚本,以便它们可以从屏幕的顶部移动到底部。但是当我在屏幕上的任何位置滑动时,滑动脚本会运行。它必须只通过滑动附加到滑动脚本的预制件来工作。此外,当刷卡时,两个预制件都受到滑动(左或右)功能的影响。每个预制件必须受到个人滑动的影响。 任何想法都表示赞赏。 编辑: 当然,我感谢你的兴趣。这是Move脚本
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour {
public float speed = 150f;
private bool swLeft = false;
private bool swRight = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//transform.Translate(Vector3.down * Time.deltaTime, Space.World);
//transform.Translate(0, -Time.deltaTime * speed, 0, Space.World);
if(swLeft == true){
transform.Translate(-Time.deltaTime * speed, 0, 0, Space.World);
}else if(swRight == true){
transform.Translate(Time.deltaTime * speed, 0, 0, Space.World);
}else if(swLeft == false && swRight == false){
transform.Translate(0, -Time.deltaTime * speed, 0, Space.World);
}
}
void OnSwipeLeft()
{
swLeft = true;
}
void OnSwipeRight()
{
swRight = true;
}
}
SwipeHandler:
using UnityEngine;
using System.Collections;
public class SwipeHandler : MonoBehaviour
{
public float minMovement = 20.0f;
public bool sendUpMessage = true;
public bool sendDownMessage = true;
public bool sendLeftMessage = true;
public bool sendRightMessage = true;
public GameObject MessageTarget = null;
private Vector2 StartPos;
private int SwipeID = -1;
void Update ()
{
if (MessageTarget == null)
MessageTarget = gameObject;
foreach (var T in Input.touches)
{
var P = T.position;
if (T.phase == TouchPhase.Began && SwipeID == -1)
{
SwipeID = T.fingerId;
StartPos = P;
}
else if (T.fingerId == SwipeID)
{
var delta = P - StartPos;
if (T.phase == TouchPhase.Moved && delta.magnitude > minMovement)
{
SwipeID = -1;
if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
{
if (sendRightMessage && delta.x > 0)
MessageTarget.SendMessage("OnSwipeRight", SendMessageOptions.DontRequireReceiver);
else if (sendLeftMessage && delta.x < 0)
MessageTarget.SendMessage("OnSwipeLeft", SendMessageOptions.DontRequireReceiver);
}
else
{
if (sendUpMessage && delta.y > 0)
MessageTarget.SendMessage("OnSwipeUp", SendMessageOptions.DontRequireReceiver);
else if (sendDownMessage && delta.y < 0)
MessageTarget.SendMessage("OnSwipeDown", SendMessageOptions.DontRequireReceiver);
}
}
else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
{
SwipeID = -1;
MessageTarget.SendMessage("OnTap", SendMessageOptions.DontRequireReceiver);
}
}
}
}
}