Early feedback for my major assessment project at AIE, Project Purification, showed an interest in seeing it find a home on an Android tablet. I found this idea to be against my natural instincts in some ways. Since the PC has been, and probably always will be my main focus as a gamer and a developer. But, given that I had recently acquired a shiny new Samsung tablet I figured it might be fun to get the game running on it.

Since I was developing this project in the Unity engine, I assumed it would be simple to get an Android build up and running. And while, as always in game dev, I ran into a bunch of unexpected and even odd-ball problems, overall it actually was pretty damn simple. Once I had changed a few of my input calls to run through Unity’s ‘CrossPlatformInputManager’ class (available in their standard assets packages) the game basically just worked.

Well… It did work, but there of course some issues straight away. See I had coded much of the game to work specifically for a keyboard and mouse. And while the input manager does in fact let you use that same PC specific input code for other platforms (with just a minor change to the actual call), touches and mouse movements are two very different things. What feels natural when using a mouse sometimes just doesn’t translate to a touch screen at all. So while the code I had written technically worked, some parts of it were clunky and horrible. Especially the camera control code.

Eventually I did end up writing bespoke touch-based code for the camera controls. But said code isn’t particularly special or anything, not worthy of a blog post in it’s honour. It was just a pretty standard implementation of a two finger pan with some direction inversion options so that it can feel natural to different users. What was actually noteworthy in my opinion was that there was actually a decent chunk of code that didn’t require any extensive rewrites at all. I hadn’t really thought about it at the time, but looking back was all to do with the fact that, while swiping your finger around on the screen is a very different experience to the way you move a mouse cursor around, tapping on a button is actually very similar to clicking on one. So most of the code that revolved around clicking, worked perfectly well for taps as well.


//if left click and not right clicking. Or if only one touch on mobile
if (CrossPlatformInputManager.GetButtonDown("Fire1") && !CrossPlatformInputManager.GetButtonDown("Fire2"))
{
    //so long as we arent clickin on the UI and we have nothing currently locked to the mouse
    if (!IsPointerOverUIObject() && LockedGO == null)
    {
        //check if we have clicked on a game object
        if (Physics.Raycast(MouseRay, out MouseHit, 100.0f))
        {
            LockedGO = MouseHit.transform.gameObject;
            LockedGI = LockedGO.GetComponent<Grid_Item>();

            //if we have clicked on a Grid_Item
            if (LockedGI)
            {
                //set locked object type to grid item
                LOT = LockedObjectType.GRID_ITEM;

                //If we have clicked on a buggins, change it back to normal
                if (LockedGI.isBuggins)
                {
                    GameManager.GM.BugIn.Remove(LockedGO);
                }

                LockedGO.layer = 9;                           // set to LTM layer
                LockedGI.LockToMouse =true;                   // set object movement flags
                LockedGI.shouldMove =true;                    // set object movement flags
                ObjectLocked = true;                          // set object movement flags
                LockedGI.lerpTimer = 0.0f;                    //reset lerptimer

                GameManager.GM.LastLockedToMouse = LockedGI;  //set this object to last locked to mouse
            }
        }
    }
}


The above code will run whenever the user clicks on one of the objects in the game. Specifically it tells the game to lock that object to the mouse so that the player can move it around and it also changes Buggins objects back to normal.

The really interesting there here is that this code works perfectly for PC and mobile platforms natively, without any major rewrites at all. The only two changes that I had to make to allow it work on mobile were right in the first line of code. Originally that line looked like this:

if (Input.GetButtonDown("Fire1")

In the final project it looks just like it did in that first code block:

if (CrossPlatformInputManager.GetButtonDown("Fire1") && !CrossPlatformInputManager.GetButtonDown("Fire2"))

Sure its now a bit ugly due to how long it is, but it was a damn simple change to make to get the game onto a whole new platform. The main change here is just using the CrossPlatformInputManager class rather than the Input class. That second part after the logical AND operator is also there for mobile platforms, it just makes sure that this bit of code doesn’t run when there are two fingers touching the screen, since that means the user wants to perform a camera pan, not a tap.

This same thing was true in a number of other mouse specific scripts. I just needed to change from using the Input class to using Unity’s CrossPlatformInputManager class. Of course that class isn’t a perfect catch all that fixes every problem you’ll ever run into. And at the end of the day, most people will probably want to write their own Input Manager class at some point if they are trying to keep a set of project files that can be built for multiple platforms. But to me this is just another example of why I love to use the Unity engine. It lets you prototype your ideas almost at the speed of thought (bar those annoying logical bugs ;p). Due to the class they had prepared for just this purpose I was able to get my then PC-only game working on my tablet with some very small code changes. (And a decent number of hours installing various SDKs and trying to work around my schools annoying policy on software installs). And even though I later spent many hours perfecting my own bespoke touch control code, just the ability to get SOMETHING working very quickly has proved to be amazingly useful to me time and time again.