2012-03-22 11 views
4

これは私の問題です:私はプレーヤークラスとSwipeDetectorクラスをC#で持っていますが、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

+0

空のオブジェクト@farooqaaとplayer.csを私のcharacterControllerに返します。 :))) – MidnightCoder

答えて

2

あなたSwipeDetectorプレーヤークラスからアクセスしたい場合、あなたは、単にパブリック変数を使用することができます。

// Player.cs 
public SwipeDetector SwipeDetector; 

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

パブリック変数を1に設定したくない場合は、一種のシングルトンパターンを使用できます。

// 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私はpublic varibaleを使用しても構いませんが、SwipeDirectionは現在のコンテキストに存在しません:)) – MidnightCoder

+0

@MidnightCoderあなたのクラスの外にenumを置くか、SwipeDetector.SwipeDirectionを使う必要があります。名前の競合が発生する可能性があるのでここで注意してください。 –

+0

シングルトンを使用すると、シングルトンの前にシンクにアクセスしようとしているgameObjectが作成されると問題が発生します。スクリプトオーダーを編集し、それを使用するスクリプトの前にSwipeDetectorスクリプトが実行されていることを確認します。 – farooq

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

あなたの答えに感謝します、それは理にかなって、私はちょうどやったと私はそれが現在のコンテキストで存在していることを言っている間違いを持っ​​ている。 – MidnightCoder

+0

SwipeDetector.SwipeDirectionを使うか、 "public enum SwipeDirection {None ......"コードをクラス外に置いてください。 – farooq

関連する問題