Fantacode Demo — Changing Mobile Input
When I left off last time, I was discussing maybe changing from trying to predict showing the mobile UI, and switching to making it a setting in the demo. First change? I removed OnDeviceChanged, which I was using to detect attached gamepads to determine whether to show the mobile controls.
private void OnDeviceChange(InputDevice device, InputDeviceChange change)
{
// If a device, reconfigure mobile controls
if (change == InputDeviceChange.Added || change == InputDeviceChange.Removed)
{
ConfigureMobileControls();
}
}Secondly, I added a quick class for not only tracking a value for whether to detect mobile controls, but also storing it in PlayerPrefs.
public class MobileUIToggle : MonoBehaviour
{
private const string PREF_KEY = "EnableMobileControls";
[SerializeField] private Toggle touchToggle;
private void Start()
{
// Default to TRUE on mobile platforms (e.g., Android build), FALSE on PC/Web unless saved
bool defaultValue = Application.isMobilePlatform;
bool isEnabled = PlayerPrefs.GetInt(PREF_KEY, defaultValue ? 1 : 0) == 1;
if (touchToggle != null)
{
touchToggle.isOn = isEnabled;
touchToggle.onValueChanged.AddListener(OnToggleChanged);
}
}
private void OnToggleChanged(bool isEnabled)
{
PlayerPrefs.SetInt(PREF_KEY, isEnabled ? 1 : 0);
PlayerPrefs.Save();
}
public static bool IsMobileControlsEnabled()
{
bool defaultValue = Application.isMobilePlatform;
return PlayerPrefs.GetInt(PREF_KEY, defaultValue ? 1 : 0) == 1;
}
}For the actual toggle, I went with a Toggle UI element on the Canvas.
The icon is from game-icons.dev. And, well, it basically works. Mostly. I’m now experiencing some odd bugs such as the mouse cursor still being captured when I exit a level, and I’m noticing that the Shooter System doesn’t display its controls due to it using a slightly different nesting scheme.
Get Sean Duggan’s stories in your inbox
Join Medium for free to get updates from this writer.
Actually, that’s revealing another issue with the default setup of these systems… we probably should only show some of these icons where relevant. For the Shooter system, if we don’t have a gun enabled, why would we show the Fire button? If we don’t have the grappling hook enabled (and the setting isn’t there to allow it to be equipped on the key press), why show that icon? Unfortunately, I suspect that’s going to be a bit more of an in-depth change, and I have a sneaking suspicion that it’s going to be one that I’m going to have to keep putting back in with later system releases.








