Advertisement
udemyuser

modify

Dec 21st, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | Source Code | 0 0
  1. /* Written by Kaz Crowe */
  2. /* UltimateTouchpad.cs */
  3. // [Previous imports remain the same...]
  4.  
  5. [ExecuteInEditTime]
  6. #if ENABLE_INPUT_SYSTEM
  7. public class UltimateTouchpad : OnScreenControl, IPointerDownHandler, IDragHandler, IPointerUpHandler
  8. #else
  9. public class UltimateTouchpad : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
  10. #endif
  11. {
  12.     // [Previous code remains the same until Start method...]
  13.  
  14.     void Start ()
  15.     {
  16.         // If the game is not running then return.
  17.         if( !Application.isPlaying )
  18.             return;
  19.  
  20.         // Configure the sensitivities.
  21.         _xSensitivity = xSensitivity * screenWidthReference;
  22.         _ySensitivity = ySensitivity * screenWidthReference;
  23.  
  24.         // If the parent canvas is null...
  25.         if( ParentCanvas == null )
  26.         {
  27.             // Then try to get the parent canvas component.
  28.             UpdateParentCanvas();
  29.  
  30.             // If it is still null, then log a error and return.
  31.             if( ParentCanvas == null )
  32.             {
  33.                 Debug.LogError( "Ultimate Touchpad\nThis component is not with a Canvas object. Disabling this component to avoid errors." );
  34.                 enabled = false;
  35.                 return;
  36.             }
  37.         }
  38.  
  39.         // If the parent canvas does not have a screen size updater, then add it.
  40.         if( !ParentCanvas.GetComponent<UltimateTouchpadScreenSizeUpdater>() )
  41.             ParentCanvas.gameObject.AddComponent<UltimateTouchpadScreenSizeUpdater>();
  42.  
  43.         // Update the size and placement of the touchpad.
  44.         UpdateTouchpadPositioning();
  45.  
  46.         // Modified: If input image exists, make it always visible with the input color
  47.         if( displayInput && inputImage != null )
  48.         {
  49.             inputImage.color = inputColor;
  50.         }
  51.  
  52.         // [Rest of the Start method remains the same...]
  53.     }
  54.  
  55.     public void OnPointerDown ( PointerEventData touchInfo )
  56.     {
  57.         // [Previous code remains the same...]
  58.  
  59.         // Modified: Removed the input image visibility change since we want it always visible
  60.        
  61.         // [Rest of the method remains the same...]
  62.     }
  63.  
  64.     public void OnPointerUp ( PointerEventData touchInfo )
  65.     {
  66.         // If the input is not active, then return.
  67.         if( !inputActive )
  68.             return;
  69.  
  70.         // Reset state and _pointerId to default.
  71.         inputActive = false;
  72.  
  73.         // Reset the horizontal and vertical values.
  74.         horizontalValue = 0.0f;
  75.         verticalValue = 0.0f;
  76.  
  77.         #if ENABLE_INPUT_SYSTEM
  78.         SendValueToControl( new Vector2( horizontalValue, verticalValue ) );
  79.         #endif
  80.  
  81.         // Modified: Instead of hiding the input image, return it to its default position
  82.         if( displayInput && inputImage != null )
  83.         {
  84.             inputImage.rectTransform.localPosition = Vector3.zero;
  85.         }
  86.  
  87.         // [Rest of the method remains the same...]
  88.     }
  89.  
  90.     void UpdateTouchPad ()
  91.     {
  92.         // [Previous code remains the same until input image handling...]
  93.  
  94.         // Modified: Update input image position while dragging, but keep it visible
  95.         if( displayInput && inputImage != null )
  96.         {
  97.             Vector2 newPosition = ( Vector2 )baseTrans.InverseTransformPoint( ParentCanvas.transform.TransformPoint( currentPosition / ParentCanvas.scaleFactor ) ) - ( canvasRectTrans.sizeDelta / 2 );
  98.             inputImage.rectTransform.localPosition = newPosition;
  99.         }
  100.     }
  101.  
  102.     void ResetTouchpad ()
  103.     {
  104.         // Reset state and _pointerId to default.
  105.         inputActive = false;
  106.  
  107.         // Reset the horizontal and vertical values.
  108.         horizontalValue = 0.0f;
  109.         verticalValue = 0.0f;
  110.  
  111.         #if ENABLE_INPUT_SYSTEM
  112.         SendValueToControl( new Vector2( horizontalValue, verticalValue ) );
  113.         #endif
  114.  
  115.         // Modified: Instead of hiding the input image, return it to its default position
  116.         if( displayInput && inputImage != null )
  117.         {
  118.             inputImage.rectTransform.localPosition = Vector3.zero;
  119.         }
  120.  
  121.         // [Rest of the method remains the same...]
  122.     }
  123.  
  124.     // [Rest of the class remains the same...]
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement