如何使用C#(在Unity3D中)访问另一个类?

4
这是我的问题:我有一个播放器类和 SwipeDetector 类在 C# 中,SwipeDetector 类帮助识别 iPhone 上垂直滑动触摸。

p.s. 我正在使用 Unity3d,但这是一个编程问题而不是游戏技巧 :))

在我的 player 类中,我试图访问 SwipeDetector 并找出它是哪个方向的滑动(上、下)。

player.cs:

if(SwipeDetetcor is up){
   print("up");
}

这是SwipeDetector类,看起来很吓人,但实际上并不可怕!!

    using UnityEngine;
    using System.Collections;

    public class SwipeDetector : MonoBehaviour {

        // Values to set:
        public float comfortZone = 70.0f;
        public float minSwipeDist = 14.0f;
        public float maxSwipeTime = 0.5f;

        private float startTime;
        private Vector2 startPos;
        private bool couldBeSwipe;

        public enum SwipeDirection {
            None,
            Up,
            Down
        }

        public SwipeDirection lastSwipe = SwipeDetector.SwipeDirection.None;
        public float lastSwipeTime;

        void  Update()
        {
            if (Input.touchCount > 0)
            {
                Touch touch = Input.touches[0];

                switch (touch.phase)
                {
                    case TouchPhase.Began:
                        lastSwipe = SwipeDetector.SwipeDirection.None;
                                            lastSwipeTime = 0;
                        couldBeSwipe = true;
                        startPos = touch.position;
                        startTime = Time.time;
                        break;

                    case TouchPhase.Moved:
                        if (Mathf.Abs(touch.position.x - startPos.x) > comfortZone)
                        {
                            Debug.Log("Not a swipe. Swipe strayed " + (int)Mathf.Abs(touch.position.x - startPos.x) +
                                      "px which is " + (int)(Mathf.Abs(touch.position.x - startPos.x) - comfortZone) +
                                      "px outside the comfort zone.");
                            couldBeSwipe = false;
                        }
                        break;
                    case TouchPhase.Ended:
                        if (couldBeSwipe)
                        {
                            float swipeTime = Time.time - startTime;
                            float swipeDist = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;

                            if ((swipeTime < maxSwipeTime) && (swipeDist > minSwipeDist))
                            {
                                // It's a swiiiiiiiiiiiipe!
                                float swipeValue = Mathf.Sign(touch.position.y - startPos.y);

                                // If the swipe direction is positive, it was an upward swipe.
                                // If the swipe direction is negative, it was a downward swipe.
                                if (swipeValue > 0){
                                    lastSwipe = SwipeDetector.SwipeDirection.Up;
                                    print("UPUPUP");
                                }
                                else if (swipeValue < 0)
                                    lastSwipe = SwipeDetector.SwipeDirection.Down;

                                // Set the time the last swipe occured, useful for other scripts to check:
                                lastSwipeTime = Time.time;
                                Debug.Log("Found a swipe!  Direction: " + lastSwipe);
                            }
                        }
                        break;
                }
            }
        }
    }

你将SwipeDetector分配给了哪个GameObject? - farooq
将一个空对象@farooqaa和player.cs添加到我的characterController中。:))) - MidnightCoder
2个回答

3
如果你想从播放器类中访问你的 SwipeDetector,你可以简单地使用一个公共变量。
// Player.cs
public SwipeDetector MySwipeDetector;

void Update() 
{
    if (MySwipeDetector.lastSwipe == SwipeDirection.Up) { .... }
}

如果您不想在Unity中设置公共变量,可以使用一种称为单例模式的方法。

// SwipeDetector.cs
private static SwipeDetector _Instance;

public static SwipeDetector Instance { get { return _Instance; } }

void Awake()
{
    if (_Instance!= null) throw new Exception(...);
    _Instance= this;
}

并且这样使用:

// Player.cs
void Update()
{
    if (SwipeDetector.Instance.lastSwipe == SwipeDirection.Up) { .... }
}

如果尝试访问单例的gameObject在单例之前创建,则会导致问题。您需要编辑脚本执行顺序,并确保SwipeDetector脚本在使用它的脚本之前执行。 - farooq
@farooqaaa 单例实例将在任何更新发生之前设置,因此如果存在任何SwipeDetector,则实例不可能为空。我在Unity中使用它们很多次。 - Felix K.
@MidnightCoder 但我们已经回答了你的问题!;-) 如果你有另一个问题,你可以在stackoverflow上开一个新的问题。 - Felix K.
你应该查看:answers.unity3d.com,以获取有关Unity的问题。 - farooq
@derHugo 但在C#中这是完全合法的 :-) - Felix K.
显示剩余5条评论

1
在你的Player类中添加一个公共变量。
// player.cs
public SwipeDetector swipeDetector;

当您单击Player GameObject时,编辑器中将出现SwipeDetector变量。将具有SwipeDetector(在编辑器中)的空gameObject分配给它。现在您可以访问该类。

// you can now use it like this:
if(swipeDetector.lastSwipe == SwipeDetector.SwipeDirection.UP)
{
    // do something
}

谢谢你的回答,我明白了。我刚刚尝试了一下,但是出现了一个错误,它说SwipeDirection在当前上下文中不存在。 - MidnightCoder
使用SwipeDetector.SwipeDirection或将“public enum SwipeDirection { None......”代码放在类外部。 - farooq

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接