You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

新手求助: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.

1. Build Custom Virtual Controls with UGUI

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 smaller Image (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")) with joystickReference.GetMovementInput().x and joystickReference.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 Player action map, then add a Move action set to Vector2 type.
    • For mobile, add a binding: Touchscreen → Primary Touch → Position, then add a Stick Deadzone processor to filter out small accidental touches.
    • Add a keyboard/mouse binding too (like WASD or Arrow Keys) for easy testing on desktop.
  • Add a virtual joystick UI: Go to GameObject → UI → Input System UI → Virtual Joystick. Drag your Input Actions asset into the joystick’s Action Reference field, and select the Player/Move action.
  • 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);
    }
}
3. Critical Mobile Adaptation Tweaks

Don’t forget these settings to make your game feel right on mobile:

  • Screen fit: Open Player Settings → Resolution and Presentation. Set Default Orientation to match your game (Portrait/Landscape), and choose Fit for Aspect Ratio to 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 like ETC2 for 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:21:32