- Home /
WebGL: Interacting with browser scripting unable to call java script method from C#
Hello,
I want to to interact with javascript from c#. For this I was following the below link:
https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
When I am trying to call any method from c# it gives me error missing function.
For eg: I have created a method in javascript like this.
mergeInto(LibraryManager.library, { Hello: function () { window.alert("Hello, world!"); }, });
and placed this script at path Assets/Plugins/WebGL/MyPlugin.jslib.
Then I am trying to access this method from c# like:
using UnityEngine; using System.Runtime.InteropServices;
public class NewBehaviourScript : MonoBehaviour {
[DllImport("__Internal")]
private static extern void Hello();
void Start() {
Hello();
}
}
but it gives me missing function : Hello while running it on browser. Please screen shot for the same
Please Help me out! Thank you!
Answer by SkyAngel_AOG · Apr 03, 2018 at 12:51 PM
HI, @unity_mwF5s3xOpUdXNA. I checked your code on my computer. Everything works perfectly! Perhaps you made a mistake elsewhere ![alt text][1]
My Code in C#
using System.Runtime.InteropServices; using UnityEngine;
public class TestBrowser : MonoBehaviour {
[DllImport("__Internal")]
private static extern void Hello();
void Start()
{
Hello();
}
}
jslib:
mergeInto(LibraryManager.library, {
Hello: function () { window.alert("Hello, world!"); },
});
Location jslib: Assets/Plugins/WebGL/MyPlugin.jslib [1]: /storage/temp/114196-screenshot-1.png
Answer by hassanyawar · Dec 19, 2018 at 08:21 AM
My reply over here is very late entry considering the question has been asked over a ago. So I faced a similar problem.
Your javascript and C# code seems fine which means the problem lies somewhere else. In my case I had forgot to change the .jslib plugin platforms from Unity's inspector window. When I was receiving missing function error, my .jslib plugin was checked into Editor platform. I unchecked it and checked WebGL platform. After creating the build everything worked as it was supposed to be.
I'm attaching a screenshot so you or anyone else facing a similar problem can understand.
Your answer
Follow this Question
Related Questions
How to send a large string from Javascript to WebGL ? 0 Answers
Is there a way to send an XMLHttpRequest and get the response in a WWW object? 0 Answers
Unity WebGL Error Running on Local Server 0 Answers
Interact across browser javascipt and unity webgl 2 Answers
Passing array of strings to native JavaScript plugin on WebGL 1 Answer