Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jdecker22 · Jul 09, 2015 at 09:25 AM · javascriptwebgl

WebGL + JSLIB Plugin, using functions

I've written a .jslib file in my /Assets/Plugins/WebGL folder that I am successfully calling back and forth with my C# code (using [DllImport ("__Internal")] in C# and SendMessage in JS). The only problem I'm running into is I have not found a way that I can add a function to my Javascript object that I can call from another JS function.

Ex.

 var WebInteraction = {
 
     func1: function()
     {
         console.log("do func1 stuff");    
             func3();
     },
     
     func2: function(str)
     {
         console.log("do func2 stuff str=" + str);
             func3();
     },
     
     func3: function()
         {
             console.log("do func3 stuff");
         }
 };
 
 mergeInto(LibraryManager.library, WebInteraction);
 

Code similar to the above example is giving me a func3 doesn't exist error. I also looked in the .js file that Unity outputs and confirmed func3 was not exported. I do see func1 and func2 as _func1() and _func2().

Is there a way to add functions into the jslib that are not intended to be called from the C# code?

I've also tried calling the function like WebInteraction.func3(), as well as moving the function out of the WebInteraction object, but both did not get the compiler to export the func3().

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Roy Sharon · Sep 23, 2015 at 11:54 AM

You need to add dependencies declaration so emscripten does not strip out func3:

 func1__deps: ['func3'],
 func2__deps: ['func3']

There's a tool that can do this for you automatically: https://www.npmjs.com/package/emscripten-library-generator

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RazaTech · Dec 06, 2015 at 03:31 PM 0
Share

@ Roy Sharon i m making an openGl project , in which user have to open file dialog and manipulate some work on it , but unity does not provide any builtin way to do it ,

found this link webgl-interactingwithbrowserscripting could not found any way to declare veritable in jslib , is there any document to study this ...?

avatar image Roy Sharon RazaTech · Dec 07, 2015 at 06:17 PM 0
Share
 mergeInto(Library$$anonymous$$anager.library, {
     foo: 'some arbitrary value',
     printFoo__deps: ['foo'],
     printFoo: function() {
         console.log(_foo); // notice that foo is preceded with an underscore
     }
 });
avatar image
0

Answer by sonxoans2 · Sep 14, 2016 at 12:34 PM

When i use js plugin run by webgl with https. I got error: "Uncaught ReferenceError: SocketSend is not defined"

Jslib: var LibraryWebSockets = { $webSocketInstances: [],

 SocketCreate: function(url)
 {
     var str = Pointer_stringify(url);
     var socket = {
         socket: new WebSocket(str),
         buffer: new Uint8Array(0),
         error: null,
         messages: []
     }
 
     socket.socket.binaryType = 'arraybuffer';
 
     socket.socket.onmessage = function (e) {
         // Todo: handle other data types?
         if (e.data instanceof Blob)
         {
             var reader = new FileReader();
             reader.addEventListener("loadend", function() {
                 var array = new Uint8Array(reader.result);
                 socket.messages.push(array);
             });
             reader.readAsArrayBuffer(e.data);
         }
         else if (e.data instanceof ArrayBuffer)
         {
             var array = new Uint8Array(e.data);
             socket.messages.push(array);
         }
     };
 
     socket.socket.onclose = function (e) {
         if (e.code != 1000)
         {
             if (e.reason != null && e.reason.length > 0)
                 socket.error = e.reason;
             else
             {
                 switch (e.code)
                 {
                     case 1001: 
                         socket.error = "Endpoint going away.";
                         break;
                     case 1002: 
                         socket.error = "Protocol error.";
                         break;
                     case 1003: 
                         socket.error = "Unsupported message.";
                         break;
                     case 1005: 
                         socket.error = "No status.";
                         break;
                     case 1006: 
                         socket.error = "Abnormal disconnection.";
                         break;
                     case 1009: 
                         socket.error = "Data frame too large.";
                         break;
                     default:
                         socket.error = "Error "+e.code;
                 }
             }
         }
     }
     var instance = webSocketInstances.push(socket) - 1;
     
     return instance;
 },
 
 SocketState: function (socketInstance)
 {
     var socket = webSocketInstances[socketInstance];
     return socket.socket.readyState;
 },
 
 SocketError: function (socketInstance, ptr, bufsize)
 {
      var socket = webSocketInstances[socketInstance];
      if (socket.error == null)
          return 0;
     var str = socket.error.slice(0, Math.max(0, bufsize - 1));
     writeStringToMemory(str, ptr, false);
     return 1;
 },
 
 SocketSend: function (socketInstance, ptr, length)
 {
     //alert('start SocketSend');
     //var socket = webSocketInstances[socketInstance];
     //socket.socket.send (HEAPU8.buffer.slice(ptr, ptr+length));
     var socket = webSocketInstances[socketInstance];
     if (socket.socket.readyState === 1) {
         socket.socket.send (HEAPU8.buffer.slice(ptr, ptr+length));
     }else{
         //var m_that = this;
         // optional: implement backoff for interval here
         setTimeout(function () {SocketSend(socketInstance, ptr, length);}, 1000);
     }
 },
 
 SocketRecvLength: function(socketInstance)
 {
     var socket = webSocketInstances[socketInstance];
     if (socket.messages.length == 0)
         return 0;
     return socket.messages[0].length;
 },
 
 SocketRecv: function (socketInstance, ptr, length)
 {
     var socket = webSocketInstances[socketInstance];
     if (socket.messages.length == 0)
         return 0;
     if (socket.messages[0].length > length)
         return 0;
     HEAPU8.set(socket.messages[0], ptr);
     socket.messages = socket.messages.slice(1);
 },
 
 SocketClose: function (socketInstance)
 {
     var socket = webSocketInstances[socketInstance];
     socket.socket.close();
 },
 
 SocketAlert: function(msg){
     alert(msg);
 }
 };
 
 autoAddDeps(LibraryWebSockets, '$webSocketInstances');
 mergeInto(LibraryManager.library, LibraryWebSockets);
 

somebody tell me how to fix this, please...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Salamandr · Oct 02, 2017 at 03:56 AM 0
Share

sonxoans2, just change memory size to 256 (default). If you increase value -> you see this bug

avatar image
0

Answer by DanielSafs · Jan 17, 2019 at 02:47 PM

" I also looked in the .js file that Unity outputs "

What is this file name? Although my functions are working fine. I tried to find my functions in UnityLoader.js but had no success.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iSinner · Jan 30, 2019 at 01:13 PM 0
Share

If you are using chrome, in the dev tools. in the source tab, you could ctrl+shift+F to search in all sources, and try to find the function name you are looking for. It worked for me.

In my case the function was defined in a blob:, where url was the address from where i loaded my webgl game.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't connect to Web socket, connection refused Unity WebGL 0 Answers

WebGL builds: decipher jsStackTrace errors with symbol map? 1 Answer

Browser interaction to Unity 0 Answers

Unity WebGL Problem 0 Answers

ReferenceError: Call is not defined 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges