最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access Unity Webgl output from browser and vice versa - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Mar 27, 2016 at 2:22 user128511 asked Mar 26, 2016 at 5:55 Hendri TobingHendri Tobing 3715 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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 remended 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./Manual/webgl-interactingwithbrowserscripting.html and start reading from where it says "Calling JavaScript functions from a plugin" .

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.

发布评论

评论列表(0)

  1. 暂无评论