Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with Js Lib

Discussion in 'WebGL' started by Karandashov, Dec 24, 2015.

  1. Karandashov

    Karandashov

    Joined:
    Jan 3, 2015
    Posts:
    4
    I Create file with extension ".jslib":
    Code (JavaScript):
    1.  
    2. var CookieLib = {
    3.     GetCookies: function()
    4.     {
    5.         alert(document.cookie);
    6.         return document.cookie;
    7.     }
    8. };
    9.  
    10. mergeInto(LibraryManager.library, CookieLib);
    11.  
    Then i add DLLImport and call method:
    Code (CSharp):
    1.  
    2. [DllImport("__Internal")]
    3.     private static extern string GetCookies();
    4.  
    5. void Awake()
    6.     {
    7.         GetCookies();
    8.     }
    9.  
    I Have exception:
    What's wrong? How i can fix it? In WebGL Alert from GetCookies working.
    Sorry for my mistakes. English isn't my native english..
     
  2. diekeure

    diekeure

    Joined:
    Jan 25, 2013
    Posts:
    221
    Where did you place the jslib file?
     
  3. Karandashov

    Karandashov

    Joined:
    Jan 3, 2015
    Posts:
    4
    Assets/Plugins/WebGL/filename.jslib
    Example of documentation is also not working.
     
  4. diekeure

    diekeure

    Joined:
    Jan 25, 2013
    Posts:
    221
    You can't return document.cookie just like that, you should follow the manual blueprint:

    Code (csharp):
    1. StringReturnValueFunction: function()
    2. {
    3. var returnStr = "bla";
    4. var buffer = _malloc(lengthBytesUTF8(returnStr) + 1);
    5. writeStringToMemory(returnStr, buffer);
    6. return buffer;
    7. }
    But I don't think that solves the exception. I take it you have this exception in the browser, right? Because in the editor it would be normal of course.
     
  5. Karandashov

    Karandashov

    Joined:
    Jan 3, 2015
    Posts:
    4
    I have this error in editor and browser.
     
  6. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    As mentioned by alexvda, you will always get that exception in editor. JSLIB files only run in browser.

    Since you've also stated that the alert does run, that means that your not actually having a missing method exception at all when in browser.

    The issue is that you likely do not want to have your js lib return a string. below is how you would go about getting a string using string builder with p/invoke
    Code (JavaScript):
    1.  
    2. var CookieLib = {
    3.     GetCookies: function(buf)
    4.     {
    5.         alert(document.cookie);
    6.         stringToUTF16(document.cookie, buf);
    7.         return document.cookie.length;
    8.     }
    9. };
    10.  
    11. mergeInto(LibraryManager.library, CookieLib);
    12.  
    Code (CSharp):
    1.  
    2. [DllImport("__Internal",CharSet = CharSet.Unicode)]
    3.     private static extern int GetCookies([MarshalAs(UnmanagedType.LPWStr)]System.Text.StringBuilder buf);
    4.  
    5. void Awake()
    6.     {
    7.         System.Text.StringBuilder sb = new System.Text.StringBuilder() { Capacity = 500, };// 500 char max..
    8.         GetCookies(sb);
    9.         string value = sb.ToString();
    10.     }
    11.  
    see how that works (note that value, would be set to the string.) the marshalas attribute may or may not be required

    One last note, when you build the player for webgl, since this call is in Awake, you'll likely get the missing method exception while building which is (or should be) harmless. If you did want to avoid it though.. you would place the following around awake
    Code (csharp):
    1.  
    2. #if !UNITY_EDITOR && UNITY_WEBGL
    3. void Awake { .......... }
    4. #endif
    5.  
     
    Last edited: Dec 26, 2015