Invisible Buttons
I just started using Unity today, and figured a good way to learn it would be to re-create my games I made with the Starling Framework. However, I have run into a small problem. My Starling game control currently uses 2 invisible Quads, each with their own event listener, which acts as an invisible button. One covers the left half of the screen, and when pressed moves the player up, and the other covers the right half of the screen, and when pressed causes the player to shoot.
My question is, what is the best solution to achieve the same result in Unity? These invisible buttons will need to resize themselves according to screen size.
Any suggestions would be greatly appreciated.
Answer by eses · Sep 11, 2018 at 10:32 AM
Hi @gerard9
You can do exactly the same, if you use Unity UI system.
I'd recommend reading / learning more about UI system, see this link:
https://unity3d.com/learn/tutorials/s/user-interface-ui
Here's some pseudo code:
Create a UI system Canvas. Make sure you have Canvas Scaler too added.
Make canvas screen space. Make it have proper reference resolution.
Add two Button GameObjects on Canvas (basically Gameobject with Image and Button Components).
For example, anchor the left side and right side Button RectTransforms so that the anchors are on corners of both RectTransform's rects. This way these UI element RectTransforms will stick to relative positions on screen, no matter what the aspect ratio / resolution is.
Make button GameObject's Image component color transparent. This way you can receive clicks but not see your button. There are more optimized ways to do this, you can read about this from forums.
In both buttons, add on their OnClick some methods to call. For example, you could add here your character input script, and select a public method that you want to call, like Move method that takes some int value.
@eses Thank you for sharing your experience, and the link to the UI information. It was the exact information I was looking for.
At the moment, optimization is not my primary goal, as I am more interested in learning the Unity interface. I am used to doing everything via scripts, so finding an equivalent “Unity Interface” option is my main struggle.