How to access another class using C # (in unity 3d)?

This is my problem: I have a player class and a SwipeDetector class in C #, the SwipeDetector class helps to recognize vertical strikes on the iPhone.

ps I use unit3d, but this is a programming issue that opposes game methods :))

In my player class, I'm trying to access the SwipeDetector and find out what kind of swipe it is (up, down).

player.cs:

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

this is the SwipeDetector class, it looks scary, but it's not there!

  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 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; } } } } 
+4
source share
2 answers

If you want to access your SwipeDetector from the player class, you can simply use the public variable.

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

If you do not want the public variable to be unique, you can use a kind of single template.

 // 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 like this:

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

Add a public variable to your Player class.

 // player.cs public SwipeDetector swipeDetector; 

You will now see the SwipeDetector variable in the editor when you click on Player GameObject. Assign an empty gameObject with the SwipeDetector (in the editor). And now you have access to this class.

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

Source: https://habr.com/ru/post/1402875/


All Articles