How to use WordPress user information to forcibly implement login function in Unity WebGL

日本語

At first

When you create a WebGL game in Unity, you usually can’t store game progress data etc. in user local environment (probably due to security issues). Inevitably, game data have to be stored online. If you store user data online, you also inevitably need to be membership-based to determine which user the data you are storing belongs to. In other words, the game system must have a membership function, at least a membership registration function and a login function.

However, the membership registration function and login function are different functions from the game itself. Implementing such a feature for a small hobby game seems like extra effort. Of course, there is no problem for those who want to enjoy making it including that point, but for those who want to release the game quickly, they want to save the effort on the non-core part.

Therefore, in this article, I will introduce how to implement the membership function by linking WordPress and Unity’s WebGL (slightly forcibly).

Overall picture

The member registration function will be implemented in WordPress. For the login function, create a php program that mediates between WordPress and Unity, and create a system that accesses the WordPress login function from the Unity side via that php program.

Preparation of WordPress side

First, implement the membership registration function in WordPress. To implement the membership registration function in WordPress, it is easy to install a plugin.

There are multiple plugins that implement the membership registration function, but since it affects the cooperation with Unity, it is better to select a plugin that does not affect the return value of the wp_signon function of WordPress. I used WP-Members.

For how to use WP-Members, refer to the external site.

Preparation of cooperation program

Create a php program that integrates WordPress and Unity.

At the beginning of the program, load wp-load.php in the directory where you installed WordPress so that you can use WordPress functions with external php. Then pass the ID and password passed in the HTTP POST method to wp_signon to authenticate the user.

The specific code is, for example:

require_once( dirname(dirname( __FILE__ )) . '/wp/wp-load.php' );

$creds = array();
$creds['user_login'] = @$_POST['user_login'];
$creds['user_password'] = @$_POST['user_password'];
$creds['remember'] = false;

$user = wp_signon( $creds, false );

if ( is_wp_error($user) )
{
    echo $user->get_error_message();

    exit('<false>');
}

In this case, if authentication fails, a message with “<false>” at the end will be returned.

Preparation of Unity side

Create a script to send and receive information with the php program for cooperation.

Use UnityWebRequest to call the cooperation program. Use WWWForm to pass the ID and password to the cooperation program.

The specific code is, for example:

    public void Login(string strID, string strPassword)
    {
        StartCoroutine(CoLogin(strID, strPassword));
    }

    IEnumerator CoLogin(string strID, string strPassword)
    {
        if(string.IsNullOrWhiteSpace(strID))
        {
            yield break;
        }

        if(string.IsNullOrWhiteSpace(strPassword))
        {
            yield break;
        }

        WWWForm form = new WWWForm();
        form.AddField("user_login", strID);
        form.AddField("user_password", strPassword);

        UnityWebRequest www = UnityWebRequest.Post(MBCManager.WEBGL_URL + "login.php", form);
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.LogError(www.error);
            yield break;
        }
        else
        {
            Debug.Log("Login complete! The response is " + www.downloadHandler.text);
        }

        if(www.downloadHandler.text.Contains("<false>"))
        {
            //Failed to log in.
            yield break;
        }
        //Success to log in.
    }

In this case, if a message containing “<false>” is returned, it is judged that authentication failed = login failed, otherwise authentication success = login success.

Operation method

The code introduced above is for explaining the mechanism of linking WordPress and Unity, and when actually operating it, it must be modified according to the actual situation.

For example, if you want to save the progress of the game, send the game data to the cooperation program in addition to the ID and password, and if the authentication is successful, continue to save the data under the ID name directory.

For example, if you want to read the progress of the game, if the authentication is successful on the cooperation program, it will continue to read the file under the ID name directory and return a message to the client.