Home / Chaos Rift Published Games / Animal Friend Adventures / The Code that Saved The World

The Code that Saved The World

Animal Friends Adventure is well on it’s way to being released on PC through Steamgames. But it’s not all delicious cake nor an easy task. When Animal Friends was on mobile, everything was so easy. All that was needed was touch controls. We just had to detect fingers which are pretty limited in what is possible. Here’s a shortened tale of my struggle to bring full GamePad support to Animal Friends.

As an Indie game make I strongly believe in helping each other. This is why at the bottom, I’m fully sharing the code to my NGUI and Rewired solution. With the move to PC, I had to add Mouse, Keyboard and Gamepad support. Mouse is like fingers, so there was no issue with that. In game controls were not an issue. It was straight mapping the player’s motions. The real challenge in all of this was found in the User Interface. The Level Select screen was an entirely different beast to solve.

The entire UI for Animal Friends is NGUI. For input and vast compatibility I’m using Rewired. Both assets are considered must have. But none of my searches would ever show how to get them working together. In the game, Rewired controlled the character and NGUI controlled everything else. In the Unity Editor Everything worked perfectly. But once I built the game for Steam, uploaded and launched it as I would any steam game the problems because huge and evil. NGUI stopped working for user input. So it became impossible to navigate from the start menu all the way through to the game and play using only the gamepad. I don’t know about you, but I play PC games on a couch with gamepad these days.

The best I could find was a post by the NGUI maker how he solved the same issue in his game. But all his stuff was written for InControl. It needed serious modifications to work. Having been so focused on the raw game and it’s mechanics, I only knew the very basics of Rewired. So yeah, it was a bit tricky to adopt the code until yesterday morning. I woke up at 6am, grabbed coffee and started working on getting this working for Steam. Suddenly everything clicked and I had a solution.

All that’s needed is to put this code into a script file of the same name. Then attach it to the UICamera object. It works with delegates that intercept input and return Rewired values. I know it works well, because in the Rewired settings I had to adjust the D-Pad buttons to be digital. By default Rewired sets it to emulate analog. Very bad for quick UI navigation. here’s the code, and hopefully Steam Approves the game shortly!

using UnityEngine;
using Rewired;

public class NGUIRewiredInput : MonoBehaviour {
    static Player player;
    public int playerId = 0;
    
    void OnEnable()
    {
        player = ReInput.players.GetPlayer(playerId);
        UICamera.GetKey = GetKey;
        UICamera.GetKeyDown = GetKeyDown;
        UICamera.GetKeyUp = GetKeyUp;
        UICamera.GetAxis = GetAxis;
    }
    
    static bool GetKeyDown(KeyCode key)
    {
        if (key >= KeyCode.JoystickButton0)
        {

            switch (key)
            {
                case KeyCode.JoystickButton0: return player.GetButtonDown("UISubmit");
                case KeyCode.JoystickButton1: return player.GetButtonDown("UICancel");
                case KeyCode.JoystickButton2: return player.GetButtonDown("X");
                case KeyCode.JoystickButton3: return player.GetButtonDown("Y");
                case KeyCode.JoystickButton4: return player.GetButtonDown("LeftShoulder1");
                case KeyCode.JoystickButton5: return player.GetButtonDown("RightShoulder1");
                case KeyCode.JoystickButton6: return player.GetButtonDown("UISubmit");
                case KeyCode.JoystickButton7: return player.GetButtonDown("Start");
                case KeyCode.JoystickButton8: return player.GetButtonDown("LeftStickButton");
                case KeyCode.JoystickButton9: return player.GetButtonDown("RightStickButton");
            }
        }
        return Input.GetKeyDown(key);
    }

    static bool GetKey(KeyCode key)
    {
        if (key >= KeyCode.JoystickButton0)
        {
            switch (key)
            {
                case KeyCode.JoystickButton0: return player.GetButtonDown("UISubmit");
                case KeyCode.JoystickButton1: return player.GetButtonDown("UICancel");
                case KeyCode.JoystickButton2: return player.GetButtonDown("X");
                case KeyCode.JoystickButton3: return player.GetButtonDown("Y");
                case KeyCode.JoystickButton4: return player.GetButtonDown("LeftShoulder1");
                case KeyCode.JoystickButton5: return player.GetButtonDown("RightShoulder1");
                case KeyCode.JoystickButton6: return player.GetButtonDown("UISubmit");
                case KeyCode.JoystickButton7: return player.GetButtonDown("Start");
                case KeyCode.JoystickButton8: return player.GetButtonDown("LeftStickButton");
                case KeyCode.JoystickButton9: return player.GetButtonDown("RightStickButton");
            }
        }
        return Input.GetKey(key);
    }


    static bool GetKeyUp(KeyCode key)
    {
        if (key >= KeyCode.JoystickButton0)
        {
            switch (key)
            {
                case KeyCode.JoystickButton0: return player.GetButtonDown("UISubmit");
                case KeyCode.JoystickButton1: return player.GetButtonDown("UICancel");
                case KeyCode.JoystickButton2: return player.GetButtonDown("X");
                case KeyCode.JoystickButton3: return player.GetButtonDown("Y");
                case KeyCode.JoystickButton4: return player.GetButtonDown("LeftShoulder1");
                case KeyCode.JoystickButton5: return player.GetButtonDown("RightShoulder1");
                case KeyCode.JoystickButton6: return player.GetButtonDown("UISubmit");
                case KeyCode.JoystickButton7: return player.GetButtonDown("Start");
                case KeyCode.JoystickButton8: return player.GetButtonDown("LeftStickButton");
                case KeyCode.JoystickButton9: return player.GetButtonDown("RightStickButton");
            }
        }
        return Input.GetKeyUp(key);
    }

    static float GetAxis(string name)
    {
        //Debug.Log("Receiving Axis Name: " + name);
        switch (name)
        {
            case "LX": return player.GetAxis("LSHorizontal"); // Match UICamera.horizontalAxisName
            case "LY": return player.GetAxis("LSVertical"); // Match UICamera.verticalAxisName
            case "RX": return player.GetAxis("RSHorizontal"); // Match UICamera.horizontalAxisName
            case "RY": return player.GetAxis("RSHorizontal"); // Match UICamera.horizontalAxisName
            case "DX": Debug.Log("DX:" + player.GetAxis("DPHorizontal")); return player.GetAxis("Horizontal");
            case "DY": Debug.Log("DY:" + player.GetAxis("DPVertical"));  return player.GetAxis("Vertical");
                //case "TX": return dev.RightTrigger - dev.LeftTrigger;
        }
        return Input.GetAxis(name);
    }
}

About Craig

Craig is the founder of The Chaos Rift and developer of the games published here. In his spare time he'll also write about games, play games and dream about games. Being a Game developer has been a dream of Craig's since he was 14 and after some detours has finally started to realize his dreams.

Check Also

panda swagger animal friends

So this Just happened!

We completed the store page, marketing and uploaded the App to Steam! Everything has been …

Talk to us!

This site uses Akismet to reduce spam. Learn how your comment data is processed.