diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index fea9965..6baf0cf 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -255,6 +255,7 @@ public void ResetStates(string[] keysToExclude = null, Action OnStatesReset = nu public void ResetPlayersStates(string[] keysToExclude = null, Action OnStatesReset = null) { _playroomService.ResetPlayersStates(keysToExclude, OnStatesReset); + } // Joystick diff --git a/Assets/PlayroomKit/dependencies/UnityBrowserBridge/Scripts/UnityBrowserBridge.cs b/Assets/PlayroomKit/dependencies/UnityBrowserBridge/Scripts/UnityBrowserBridge.cs index aac211a..31392aa 100644 --- a/Assets/PlayroomKit/dependencies/UnityBrowserBridge/Scripts/UnityBrowserBridge.cs +++ b/Assets/PlayroomKit/dependencies/UnityBrowserBridge/Scripts/UnityBrowserBridge.cs @@ -363,6 +363,8 @@ public void CallJs(string jsFunctionName, string callbackName = null, string gam { List allParams = new(); + Debug.LogWarning(args); + foreach (var param in args) { if (param.StartsWith("{") && param.EndsWith("}")) @@ -401,6 +403,7 @@ public T CallJs(string jsFunctionName, string callbackName = null, string gam string jsCall = $"{jsFunctionName}({string.Join(", ", allParams)})"; if (isAsync) jsCall = $"await {jsCall}"; + Debug.Log(jsCall); return ExecuteJS(jsCall); } diff --git a/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockPlayerService.cs b/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockPlayerService.cs index 270e8b4..155c56e 100644 --- a/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockPlayerService.cs +++ b/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockPlayerService.cs @@ -43,14 +43,45 @@ public void SetState(string key, string value, bool reliable = false) public void SetState(string key, object value, bool reliable = false) { - _ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, value.ToString(), + string json; + + // Check if the value is a primitive type and wrap it if necessary + json = JsonUtility.ToJson(value is int or float or bool or string + ? new PrimitiveWrapper(value) + : value); + + _ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, json, reliable.ToString().ToLower()); } public T GetState(string key) { - Debug.Log(key); - return _ubb.CallJs(key); + string stringValue = _ubb.CallJs("GetPlayerStateByPlayerId", null, null, false, _id, key); + + if (string.IsNullOrEmpty(stringValue)) + { + Debug.LogWarning($"State for key '{key}' is null or empty."); + return default; + } + + try + { + if (typeof(T) == typeof(int) || typeof(T) == typeof(float) || typeof(T) == typeof(bool) || + typeof(T) == typeof(string)) + { + var wrapper = JsonUtility.FromJson>(stringValue); + return wrapper.value; + } + else + { + return JsonUtility.FromJson(stringValue); + } + } + catch (Exception e) + { + Debug.LogError($"Failed to parse JSON for key '{key}': {e.Message}"); + return default; // Return default if parsing fails. + } } #endregion @@ -60,7 +91,7 @@ public PlayroomKit.Player.Profile GetProfile() string json = _ubb.CallJs("GetProfile", null, null, false, _id); Debug.Log(json); - + var profileData = Helpers.ParseProfile(json); return profileData; } @@ -80,5 +111,17 @@ public void WaitForState(string stateKey, Action onStateSetCallback = nu { _ubb.CallJs("WaitForPlayerState", null, null, true, _id, stateKey); } + + + #region UTILS + + [System.Serializable] + private class PrimitiveWrapper + { + public T value; + public PrimitiveWrapper(T value) => this.value = value; + } + + #endregion } } \ No newline at end of file diff --git a/Assets/PlayroomKit/modules/MockMode/BrowserMode/PlayroomMockBrowserBridge.js b/Assets/PlayroomKit/modules/MockMode/BrowserMode/PlayroomMockBrowserBridge.js index 1d9641c..d6adff0 100644 --- a/Assets/PlayroomKit/modules/MockMode/BrowserMode/PlayroomMockBrowserBridge.js +++ b/Assets/PlayroomKit/modules/MockMode/BrowserMode/PlayroomMockBrowserBridge.js @@ -3,9 +3,13 @@ unityInstance.SendMessage(gameObjectName, onLaunchCallBackName); } + console.log( + "InsertCoin called", + options, + onLaunchCallBackName, + gameObjectName + ); - console.log("InsertCoin called", options, onLaunchCallBackName, gameObjectName); - await Playroom.insertCoin(options, onLaunchCallBack); }; @@ -177,7 +181,16 @@ WaitForPlayerState = async function (playerId, stateKey, onStateSetCallback) { return null; } - await Playroom.waitForPlayerState(playerState, stateKey, onStateSetCallback); + await Playroom.waitForPlayerState().then((stateVal) => { + // playerState, stateKey, onStateSetCallback + const data = { + key: onStateSetCallback, + parameter: stateVal, + }; + + const jsonData = JSON.stringify(data); + unityInstance.SendMessage("CallbackManager", "InvokeCallback", jsonData); + }); }; Kick = async function (playerID) { diff --git a/Assets/PlayroomKit/modules/MockMode/CallBacksManagerMock.cs b/Assets/PlayroomKit/modules/MockMode/CallBacksManagerMock.cs index 135fe98..99a9ae9 100644 --- a/Assets/PlayroomKit/modules/MockMode/CallBacksManagerMock.cs +++ b/Assets/PlayroomKit/modules/MockMode/CallBacksManagerMock.cs @@ -73,8 +73,6 @@ public void HandleRPC(string jsonData) public void InvokeCallback(string jsonData) { - - var jsonNode = JSON.Parse(jsonData); string key = jsonNode["key"];