新手求助:Unity 2D Game Kit移动端控制器实现方法
Hey there! As a fellow Unity dev who’s built a handful of 2D mobile casual games, I totally get where you’re coming from—Unity 2D Game Kit is awesome for kicking off a project, but it doesn’t include built-in mobile controls. Let’s walk through two solid methods to add mobile support, plus key setup tweaks to make your game feel polished on phones and tablets.
This is a straightforward approach if you want full control over how your controls look and behave:
- Create UI elements: Add a large
Image(as the joystick background) to your Canvas, then nest a smallerImage(the joystick handle) inside it. Position the joystick in a corner (like bottom-left) where it won’t block gameplay. - Add a control script: Attach this simple script to your joystick background to handle touch input:
using UnityEngine; using UnityEngine.EventSystems; public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler { private RectTransform joystickBg; private RectTransform joystickHandle; private Vector2 inputVector; void Start() { joystickBg = GetComponent<RectTransform>(); joystickHandle = transform.GetChild(0).GetComponent<RectTransform>(); } public void OnDrag(PointerEventData eventData) { Vector2 pos; if (RectTransformUtility.ScreenPointToLocalPointInRectangle(joystickBg, eventData.position, eventData.pressEventCamera, out pos)) { // Normalize input to a -1 to 1 range pos.x = (pos.x / joystickBg.sizeDelta.x); pos.y = (pos.y / joystickBg.sizeDelta.y); inputVector = new Vector2(pos.x * 2 - 1, pos.y * 2 - 1); // Clamp input to prevent over-extending the joystick inputVector = inputVector.magnitude > 1f ? inputVector.normalized : inputVector; // Move the joystick handle joystickHandle.anchoredPosition = new Vector2( inputVector.x * (joystickBg.sizeDelta.x / 3), inputVector.y * (joystickBg.sizeDelta.y / 3) ); } } public void OnPointerDown(PointerEventData eventData) => OnDrag(eventData); public void OnPointerUp(PointerEventData eventData) { inputVector = Vector2.zero; joystickHandle.anchoredPosition = Vector2.zero; } // Call this from your player script to get movement input public Vector2 GetMovementInput() => inputVector; }
- Hook it up to your player: In your existing player movement script (from the 2D Game Kit), replace keyboard input (like
Input.GetAxis("Horizontal")) withjoystickReference.GetMovementInput().xandjoystickReference.GetMovementInput().y.
Unity’s modern Input System is more flexible and built for multi-platform support, including mobile:
- Install the Input System: Open the Package Manager (Window → Package Manager), search for "Input System", and install it. You’ll need to restart Unity when prompted.
- Create Input Actions: Right-click in the Project window → Input Actions → New. Open the editor, create a
Playeraction map, then add aMoveaction set toVector2type.- For mobile, add a binding:
Touchscreen → Primary Touch → Position, then add aStick Deadzoneprocessor to filter out small accidental touches. - Add a keyboard/mouse binding too (like
WASDorArrow Keys) for easy testing on desktop.
- For mobile, add a binding:
- Add a virtual joystick UI: Go to GameObject → UI → Input System UI → Virtual Joystick. Drag your Input Actions asset into the joystick’s
Action Referencefield, and select thePlayer/Moveaction. - Update your player script: Use this code to read input from the system:
using UnityEngine; using UnityEngine.InputSystem; public class PlayerMovement : MonoBehaviour { private PlayerInputActions inputActions; public float moveSpeed = 5f; void Awake() { inputActions = new PlayerInputActions(); } void OnEnable() => inputActions.Player.Enable(); void OnDisable() => inputActions.Player.Disable(); void Update() { Vector2 moveInput = inputActions.Player.Move.ReadValue<Vector2>(); transform.Translate(moveInput * moveSpeed * Time.deltaTime); } }
Don’t forget these settings to make your game feel right on mobile:
- Screen fit: Open Player Settings → Resolution and Presentation. Set
Default Orientationto match your game (Portrait/Landscape), and chooseFitforAspect Ratioto avoid stretching. - Touch-friendly UI: Make all interactive elements (joysticks, buttons) at least 48x48px—this is the minimum size for reliable finger taps. Position controls away from important game content.
- Performance optimizations: For smooth gameplay on lower-end devices:
- Use Sprite Atlases to reduce draw calls.
- Compress textures in the Inspector (set texture type to
Sprite (2D and UI)and choose a compression format likeETC2for Android). - Disable unnecessary colliders and components when they’re not in use.
Hope these steps get your 2D game up and running on mobile without a hitch! Feel free to adjust the scripts or settings to match your game’s unique style.
内容的提问来源于stack exchange,提问作者ALI FARZAN TAHAA

