Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[HELP Updating broken code]Help me update this joystick code to avoid using depreciated GUITexture.

Discussion in 'General Graphics' started by Mr-Mechanical, Apr 16, 2020.

  1. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [RequireComponent(typeof(GUITexture))]
    4. public class Joystick : MonoBehaviour
    5. {
    6.     // A simple class for bounding how far the GUITexture will move
    7.     public class Boundary
    8.     {
    9.         public Vector2 min = Vector2.zero;
    10.         public Vector2 max = Vector2.zero;
    11.     }
    12.     private static Joystick[] joysticks;                    // A static collection of all joysticks
    13.     private static bool enumeratedJoysticks = false;
    14.     private static float tapTimeDelta = 0.3f;                // Time allowed between taps
    15.  
    16.     public bool touchPad;                                     // Is this a TouchPad?
    17.     public Rect touchZone;
    18.     public Vector2 deadZone = Vector2.zero;                    // Control when position is output
    19.     public bool normalize = false;                             // Normalize output after the dead-zone?
    20.     public Vector2 position;                                // [-1, 1] in x,y
    21.     public int tapCount;                                    // Current tap count
    22.  
    23.     private int lastFingerId = -1;                            // Finger last used for this joystick
    24.     private float tapTimeWindow;                            // How much time there is left for a tap to occur
    25.     private Vector2 fingerDownPos;
    26.  
    27.     private GUITexture gui;                                    // Joystick graphic
    28.     private Rect defaultRect;                                // Default position / extents of the joystick graphic
    29.     private Boundary guiBoundary = new Boundary();            // Boundary for joystick graphic
    30.     private Vector2 guiTouchOffset;                            // Offset to apply to touch input
    31.     private Vector2 guiCenter;                                // Center of joystick
    32.  
    33.     private void Start()
    34.     {
    35.         // Cache this component at startup instead of looking up every frame
    36.         gui = GetComponent<GUITexture>();
    37.      
    38.         // Store the default rect for the gui, so we can snap back to it
    39.         defaultRect = gui.pixelInset;
    40.      
    41.         defaultRect.x += transform.position.x * Screen.width;// + gui.pixelInset.x; // -  Screen.width * 0.5;
    42.         defaultRect.y += transform.position.y * Screen.height;// - Screen.height * 0.5;
    43.      
    44.         transform.position = new Vector3(0f, 0f, transform.position.z);
    45.      
    46.         if (touchPad)
    47.         {
    48.             // If a texture has been assigned, then use the rect ferom the gui as our touchZone
    49.             if (gui.texture != null)
    50.                 touchZone = defaultRect;
    51.         }
    52.         else
    53.         {            
    54.             // This is an offset for touch input to match with the top left
    55.             // corner of the GUI
    56.             guiTouchOffset.x = defaultRect.width * 0.5f;
    57.             guiTouchOffset.y = defaultRect.height * 0.5f;
    58.          
    59.             // Cache the center of the GUI, since it doesn't change
    60.             guiCenter.x = defaultRect.x + guiTouchOffset.x;
    61.             guiCenter.y = defaultRect.y + guiTouchOffset.y;
    62.          
    63.             // Let's build the GUI boundary, so we can clamp joystick movement
    64.             guiBoundary.min.x = defaultRect.x - guiTouchOffset.x;
    65.             guiBoundary.max.x = defaultRect.x + guiTouchOffset.x;
    66.             guiBoundary.min.y = defaultRect.y - guiTouchOffset.y;
    67.             guiBoundary.max.y = defaultRect.y + guiTouchOffset.y;
    68.         }
    69.     }
    70.  
    71.     private void Update()
    72.     {
    73.         if ( !enumeratedJoysticks )
    74.         {
    75.             // Collect all joysticks in the game, so we can relay finger latching messages
    76.             joysticks = FindObjectsOfType<Joystick>();
    77.             enumeratedJoysticks = true;
    78.         }
    79.      
    80.         int count = Input.touchCount;
    81.      
    82.         // Adjust the tap time window while it still available
    83.         if ( tapTimeWindow > 0 )
    84.             tapTimeWindow -= Time.deltaTime;
    85.         else
    86.             tapCount = 0;
    87.      
    88.         if ( count == 0 )
    89.             ResetJoystick();
    90.         else
    91.         {
    92.             for(int i = 0; i < count; i++)
    93.             {
    94.                 Touch touch = Input.GetTouch(i);        
    95.                 Vector2 guiTouchPos = touch.position - guiTouchOffset;
    96.              
    97.                 bool shouldLatchFinger = false;
    98.                 if ( touchPad )
    99.                 {            
    100.                     if ( touchZone.Contains( touch.position ) )
    101.                         shouldLatchFinger = true;
    102.                 }
    103.                 else if ( gui.HitTest( touch.position ) )
    104.                 {
    105.                     shouldLatchFinger = true;
    106.                 }    
    107.              
    108.                 // Latch the finger if this is a new touch
    109.                 if ( shouldLatchFinger && ( lastFingerId == -1 || lastFingerId != touch.fingerId ) )
    110.                 {
    111.                  
    112.                     if ( touchPad )
    113.                     {
    114.                         gui.color = new Color(gui.color.r, gui.color.g, gui.color.b, 0.15f);
    115.                      
    116.                         lastFingerId = touch.fingerId;
    117.                         fingerDownPos = touch.position;
    118.                     }
    119.                  
    120.                     lastFingerId = touch.fingerId;
    121.                  
    122.                     // Accumulate taps if it is within the time window
    123.                     if ( tapTimeWindow > 0 )
    124.                         tapCount++;
    125.                     else
    126.                     {
    127.                         tapCount = 1;
    128.                         tapTimeWindow = tapTimeDelta;
    129.                     }
    130.                  
    131.                     // Tell other joysticks we've latched this finger
    132.                     foreach (Joystick j in joysticks)
    133.                     {
    134.                         if ( j != this )
    135.                             j.LatchedFinger( touch.fingerId );
    136.                     }                    
    137.                 }            
    138.              
    139.                 if ( lastFingerId == touch.fingerId )
    140.                 {
    141.                     // Override the tap count with what the iPhone SDK reports if it is greater
    142.                     // This is a workaround, since the iPhone SDK does not currently track taps
    143.                     // for multiple touches
    144.                     if ( touch.tapCount > tapCount )
    145.                         tapCount = touch.tapCount;
    146.                  
    147.                     if ( touchPad )
    148.                     {
    149.                         // For a touchpad, let's just set the position directly based on distance from initial touchdown
    150.                         position.x = Mathf.Clamp( ( touch.position.x - fingerDownPos.x ) / ( touchZone.width / 2 ), -1, 1 );
    151.                         position.y = Mathf.Clamp( ( touch.position.y - fingerDownPos.y ) / ( touchZone.height / 2 ), -1, 1 );
    152.                     }
    153.                     else
    154.                     {                
    155.                         // Change the location of the joystick graphic to match where the touch is
    156.                         gui.pixelInset = new Rect(
    157.                             Mathf.Clamp(guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x),
    158.                             Mathf.Clamp(guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y),
    159.                             gui.pixelInset.width,
    160.                             gui.pixelInset.height);    
    161.                     }
    162.                  
    163.                     if ( touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled )
    164.                         ResetJoystick();                
    165.                 }        
    166.             }
    167.         }
    168.      
    169.         if ( !touchPad )
    170.         {
    171.             // Get a value between -1 and 1 based on the joystick graphic location
    172.             position.x = ( gui.pixelInset.x + guiTouchOffset.x - guiCenter.x ) / guiTouchOffset.x;
    173.             position.y = ( gui.pixelInset.y + guiTouchOffset.y - guiCenter.y ) / guiTouchOffset.y;
    174.         }
    175.      
    176.         // Adjust for dead zone
    177.         var absoluteX = Mathf.Abs( position.x );
    178.         var absoluteY = Mathf.Abs( position.y );
    179.      
    180.         if ( absoluteX < deadZone.x )
    181.         {
    182.             // Report the joystick as being at the center if it is within the dead zone
    183.             position.x = 0;
    184.         }
    185.         else if ( normalize )
    186.         {
    187.             // Rescale the output after taking the dead zone into account
    188.             position.x = Mathf.Sign( position.x ) * ( absoluteX - deadZone.x ) / ( 1 - deadZone.x );
    189.         }
    190.      
    191.         if ( absoluteY < deadZone.y )
    192.         {
    193.             // Report the joystick as being at the center if it is within the dead zone
    194.             position.y = 0;
    195.         }
    196.         else if ( normalize )
    197.         {
    198.             // Rescale the output after taking the dead zone into account
    199.             position.y = Mathf.Sign( position.y ) * ( absoluteY - deadZone.y ) / ( 1 - deadZone.y );
    200.         }
    201.     }
    202.     public void ResetJoystick()
    203.     {
    204.         // Release the finger control and set the joystick back to the default position
    205.         gui.pixelInset = defaultRect;
    206.         lastFingerId = -1;
    207.         position = Vector2.zero;
    208.         fingerDownPos = Vector2.zero;
    209.      
    210.         if (touchPad)
    211.             gui.color = new Color(gui.color.r, gui.color.g, gui.color.b, 0.025f);
    212.     }
    213.     public void LatchedFinger(int fingerId)
    214.     {
    215.         // If another joystick has latched this finger, then we must release it
    216.         if (lastFingerId == fingerId)
    217.             ResetJoystick();
    218.     }
    219.     public void Disable()
    220.     {
    221.         gameObject.SetActive(false);
    222.         enumeratedJoysticks = false;
    223.     }
    224.     public bool IsFingerDown()
    225.     {
    226.         return (lastFingerId != -1);
    227.     }
    228. }
     
  2. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    I need to use UI.Image instead GUITexture, as UI.Image is no longer supported in the version of Unity I am using to develop my project. I am not familiar with UI.Image so I need help.
     
  3. Neteraxe

    Neteraxe

    Joined:
    Jun 30, 2020
    Posts:
    1
    same issue. It's exactly no information .