在Unity3D中如何使用C#读取其它class?

我用C#编写了一个player class和SwipeDetector class,SwipeDetector class用来感应iPhone上的垂直触摸。我用的是unity3d,但是这个问题属于编程问题,而不单单是游戏开发方面的技巧问题。
在player class中,我想读取SwipeDetector中的数据,了解它可以感应哪些触摸活动,该怎么办呢?
player.cs:

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

这就是SwipeDetector class

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;
                }
            }
        }
    }

原问题:How to access another class using C#(in unity3d)?

阅读 5.7k
1 个回答

答:Felix K.
如果你想从play class中读取SwipeDetector,只需要使用一个公共变量即可。

// Player.cs
public SwipeDetector SwipeDetector;

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

如果你不想设定公共变量,可以设定单件模式(singletone pattern):

// 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) { .... }
}

答:farooqaaa
你可以在player class中添加一个公共变量

// player.cs
public SwipeDetector swipeDetector;

现在,在编辑器中点击Player GameObject,可以看到SwipeDetector变量。为gameObject赋予一定的值,这样你就可以读取想要的class了。

// you can now use it like this:
if(swipeDetector.lastSwipe == SwipeDetector.SwipeDirection.UP)
{
    // do something
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
Stack Overflow 翻译
子站问答
访问
宣传栏