From 85d300ae19280ebe417916f6073c6779087d0e8f Mon Sep 17 00:00:00 2001
From: momintlh <77355191+momintlh@users.noreply.github.com>
Date: Fri, 13 Dec 2024 16:37:02 +0500
Subject: [PATCH] fix: json parsing in browser mode
---
.../2d-platformer/scripts/GameManager2d.cs | 54 +++++++++++--------
.../BrowserMode/BrowserMockPlayerService.cs | 31 +++++++----
ProjectSettings/EditorBuildSettings.asset | 4 +-
ProjectSettings/ProjectSettings.asset | 2 +-
4 files changed, 57 insertions(+), 34 deletions(-)
diff --git a/Assets/PlayroomKit/Examples/2d-platformer/scripts/GameManager2d.cs b/Assets/PlayroomKit/Examples/2d-platformer/scripts/GameManager2d.cs
index a765c57..b72a75b 100644
--- a/Assets/PlayroomKit/Examples/2d-platformer/scripts/GameManager2d.cs
+++ b/Assets/PlayroomKit/Examples/2d-platformer/scripts/GameManager2d.cs
@@ -11,16 +11,19 @@
public class GameManager2d : MonoBehaviour
{
-
- [SerializeField] private GameObject playerPrefab;
+ [SerializeField]
+ private GameObject playerPrefab;
///
/// player scores and UI to display score of the game.
///
[Header("Score and UI")]
- [SerializeField] private int score = 0;
- [SerializeField] private TextMeshProUGUI scoreTextPlayer1;
- [SerializeField] private TextMeshProUGUI scoreTextPlayer2;
+ [SerializeField]
+ private int score = 0;
+ [SerializeField]
+ private TextMeshProUGUI scoreTextPlayer1;
+ [SerializeField]
+ private TextMeshProUGUI scoreTextPlayer2;
private TextMeshProUGUI selectedScoreText;
@@ -49,10 +52,10 @@ private void Initialize()
_playroomKit.InsertCoin(new InitOptions()
{
maxPlayersPerRoom = 2,
- defaultPlayerStates = new() {
- {"score", 0},
- },
-
+ defaultPlayerStates = new()
+ {
+ { "score", 0 },
+ },
}, () =>
{
_playroomKit.OnPlayerJoin(AddPlayer);
@@ -92,7 +95,6 @@ void HandleScoreUpdate(string data, string caller)
{
Debug.LogError($"No GameObject found for caller: {caller}");
}
-
}
///
@@ -110,25 +112,36 @@ private void Update()
players[index].SetState("pos", playerGameObjects[index].transform.position);
+ players[index].SetState("a", "HELLO WORLD");
+ players[index].SetState("d", 11);
+ players[index].SetState("b", 9.81f);
+ players[index].SetState("c", false);
+
ShootBullet(index);
for (var i = 0; i < players.Count; i++)
{
if (players[i] != null && PlayerDict.TryGetValue(players[i].id, out GameObject playerObj))
{
- Debug.Log("Getting state of: " + players[i].id);
+ // Debug.Log("Getting state of: " + players[i].id);
var pos = players[i].GetState("pos");
-
- Debug.Log(pos);
-
+
+ string a = players[i].GetState("a");
+ float b = players[i].GetState("b");
+ bool c = players[i].GetState("c");
+ int d = players[i].GetState("d");
+
+ Debug.Log($"Player {i} state: a: {a}, b: {b}, c: {c}, d: {d}");
+ Debug.Log($"a type: {a.GetType()}, b type: {b.GetType()}, c type: {c.GetType()}, d type: {d.GetType()}");
+
+
var color = players[i].GetState("color");
if (playerGameObjects != null)
{
playerGameObjects[i].GetComponent().position = pos;
-
+
playerGameObjects[i].GetComponent().color = color;
}
-
}
}
}
@@ -144,12 +157,11 @@ private void ShootBullet(int playerIndex)
{
Vector3 playerPosition = playerGameObjects[playerIndex].transform.position;
- score = playerGameObjects[playerIndex].GetComponent().ShootBullet(playerPosition, 50f, score);
+ score = playerGameObjects[playerIndex].GetComponent()
+ .ShootBullet(playerPosition, 50f, score);
- _playroomKit.RpcCall("ShootBullet", score, PlayroomKit.RpcMode.ALL, () =>
- {
- Debug.Log("Shooting bullet");
- });
+ _playroomKit.RpcCall("ShootBullet", score, PlayroomKit.RpcMode.ALL,
+ () => { Debug.Log("Shooting bullet"); });
}
}
diff --git a/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockPlayerService.cs b/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockPlayerService.cs
index c9ed84a..b83ce8a 100644
--- a/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockPlayerService.cs
+++ b/Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockPlayerService.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using UBB;
using UnityEngine;
+using SimpleJSON;
namespace Playroom
{
@@ -58,9 +59,9 @@ public void SetState(string key, object value, bool reliable = false)
public T GetState(string key)
{
- string stringValue = _ubb.CallJs("GetPlayerStateByPlayerId", null, null, false, _id, key);
+ string rawValue = _ubb.CallJs("GetPlayerStateByPlayerId", null, null, false, _id, key);
- if (string.IsNullOrEmpty(stringValue))
+ if (string.IsNullOrEmpty(rawValue))
{
Debug.LogWarning($"State for key '{key}' is null or empty.");
return default;
@@ -68,21 +69,31 @@ public T GetState(string key)
try
{
- if (typeof(T) == typeof(int) || typeof(T) == typeof(float) || typeof(T) == typeof(bool) ||
- typeof(T) == typeof(string))
+ var jsonNode = JSON.Parse(rawValue);
+
+ if (typeof(T) == typeof(string))
{
- var wrapper = JsonUtility.FromJson>(stringValue);
- return wrapper.value;
+ return (T)(object)jsonNode.Value;
}
- else
+ if (typeof(T) == typeof(int))
{
- return JsonUtility.FromJson(stringValue);
+ return (T)(object)jsonNode.AsInt;
}
+ if (typeof(T) == typeof(float))
+ {
+ return (T)(object)jsonNode.AsFloat;
+ }
+ if (typeof(T) == typeof(bool))
+ {
+ return (T)(object)jsonNode.AsBool;
+ }
+
+ return JsonUtility.FromJson(rawValue);
}
catch (Exception e)
{
- Debug.LogError($"Failed to parse JSON for key '{key}': {e.Message}");
- return default; // Return default if parsing fails.
+ Debug.LogError($"Failed to parse state for key '{key}': {e.Message}\nReceived value: {rawValue}");
+ return default;
}
}
diff --git a/ProjectSettings/EditorBuildSettings.asset b/ProjectSettings/EditorBuildSettings.asset
index d521744..8fde211 100644
--- a/ProjectSettings/EditorBuildSettings.asset
+++ b/ProjectSettings/EditorBuildSettings.asset
@@ -5,10 +5,10 @@ EditorBuildSettings:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Scenes:
- - enabled: 0
+ - enabled: 1
path: Assets/PlayroomKit/Examples/2d-platformer/2d-platformer.unity
guid: cf82eec4c654cf44b89cec2a91afebd5
- - enabled: 1
+ - enabled: 0
path: Assets/PlayroomKit/Examples/discord-activity/discord-activity.unity
guid: bb7ef8e4a67f3fb46b78e5d95a6858a7
- enabled: 0
diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset
index 71d6a92..2f81a5e 100644
--- a/ProjectSettings/ProjectSettings.asset
+++ b/ProjectSettings/ProjectSettings.asset
@@ -675,7 +675,7 @@ PlayerSettings:
webGLDebugSymbols: 0
webGLEmscriptenArgs:
webGLModulesDirectory:
- webGLTemplate: APPLICATION:Default
+ webGLTemplate: PROJECT:DiscordTemplate
webGLAnalyzeBuildSize: 0
webGLUseEmbeddedResources: 0
webGLCompressionFormat: 2