Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question .jslib safari functions only accepting strings as jsons?

Discussion in 'Web' started by unity_2D833335567EB140E14B, Oct 11, 2023.

  1. unity_2D833335567EB140E14B

    unity_2D833335567EB140E14B

    Joined:
    Jul 28, 2023
    Posts:
    2
    Using Unity 2022.3.1f1

    I was having an issue with calling the function "FBIG_GetUserDataJSON" in a .jslib file on Safari. When executed on Safari the string passed in the parameter 'keys' would be empty, but other browsers it was not. It took me a while to find a workaround for this. But converting the string to a JSON like
    Code (CSharp):
    1. string keysJson = $"{{ \"Keys\":\"{string.Join(",", keys)}\" }}";
    from
    Code (CSharp):
    1. string joinedKeys = string.Join(",", keys);
    and passing that seemed to allow the string to be read in Safari. I mean the code works now, but I want to understand if this is a known limitation on Safari, or if it is documented anywhere?

    // BROKEN CODE BELOW
    Code (CSharp):
    1. #if UNITY_WEBGL
    2. using AOT;
    3. using Cysharp.Threading.Tasks;
    4. using System;
    5. using System.Runtime.InteropServices;
    6. using UnityEngine;
    7.  
    8. namespace Web.Facebook.Player
    9. {
    10.     public abstract class GetPlayerDataJSON
    11.     {
    12.         [DllImport("__Internal")]
    13.         private static extern void FBIG_GetUserDataJSON(string joinedKeys, Action<string> cb);
    14.  
    15.         private static UniTaskCompletionSource<string> s_utcs;
    16.  
    17.         [MonoPInvokeCallback(typeof(Action<string>))]
    18.         private static void RunFuncCB(string val)
    19.         {
    20.             s_utcs.TrySetResult(val);
    21.         }
    22.  
    23.         public static async UniTask<string> Execute(string[] keys)
    24.         {
    25.             try
    26.             {
    27.                 string joinedKeys = string.Join(",", keys);
    28.                 s_utcs = new UniTaskCompletionSource<string>();
    29.                 FBIG_GetUserDataJSON(joinedKeys, RunFuncCB);
    30.                 string json = await s_utcs.Task;
    31.                 if (json == "{}") return null;
    32.                 return json;
    33.             }
    34.             catch (Exception e)
    35.             {
    36.                 Debug.LogException(e);
    37.             }
    38.             return null;
    39.         }
    40.     }
    41. }
    42. #endif
    Code (JavaScript):
    1. FBIG_GetUserDataJSON: async function (keys, cb) {
    2.     var keys = UTF8ToString(keys).split(',');
    3.     var data = await WebPlatform.Facebook.Player.getDataAsync(keys);
    4.     var json = JSON.stringify(data);
    5.     Module.dynCall_vi(cb, Module.JSStringToBuffer(json));
    6.   },
    // WORKING CODE BELOW

    Code (CSharp):
    1. #if UNITY_WEBGL
    2. using AOT;
    3. using Cysharp.Threading.Tasks;
    4. using System;
    5. using System.Runtime.InteropServices;
    6. using UnityEngine;
    7.  
    8. namespace Web.Facebook.Player
    9. {
    10.     public abstract class GetPlayerDataJSON
    11.     {
    12.         [DllImport("__Internal")]
    13.         private static extern void FBIG_GetUserDataJSON(string joinedKeys, Action<string> cb);
    14.  
    15.         private static UniTaskCompletionSource<string> s_utcs;
    16.  
    17.         [MonoPInvokeCallback(typeof(Action<string>))]
    18.         private static void RunFuncCB(string val)
    19.         {
    20.             s_utcs.TrySetResult(val);
    21.         }
    22.  
    23.         public static async UniTask<string> Execute(string[] keys)
    24.         {
    25.             try
    26.             {
    27.                 string keysJson = $"{{ \"Keys\":\"{string.Join(",", keys)}\" }}";
    28.                 s_utcs = new UniTaskCompletionSource<string>();
    29.                 FBIG_GetUserDataJSON(keysJson, RunFuncCB);
    30.                 string json = await s_utcs.Task;
    31.                 if (json == "{}") return null;
    32.                 return json;
    33.             }
    34.             catch (Exception e)
    35.             {
    36.                 Debug.LogException(e);
    37.             }
    38.             return null;
    39.         }
    40.     }
    41. }
    42. #endif
    Code (JavaScript):
    1. FBIG_GetUserDataJSON: async function (json, cb) {
    2.     var keys = JSON.parse(UTF8ToString(json)).Keys.split(',');
    3.     var data = await WebPlatform.Facebook.Player.getDataAsync(keys);
    4.     var stringifiedData = JSON.stringify(data);
    5.     Module.dynCall_vi(cb, Module.JSStringToBuffer(stringifiedData));
    6.   },
     
    Last edited: Oct 11, 2023