An open-source project dedicated to providing mobile game developers a quick headstart on setting up mobile/touch controls for their Unity game.
This asset requires two packages to be installed in your project:
- The Input System Package
- To install: Editor > Window > Package Manager > Packages: Unity Registry > Input System
- DOTween
- To install: Add to Assets > Open in Unity > Select which files you want to include > Import
- 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 in implementing cross-platform a lot more do-able. This asset utilizes the
On-Screen Control
component that is 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 scripts to check if the buttons are pressed just to perform an action. In other words, the scripts also get checked even when not called in theUpdate()
method which furthermore improves the game loop's overall performance.
- It's simply a better way to manage tasks that are too simple for Coroutines; the best part is that DOTween makes less to no garbage at all. This asset utilizes DOTween to animate and change button colors and sprites.
Table of Contents |
---|
Features |
Usage |
Sponsorship |
Contribution |
This project extends from the On-Screen Control components 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 the
On-Screen Button
component provided by the Input System package. It can have animated sprites and color fading!
- ControlButton is an extension of the
-
- ControlStick is a heavily-improved version of the
On-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.
- ControlStick is a heavily-improved version of the
-
- 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!!!
- You can implement the
ControlExtension
component to any of the provided scripts according to your liking. The code I wrote in the scripts (especially for ControlButton) are merely placeholders and you can re-write them to suit your needs.
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!