2

I receive a package of Unity webgl and put that into a course in Wordpress. The index.html produced form Unity is accessed inside an iframe there. This must be able to count user click when accessing the course.

The Unity developer only said that the variable is Click and there is also information:

Application.ExternalCall("Send_Clicks", clickCount.ToString());

I read Unity documentation, I should create a js script to retrieve the message from Unity. How to retrieve the message from Unity? Where do the script should be placed?

Any answers are greatly appreciated. Thank you.

2 Answers 2

5

EDIT:

Unity now supports Application.ExternalCall for WebGL so you no longer need a plugin for that. I will leave the old answer up here for those that use the older version of Unity. It is still recommended to use the old answer below because there is a security restrictions when Application.ExternalCall is used in a packed Chrome app. This does not happen with a plugin.

OLD ANSWER:

Application.ExternalCall is for WebPlayer only. Application.ExternalCall will NOT work for WebGL.

This can be done with a plugin. Make your JavaScript code to look like something below. Then name it PluginName.jslib. The extension must be .jslib. Save it to Assets/Plugins/WebGL/PluginName.jslib

var MyPlugin = {
    Hello: function()
    {
        window.alert("Hello, world!");
    },
    HelloString: function(str)
    {
        window.alert(Pointer_stringify(str));
    },
};
mergeInto(LibraryManager.library, MyPlugin);

Now in C#, you can should do something like this:

using UnityEngine;
using System.Runtime.InteropServices;

public class NewBehaviourScript : MonoBehaviour {

    [DllImport("__Internal")]
    private static extern void Hello();

    [DllImport("__Internal")]
    private static extern void HelloString(string str);

    void Start() {
        Hello(); //Call Hello from JavaScript

        HelloString("This is a string."); //Call HelloString from JavaScript with parameter
    }
}

You must include using System.Runtime.InteropServices; to be able to do this. For more information about this go to http://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html and start reading from where it says "Calling JavaScript functions from a plugin" .

Sign up to request clarification or add additional context in comments.

6 Comments

Application.ExternalCall works with WebGL builds nowadaus too -- documented on that same page.
@antont What is nowadaus ?
uh, typo, tried to say 'nowadays', as in currently, in late 2016 :)
Oh lol. That didn't come to my mind. I checked the documentation and this seems to be true. I have updated the answer. Thanks for the heads up.
perhaps best move the edit to the top? doesn't make sense to say "Application.ExternalCall is for WebPlayer only. Your question is about WebGL and not WebPlayer so Application.ExternalCall will NOT work for WebGL" so prominently when that is exactly the opposite now
|
1

Technically you can also use jQuery. The only problem is, you have to make sure it is inside onReady function. Just a side note.

Application.ExternalCall("Send_Clicks", clickCount.ToString());

To Explain further, the Code above is pretty Straight forward. The arguments that you have for `ExternalCall is 1: The Method name 2: the Argument parameters.

You can also pass in different numbers of Parameters, just make sure you have the correct overload.

This is a basic example. In your Unity Let us say that you have this function called.

Application.ExternalCall("MyInfo", "John", 23);

And in your wordpress Page. make sure you have a <script> tag A normal JS will do.

<script>
function MyInfo( arg1, arg2 )
    {
        alert("My name is :"+arg1 + " Age is : "+ arg2 );
    }
</script>

Simple as that, normally in MVC, JS is put somewhere at the bottom of the page <body> tag. To make sure, it knows all the ids and class names. Best of luck.

3 Comments

This will not work for webgl. It will for weplayer, but the tag is about webgl.
good Point. Missed that one. Application.ExternalCall lost me on track.
that's fine. I had to dig in the document to realize there were some changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.