简体   繁体   English

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

[英]How to access another class using C#(in unity3d)?

This is my problem: I have a player class and SwipeDetector Class in c#, the SwipeDetector class helps to do recognized swipe touches vertically on the iPhone. 这是我的问题:我在c#中有一个播放器类和SwipeDetector类,SwipeDetector类有助于在iPhone上垂直进行识别的滑动​​触摸。

ps I'm using unity3d but this is a programming question oppose to gaming techniques :)) ps我正在使用unity3d,但这是一个反对游戏技术的编程问题:))

In my player class I'm trying to access the SwipeDetector and find out which is swipe it was (up, down). 在我的播放器类中,我正在尝试访问SwipeDetector并找出哪个是滑动(向上,向下)。

player.cs: player.cs:

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

this is the SwipeDetector class, it looks scary but its not!! 这是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;
                }
            }
        }
    }

If you want to access your SwipeDetector from the player class, you simply can use a public variable. 如果要从播放器类访问SwipeDetector ,只需使用公共变量即可。

// Player.cs
public SwipeDetector MySwipeDetector;

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

If you don't want to set the public variable in unity, you can use a kind of 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;
}

And use it this way: 并以这种方式使用它:

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

Add a public variable to your Player class. 将一个公共变量添加到您的Player类。

// player.cs
public SwipeDetector swipeDetector;

You will now see the SwipeDetector variable in the editor when you click on the Player GameObject. 现在,当您单击Player GameObject时,您将在编辑器中看到SwipeDetector变量。 Assign that empty gameObject that has SwipeDetector (in the Editor) to it. 将具有SwipeDetector(在编辑器中)的空游戏对象分配给它。 And now you have access to that class. 现在您可以访问该课程。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM