Introduction and use of the StandaloneInputModule component of Unity UGUI
1. What is the StandaloneInputModule component?
StandaloneInputModule is a standard input module component in the Unity UGUI system that handles input events on the mouse and keyboard. It can convert mouse and keyboard input into events in the UGUI system, such as clicks, drags, etc.
2. How the StandaloneInputModule component works
The StandaloneInputModule component listens to input events on the mouse and keyboard and converts them into events in the UGUI system. It determines the current operation object based on the mouse position and keyboard keys and triggers the corresponding event.
3. Common properties of StandaloneInputModule component
-
forceModuleActive
: Whether to force activation of the input module. -
inputActionsPerSecond
: The number of input events processed per second. -
repeatDelay
: The delay time of repeated triggering events. -
horizontalAxis
: The name of the horizontal axis. -
verticalAxis
: The name of the vertical axis. -
submitButton
: The name of the submit button. -
cancelButton
: The name of the cancel button.
4. Common functions of StandaloneInputModule component
-
Process
: A function that handles input events, used to convert input from the mouse and keyboard into events in the UGUI system.
5. Complete example code
Example 1: Clicking the button to trigger an event
using UnityEngine; using ; public class ButtonExample : MonoBehaviour { public Button button; private void Start() { (OnClick); } private void OnClick() { ("Button Clicked!"); } }
Operation steps:
- Create an empty object and mount the ButtonExample script to the object.
- Create a button in the scene and drag the button onto the button property of the ButtonExample script.
- Run the game, click the button, and the console will output "Button Clicked!".
Notes:
- You need to create a button in the scene and associate it with the button property in the code.
Example 2: Dragging an object
using UnityEngine; using ; public class DragExample : MonoBehaviour, IDragHandler { public void OnDrag(PointerEventData eventData) { = ; } }
Operation steps:
- Create an empty object and mount the DragExample script to the object.
- Run the game, click and drag the object in the scene, and the object will follow the mouse.
Notes:
- The DragExample script needs to be mounted to the object that needs to be dragged.
- You need to add the EventSystem component to the scene.
Example 3: Keyboard controls object movement
using UnityEngine; public class KeyboardExample : MonoBehaviour { public float speed = 5f; private void Update() { float horizontal = ("Horizontal"); float vertical = ("Vertical"); (new Vector3(horizontal, vertical, 0) * speed * ); } }
Operation steps:
- Create an empty object and mount the KeyboardExample script to the object.
- Run the game and use the keyboard's arrow keys to control the movement of objects.
Notes:
- The KeyboardExample script needs to be mounted to the object that needs to be controlled.
Example 4: Pressing the button to trigger an event
using UnityEngine; using ; public class SubmitButtonExample : MonoBehaviour { public Button submitButton; private void Start() { (OnSubmit); } private void OnSubmit() { ("Submit Button Clicked!"); } }
Operation steps:
- Create an empty object and mount the SubmitButtonExample script to the object.
- Create a button in the scene and drag the button onto the submitButton property of the SubmitButton Example script.
- Run the game, click the button, and the console will output "Submit Button Clicked!".
Notes:
- You need to create a button in the scene and associate it with the submitButton property in the code.
Example 5: Cancel operation
using UnityEngine; using ; public class CancelButtonExample : MonoBehaviour { public Button cancelButton; private void Start() { (OnCancel); } private void OnCancel() { ("Cancel Button Clicked!"); } }
Operation steps:
- Create an empty object and mount the CancelButtonExample script to the object.
- Create a button in the scene and drag the button onto the cancelButton property of the CancelButton Example script.
- Run the game, click the button, and the console will output "Cancel Button Clicked!".
Notes:
- You need to create a button in the scene and associate it with the cancelButton property in the code.
References
- Unity official documentation:StandaloneInputModule
The above is the detailed content of the use example of the standard input module component of Unity UGUI. For more information about Unity UGUI StandaloneInputModule, please follow my other related articles!