I’m doing something wrong and I don’t know what, so maybe if I detail my process someone can point to my error.
I’m using information in the documentation from here > Unity - Manual: Interaction with browser scripting
Step 1
From that page I just copy and paste the entire first section of code into a JavaScript editor file, then save it as a Javascript library and name it something like js_test.jslib:
Code Sample:
mergeInto(LibraryManager.library, {
Hello: function () {
window.alert("Hello, world!");
},
HelloString: function (str) {
window.alert(Pointer_stringify(str));
},
PrintFloatArray: function (array, size) {
for(var i = 0; i < size; i++)
console.log(HEAPF32[(array >> 2) + i]);
},
AddNumbers: function (x, y) {
return x + y;
},
StringReturnValueFunction: function () {
var returnStr = "bla";
var bufferSize = lengthBytesUTF8(returnStr) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(returnStr, buffer, bufferSize);
return buffer;
},
BindWebGLTexture: function (texture) {
GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[texture]);
},
});
Step 2
In my Unity project I go into my Assets folder and create a folder named Plugins. so that is Assets > Plugins.
Step 3
I put the js_test.jslib file into that Plugins folder.
Step 4
In my Unity project, under Assets > Scripts I create a new C# script and name it something like cs_js_test.cs.
Step 5
I go back to the Unity documents and copy the entire second section of code, pasting it into that C# script, so it looks like:
Code Sample:
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);
[DllImport("__Internal")]
private static extern void PrintFloatArray(float[] array, int size);
[DllImport("__Internal")]
private static extern int AddNumbers(int x, int y);
[DllImport("__Internal")]
private static extern string StringReturnValueFunction();
[DllImport("__Internal")]
private static extern void BindWebGLTexture(int texture);
void Start() {
Hello();
HelloString("This is a string.");
float[] myArray = new float[10];
PrintFloatArray(myArray, myArray.Length);
int result = AddNumbers(5, 7);
Debug.Log(result);
Debug.Log(StringReturnValueFunction());
var texture = new Texture2D(0, 0, TextureFormat.ARGB32, false);
BindWebGLTexture(texture.GetNativeTextureID());
}
}
Step 6
In my Unity project, I drag that C# script and place it on the camera (because that is a game object that is active at the beginning of the game).
Step 7
I attempt to play the game (Play button in the Unity Editor).
My Result
The console shows the following error: EntryPointNotFoundException: Hello cs_js_test.cs.Start() (at Assets/Scripts/cs_js_test.cs:27)
Line 27, where the error is, is the first call to a function, the Hello() function in the JavaScript library file.
My question is this: I don’t see how the C# script is supposed to “know” that there is a JavaScript library named js_test.jslib, even if it is in the Plugins folder. In the C# script are calls to import a dynamic link library, such as at line 7 - [DllImport(“__Internal”)] , however I don’t see any way to reference my exact .jslib file, which is named js_test.jslib.
Surely that is the reason for the “I can’t find the function named "Hello()” error I’m getting, because it doesn’t know to look in that file. Shouldn’t there be something in the C# script that says “go to Assets > Plugins and open the file named js_test.jslib?”