An open-source project dedicated to providing mobile game developers a quick headstart on setting up mobile/touch controls for their Unity game.
Before you use this project as a base for your mobile game, I am putting this here to remind you that this project is currently being actively developed and things are subject to change. Kindly star the repo before using, forking or cloning, please!
- The Input System Package
- To install: Editor > Window > Package Manager > Packages: Unity Registry > Input System
- This asset also provide scripts that support Unity's Legacy Input System. However, using the new Input System package gives you more advantages than the legacy input system. It has more flexible features and settings that everyone can configure, and it helps make implementing cross-platform play a lot more do-able. This asset utilizes the
On-Screen Control
class provided by the Input System package which lets you configure the button/joystick bindings according to your liking. When combined with events subscription, you don't even have to reference the control scripts just to check if it's "pressed" just to perform an action, and it also gets checked even when not called in theUpdate()
method which furthermore improves the game loop's performance.
Table of Contents |
---|
Features |
Usage |
Sponsorship |
Contribution |
This project extends from the on-screen controls that Unity's Input System package have provided by default. The following demos are using (NKRsys-FPP-CC) NeKoRoSYS's First Person Perspective Character Controller to showcase the mobile controls.
-
ControlButton
is an extension of theOn-Screen Button
component provided by the Input System package. It can have animated sprites and color fading!
-
ControlStick
is a heavily-improved version of theOn-Screen Stick
component provided by the Input System package. It has a variety of configurable settings, and it can be used for several features that your game may already have, like player movement.
-
ControlPad
is a component that registers input if the touch made contact within its rect bounds. This is a totally new feature, as the Input System does not provide their own rendition of what could be a touchpad. It supports multitouch and can be configured and used for a variety of features you may plan on implementing to your project!
Prefabs are cool and all, but I recommend you to set up the controls from scratch like a boss!
-
- Create a 2D GameObject inside the canvas.
- Add an
Image
component and change the sprite if applicable. - Add the
ControlButton
script then customize it if necessary. - Set the
Control Path
binding. - Press, release, and enjoy!
-
- Create a parent 2D GameObject inside the canvas, make sure the rect bounds are big enough to cover a portion of the screen. (I recommend covering the entire half!)
- Add an
Image
component to the parent 2D GameObject and make the colors transparent. - Add the
ControlStick
script then configure it if necessary. - Set the
Control Path
binding. - Create two children 2D GameObjects and add an
Image
component to both of them. Rename the first child to "State," then rename the second child to "Handle" to avoid confusion.
"State" is the background of the joystick, and one can code it so it changes visuals depending on the state of what's being controlled by the joystick (eg. make it color red if active, make it scale bigger or smaller depending on how far the joystick is pushed, etc). On the other hand, "Handle" is what you drag to move the joystick. I have provided sample sprites for you to use if you don't have any!
- Assign the parent 2D GameObject to the
inputArea
variable in theControlStick
script. - Assign "State" to the
stickBounds
variable. - Assign "Handle" to the
stickHandle
variable. - Hold, drag, and enjoy!!
-
- Create an empty 2D GameObject inside the canvas and set its rect bounds so that it covers the entire right half of the screen.
- Add an
Image
component and set the colors to transparent. - Add the
ControlPad
script. - Subscribe to the
OnTouchDrag
UnityEvent using another script, which would most likely be your camera controller.
ControlPad controlPad; void Start() => controlPad.OnTouchDrag.AddListener(Something());
- Press, drag, look/pan around, and enjoy!!!
I am an aspiring software and game developer that currently do stuff solo, and I need funding to motivate me to do a lot better on my tasks so that I could deliver way better content. Donating is not a must, but it will be immensely cherished and appreciated!
Something's wrong with the code or you know better workarounds and alternatives? You can either make an issue or a pull request. It will be very much appreciated!