Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
3
Question by hydrix · May 09, 2018 at 02:39 PM · webgl

jslib is passing string as number

When trying to use the jslib as documented here https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html my function with all string parameters is being passed as number values. The console.log in the web browser shows that url and url2 are numbers, when they should be strings.

Code:

 //jslib:
 
  var websocketJSLib = {
 
     wss : null,
 
 
     StartMMJS : function(url, url2) {
 
         console.log("js connecting to " + url2);
         console.log(url2);
         console.log(typeof(url2));
 
         console.log("2 js connecting to " + url2.toString() + " --" + String(url2));
         console.log(url2);
         console.log(typeof(url2));
 
      
         wss = new WebSocket(url);
 
     },
 
 }
 mergeInto(LibraryManager.library, websocketJSLib);
 



 //cs code:
 
 public class test : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         Debug.LogWarning("4 cs booting");
         StartMM("ws://mm.myserver.com/MM");
     }
 
 
     [DllImport("__Internal")]
     private static extern void StartMMJS(string url, string url2);
 
     public void StartMM(string server)
     {
         Debug.LogWarning("4 cs connecting to " + server);
 
         StartMMJS("ws://mm.myserver.com/MM", server);
 
      }
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by Bunny83 · Jul 23, 2018 at 09:25 AM

Just came along this question. You said you implemented your jslib like documented, but you actually didn't. Your "managed" C# code actually runs in a virtual machine that is implemented in javascript. However it has it's own memory management. In the documentation there's an example of passing a string argument to a js function:

 HelloString: function (str) {
     window.alert(Pointer_stringify(str));
 },

That means a managed string (which is a managed object in the managed heap) has to be converted to a js string using Pointer_stringify(str)


Note that returning a string to C# / Mono is actually a bit more complex as you have to allocate the memory in the managed heap first. Though there's also an example in the docs.

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 drlecks1 · May 09 at 11:09 AM 0
Share

This reply saved me ;) . Now Pointer_stringify(str); is obsolete, UTF8ToString(str); is the replacement.

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

86 People are following this question.

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

Related Questions

How to upload WebGL games on reputed websites? 1 Answer

Webgl facebook integration 0 Answers

WebGL -Chrome seems to do nothing while loading WebGL appllication 0 Answers

webgl rotate texture on the face of cube using glmatrix 0 Answers

Cloud Built WebGL Stopped working 0 Answers


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