Search Unity

MultiTouch: Joysticks and Buttons (with Event System)

Discussion in 'Community Learning & Teaching' started by IsGreen, Jul 5, 2015.

  1. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    VIDEO: http://dai.ly/x2wq3mu

    Download link.


    To apply on screen several Joysticks and buttons, first i created one base class called MultiTouch:

    This MultiTouch class, detects touchs on screen 1 time per frame.

    After, I created two derived class of this, one for Joysticks (MultiTouchJoystick) and one for the buttons (MultiTouchButton), including a system of events that invokes a specific method:

    MultiTouchJoystick envent methods: JoystickNone, JoystickEnter, JoystickStay, JoystickExit.
    MultiTouchButton event methods: TouchNone, TouchEnter, TouchStay, TouchExit.

    Event System incorporates 4 states types: None, Enter, Stay, and Exit.

    ____________________________________________________________________


    HOW TO USE

    To use these two types of controls (Joystick and Button) must create derived class of MultiTouchJoystick or MultiTouchButton.

    For example, if you want two buttons A and B, you create two derived class called ButtonA and ButtonB of MultiTouchButton.

    Then you can use the event system (None, Enter, Stay, Exit) adding methods: TouchNone, TouchEnter, TouchStay and TouchExit.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ButtonA : MultiTouchButton {
    5.  
    6.     bool show = false;
    7.  
    8.  
    9.     void TouchEnter(){
    10.  
    11.         Debug.Log("EnterButtonA");
    12.     }
    13.  
    14.     void TouchStay(){
    15.  
    16.         show = true;
    17.  
    18.     }
    19.  
    20.     void TouchExit(){
    21.  
    22.         show = false;
    23.         Debug.Log("ExitButtonA");
    24.  
    25.     }
    26.  
    27.     void OnGUI(){
    28.  
    29.         if(show) GUILayout.Label("FireButtonA");
    30.  
    31.     }
    32.  
    33. }
    Similarly, if you want to create two Joystick, you create two derived class called Joystick1 and Joystick2 of MultiTouchJoystick.

    Then you can use the event system, adding methods: JoystickNone, JoystickEnter, JoystickStay, JoystickExit.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class JoystickA : MultiTouchJoystick {
    5.  
    6.     public Rect rect;
    7.  
    8.     void JoystickEnter(){
    9.    
    10.         Debug.Log("EnterJoystickA");
    11.     }
    12.  
    13.  
    14.     void JoystickExit(){
    15.    
    16.         Debug.Log("ExitJoystickA");
    17.    
    18.     }
    19.  
    20.     void OnGUI(){
    21.    
    22.         GUI.Label(this.rect, "JoystickA,  X: "+this.Horizontal+"   Y: "+this.Vertical);
    23.    
    24.     }
    25.  
    26. }
    27.  
     
    Last edited: Jul 5, 2015
    theANMATOR2b likes this.