Call C# Method System for WebGL

Introduction

The WebGL Call C# Method System pack provides a way to call your methods from the browser's native JavaScript in WebGL builds, and obtain the method's response. The system is thus an extension over the standard Unity's SendMessage capabilities, which do not let you get the return value or call methods with more than one parameter.


The asset overcomes these limitations, providing an easy-to-use way to call the C# methods and return their values, and including a Sample scene, a Sample WebGL template, and several unit and integration tests.

  • Can call methods from browser-native JavaScript, and get the return value.
  • Can call methods with multiple parameters.
  • Full source code provided, with well-written and commented C# code, and with editor-runnable unit tests.

Here we include portions of the Sample Scene that is provided with the asset, showcasing how the system is used. The JavaScript snippet is the code that makes use of the library to call certain C# methods. The C# snippet are the methods that are called themselves.

Calling C# methods from JavaScript
    // When we click Apply Damage
    document.getElementById("apply-dmg").onclick = function (ev) {

        // Call the "ApplyDamage" method in C#, on the MrSmiley object.
        // The ApplyDamage method takes the damage (a long) and the type (a string) as a parameter.
        // We receive the return value through a callback.
        callMethod("MrSmiley", "ApplyDamage", [10, "ice"],
                function (health) {
                    // Health that we have after applying the damage.
                    document.getElementById("health").setAttribute("value", health);
                },
                function (errorMessage) {
                    console.error(errorMessage)
                });
    };

    // When we click Resurrect
    document.getElementById("resurrect").onclick = function (ev) {
        // We call the Resurrect method on the MrSmiley object. This time it takes no parameters and
        // we are not interested in the return value.
        callMethod("MrSmiley", "Resurrect");
    };

C# Methods to call
    /// <summary>
    /// Apply the specified damage.
    /// </summary>
    /// <param name="damage">Damage to apply.</param>
    /// <param name="type">Damage type.</param>
    /// <returns>Health remaining.</returns>
    public long ApplyDamage(long damage, string type)
    {
        Debug.Log(string.Format("Received {0} {1} damage!", damage, type));
        health -= damage;
        if(health <= 0)
        {
            // The Smiley died! Make it black and white.
            mSmileyRenderer.material.shader = smileyDeadShader;
            health = 0;
        }
        else
        {
            // The smiley is still alive, so we ensure that the shader is the default (color) one.
            mSmileyRenderer.material.shader = mDefaultShader;
        }

        UpdateHealthMessage();

        return health;
    } // !ApplyDamage


    /// <summary>
    /// Restores the smiley to full health.
    /// </summary>
    public void Resurrect()
    {
        health = 100;
        mSmileyRenderer.material.shader = mDefaultShader;
        UpdateHealthMessage();
    } // !Resurrect