Deprecated: Return type of WP_Theme::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/class-wp-theme.php on line 554

Deprecated: Return type of WP_Theme::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/class-wp-theme.php on line 595

Deprecated: Return type of WP_Theme::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/class-wp-theme.php on line 535

Deprecated: Return type of WP_Theme::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/class-wp-theme.php on line 544

Deprecated: Return type of WP_REST_Request::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/rest-api/class-wp-rest-request.php on line 960

Deprecated: Return type of WP_REST_Request::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/rest-api/class-wp-rest-request.php on line 980

Deprecated: Return type of WP_REST_Request::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/rest-api/class-wp-rest-request.php on line 992

Deprecated: Return type of WP_REST_Request::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/rest-api/class-wp-rest-request.php on line 1003

Deprecated: Return type of WP_Block_List::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/class-wp-block-list.php on line 151

Deprecated: Return type of WP_Block_List::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/class-wp-block-list.php on line 175

Deprecated: Return type of WP_Block_List::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/class-wp-block-list.php on line 164

Deprecated: Return type of WP_Block_List::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/class-wp-block-list.php on line 186

Deprecated: Return type of WP_Block_List::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/class-wp-block-list.php on line 138

Deprecated: Return type of WP_Block_List::offsetExists($index) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/class-wp-block-list.php on line 75

Deprecated: Return type of WP_Block_List::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/class-wp-block-list.php on line 89

Deprecated: Return type of WP_Block_List::offsetSet($index, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/class-wp-block-list.php on line 110

Deprecated: Return type of WP_Block_List::offsetUnset($index) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/class-wp-block-list.php on line 127

Deprecated: Return type of WP_Block_List::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /www/hosting/ChaosriftGames/wp-includes/class-wp-block-list.php on line 199

Deprecated: DateTime::__construct(): Passing null to parameter #1 ($datetime) of type string is deprecated in /www/hosting/ChaosriftGames/wp-includes/script-loader.php on line 333

Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /www/hosting/ChaosriftGames/wp-content/plugins/wordpress-seo/src/conditionals/third-party/elementor-edit-conditional.php on line 22

Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /www/hosting/ChaosriftGames/wp-content/plugins/wordpress-seo/src/conditionals/third-party/elementor-edit-conditional.php on line 28

Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /www/hosting/ChaosriftGames/wp-includes/class-wp.php on line 173
The Code that Saved The World - Chaos Rift Games
Deprecated: parse_str(): Passing null to parameter #1 ($string) of type string is deprecated in /www/hosting/ChaosriftGames/wp-includes/formatting.php on line 4975

Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /www/hosting/ChaosriftGames/wp-content/plugins/wordpress-seo/src/generators/schema-generator.php on line 185

Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /www/hosting/ChaosriftGames/wp-content/plugins/wordpress-seo/src/generators/schema-generator.php on line 185

Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /www/hosting/ChaosriftGames/wp-content/plugins/wordpress-seo/src/generators/schema-generator.php on line 185

Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /www/hosting/ChaosriftGames/wp-content/plugins/wordpress-seo/src/generators/schema-generator.php on line 185

Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /www/hosting/ChaosriftGames/wp-content/plugins/wordpress-seo/src/generators/schema-generator.php on line 185

Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /www/hosting/ChaosriftGames/wp-includes/wp-db.php on line 3030
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.

Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /www/hosting/ChaosriftGames/wp-includes/wp-db.php on line 3030

Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /www/hosting/ChaosriftGames/wp-includes/wp-db.php on line 3030

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 …


Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /www/hosting/ChaosriftGames/wp-includes/wp-db.php on line 3030

Leave a Reply

Your email address will not be published. Required fields are marked *

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