2012-03-22 46 views
4

這是我的問題:我在c#中有一個播放器類和SwipeDetector類,SwipeDetector類有助於在iPhone上垂直識別滑動觸摸。如何使用C#訪問另一個類(在unity3d中)?

p.s.我使用的是unity3d,但這是一個反對遊戲技巧的編程問題:))

在我的播放器類中,我試圖訪問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; 
       } 
      } 
     } 
    } 
+0

哪個遊戲對象你分配給SwipeDetector? – farooq 2012-03-22 12:22:51

+0

一個空的對象@farooqaa和player.cs到我的characterController。 :))) – MidnightCoder 2012-03-22 12:25:29

回答

2

如果你想你的SwipeDetector從播放器類訪問,你根本可以使用公共變量。

// Player.cs 
public SwipeDetector SwipeDetector; 

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

如果你不想統一設置公共變量,你可以使用一種singletone模式。

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

感謝felix我不介意使用公共varibale,但它的說SwipeDirection不存在於當前的上下文:)) – MidnightCoder 2012-03-22 12:36:58

+0

@MidnightCoder您需要將您的枚舉放在您的類之外或使用SwipeDetector.SwipeDirection 。在這裏要小心,因爲你可能會遇到一些名稱衝突,所以最好把enum放在外面。 – 2012-03-22 12:38:46

+0

如果嘗試訪問它的gameObject在單例之前創建,則使用單例會導致問題。您將編輯腳本順序並確保在使用它的腳本之前執行SwipeDetector腳本。 – farooq 2012-03-22 12:41:47

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 
} 
+0

感謝您的回答,它的deos有意義,我只是做了它,我得到了一個錯誤,它說SwipeDirection deosnt存在於當前的上下文中。 – MidnightCoder 2012-03-22 12:35:14

+0

使用SwipeDetector.SwipeDirection或在類的外部放置「public enum SwipeDirection {None ......」代碼。 – farooq 2012-03-22 12:38:24