Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Written by Kaz Crowe */
- /* UltimateTouchpad.cs */
- // [Previous imports remain the same...]
- [ExecuteInEditTime]
- #if ENABLE_INPUT_SYSTEM
- public class UltimateTouchpad : OnScreenControl, IPointerDownHandler, IDragHandler, IPointerUpHandler
- #else
- public class UltimateTouchpad : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
- #endif
- {
- // [Previous code remains the same until Start method...]
- void Start ()
- {
- // If the game is not running then return.
- if( !Application.isPlaying )
- return;
- // Configure the sensitivities.
- _xSensitivity = xSensitivity * screenWidthReference;
- _ySensitivity = ySensitivity * screenWidthReference;
- // If the parent canvas is null...
- if( ParentCanvas == null )
- {
- // Then try to get the parent canvas component.
- UpdateParentCanvas();
- // If it is still null, then log a error and return.
- if( ParentCanvas == null )
- {
- Debug.LogError( "Ultimate Touchpad\nThis component is not with a Canvas object. Disabling this component to avoid errors." );
- enabled = false;
- return;
- }
- }
- // If the parent canvas does not have a screen size updater, then add it.
- if( !ParentCanvas.GetComponent<UltimateTouchpadScreenSizeUpdater>() )
- ParentCanvas.gameObject.AddComponent<UltimateTouchpadScreenSizeUpdater>();
- // Update the size and placement of the touchpad.
- UpdateTouchpadPositioning();
- // Modified: If input image exists, make it always visible with the input color
- if( displayInput && inputImage != null )
- {
- inputImage.color = inputColor;
- }
- // [Rest of the Start method remains the same...]
- }
- public void OnPointerDown ( PointerEventData touchInfo )
- {
- // [Previous code remains the same...]
- // Modified: Removed the input image visibility change since we want it always visible
- // [Rest of the method remains the same...]
- }
- public void OnPointerUp ( PointerEventData touchInfo )
- {
- // If the input is not active, then return.
- if( !inputActive )
- return;
- // Reset state and _pointerId to default.
- inputActive = false;
- // Reset the horizontal and vertical values.
- horizontalValue = 0.0f;
- verticalValue = 0.0f;
- #if ENABLE_INPUT_SYSTEM
- SendValueToControl( new Vector2( horizontalValue, verticalValue ) );
- #endif
- // Modified: Instead of hiding the input image, return it to its default position
- if( displayInput && inputImage != null )
- {
- inputImage.rectTransform.localPosition = Vector3.zero;
- }
- // [Rest of the method remains the same...]
- }
- void UpdateTouchPad ()
- {
- // [Previous code remains the same until input image handling...]
- // Modified: Update input image position while dragging, but keep it visible
- if( displayInput && inputImage != null )
- {
- Vector2 newPosition = ( Vector2 )baseTrans.InverseTransformPoint( ParentCanvas.transform.TransformPoint( currentPosition / ParentCanvas.scaleFactor ) ) - ( canvasRectTrans.sizeDelta / 2 );
- inputImage.rectTransform.localPosition = newPosition;
- }
- }
- void ResetTouchpad ()
- {
- // Reset state and _pointerId to default.
- inputActive = false;
- // Reset the horizontal and vertical values.
- horizontalValue = 0.0f;
- verticalValue = 0.0f;
- #if ENABLE_INPUT_SYSTEM
- SendValueToControl( new Vector2( horizontalValue, verticalValue ) );
- #endif
- // Modified: Instead of hiding the input image, return it to its default position
- if( displayInput && inputImage != null )
- {
- inputImage.rectTransform.localPosition = Vector3.zero;
- }
- // [Rest of the method remains the same...]
- }
- // [Rest of the class remains the same...]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement