Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有办法将触摸输入限制到Unity3D中的面板?_C#_Unity3d - Fatal编程技术网

C# 有没有办法将触摸输入限制到Unity3D中的面板?

C# 有没有办法将触摸输入限制到Unity3D中的面板?,c#,unity3d,C#,Unity3d,我正在尝试在我的游戏中实现刷卡控制,希望只在屏幕左侧检测到刷卡,并保持游戏的其他控制机制不变 我正在使用此SwipManager: public enum Swipes { None, Up, Down, Left, TopLeft, BottomLeft, Right, TopRight, BottomRight}; public class SwipeManager : MonoBehaviour { public float minSwipeLength = 200f;

我正在尝试在我的游戏中实现刷卡控制,希望只在屏幕左侧检测到刷卡,并保持游戏的其他控制机制不变

我正在使用此
SwipManager

public enum Swipes { None, Up, Down, Left, TopLeft, BottomLeft, Right, TopRight,  BottomRight};

public class SwipeManager : MonoBehaviour
{
    public float minSwipeLength = 200f;

    private Vector2 fingerStart;
    private Vector2 fingerEnd;

    public static Swipes direction;
    public static float angle;
    public static Vector2 strength;

    public static bool debugMode = false;

    void Update ()
    {
        SwipeDetection();
    }

    public void SwipeDetection ()
    {
        if (Input.GetMouseButtonDown(0)) {
            fingerStart = Input.mousePosition;
            fingerEnd  = Input.mousePosition;
        }

        if(Input.GetMouseButton(0)) {
            fingerEnd = Input.mousePosition;

            strength = new Vector2 (fingerEnd.x - fingerStart.x, fingerEnd.y - fingerStart.y);

            // Make sure it was a legit swipe, not a tap
            if (strength.magnitude < minSwipeLength) {
                direction = Swipes.None;
                return;
            }

            angle = (Mathf.Atan2(strength.y, strength.x) / (Mathf.PI));
            if (debugMode) Debug.Log(angle);
            // Swipe up
            if (angle>0.375f && angle<0.625f) {
                direction = Swipes.Up;
                if (debugMode) Debug.Log ("Up");
                // Swipe down
            } else if (angle<-0.375f && angle>-0.625f) {
                direction = Swipes.Down;
                if (debugMode) Debug.Log ("Down");
                // Swipe left
            } else if (angle<-0.875f || angle>0.875f) {
                direction = Swipes.Left;
                if (debugMode) Debug.Log ("Left");
                // Swipe right
            } else if (angle>-0.125f && angle<0.125f) {
                direction = Swipes.Right;
                if (debugMode) Debug.Log ("Right");
            }
            else if(angle>0.125f && angle<0.375f){
                direction = Swipes.TopRight;
                if (debugMode) Debug.Log ("top right");
            }
            else if(angle>0.625f && angle<0.875f){
                direction = Swipes.TopLeft;
                if (debugMode) Debug.Log ("top left");
            }
            else if(angle<-0.125f && angle>-0.375f){
                direction = Swipes.BottomRight;
                if (debugMode) Debug.Log ("bottom right");
            }
            else if(angle<-0.625f && angle>-0.875f){
                direction = Swipes.BottomLeft;
                if (debugMode) Debug.Log ("bottom left");
            }
        }

        if(Input.GetMouseButtonUp(0)) {
            direction = Swipes.None;  
        }
    }
}
公共枚举滑动{None、Up、Down、Left、TopLeft、BottomLeft、Right、toplright、BottomRight};
公共类SwipManager:MonoBehavior
{
公共浮子长度=200f;
专用矢量2 fingerStart;
专用矢量2指端;
公共静态滑动方向;
公共静态浮动角;
公共静态向量2强度;
公共静态bool debugMode=false;
无效更新()
{
SwipeDetection();
}
公共无效SwipedDetection()
{
if(Input.GetMouseButtonDown(0)){
fingerStart=Input.mousePosition;
fingerEnd=Input.mousePosition;
}
if(输入。GetMouseButton(0)){
fingerEnd=Input.mousePosition;
强度=新矢量2(fingerEnd.x-fingerStart.x,fingerEnd.y-fingerStart.y);
//确保这是合法的一击,而不是水龙头
if(强度、幅值<长度){
方向=滑动。无;
返回;
}
角度=(数学Atan2(强度y,强度x)/(数学PI));
if(debugMode)Debug.Log(角度);
//刷卡

如果(angle>0.375f&&angle-0.125f&&angle0.125f&&angle0.625f&&angle当鼠标指针位于面板外部时,一种解决方案是跳过
Swipedtection
。因此,如果您可以获得对面板的
RectTransform
的引用,那么您可以在调用
Swipedtection

为了考虑用户从面板中压出然后进入面板的可能性,您可以在鼠标位于矩形外时指定
fingerStart=Input.mousePosition;

总之,这可能看起来像:

public RectTransform rectTransform; // panel RectTransform assigned to this variable

...

void Update ()
{
    Vector2 mousePosition = Input.mousePosition;

    if (RectTransformUtility.RectangleContainsScreenPoint(rectTransform, mousePosition))
    {
        SwipeDetection();
    }
    else 
    {
        fingerStart = Input.mousePosition;
        direction = Swipes.None;
    }
}

您好,谢谢您的回答。我按照建议做了,编辑了
swipmanager.cs
脚本以包含rect转换,并将我的面板拖到inspector中。然后我包装了
swipedtection()
在您提供的if块中运行。但它似乎没有跟随它,屏幕上的任何地方仍能检测到滑动。@StuckInPhD尝试编辑。我之前使用的代码假设面板的父面板覆盖了整个屏幕。这非常有效。谢谢。我将接受答案,因为我的初始问题已经解决。我正在使用这些改变攻击车道的侧击,有4条车道,我面临的另一个问题是,单次侧击不被认为是单次侧击,而是多条车道在单次侧击中被改变和循环。有没有办法将单次侧击包含在一次动作中?而不是连续的一系列侧击Thanks@StuckInPhD我想加上co在车道变换器中设置一个标志,如
bool-alreadySwipe=false;
。当检测到刷卡时,请选中
alreadySwipe
。如果
alreadySwipe
true
,则不执行任何操作。如果
false
,则执行车道更改并设置
alreadySwipe=true;
。无论何时
swipmanager.direction==刷卡.None
put
alreadySwipe=false;
再次。还请注意,我在我的答案中添加了
direction=Swipes.None;
。感谢您的解释。这很有意义,我会将其添加到我的代码中。但我不明白您的最后一行,您说过我应该注意您在答案中添加了
direction=Swipes.None;
。但我看不出来谢谢你的帮助:)也许可以继续问