Skip to content

Commit

Permalink
Merge pull request #49 from asadm/ErrorPassing
Browse files Browse the repository at this point in the history
fix: passing error to unity.
  • Loading branch information
momintlh authored Apr 18, 2024
2 parents c288eb8 + 97a41e5 commit 70a7969
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
19 changes: 16 additions & 3 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ public class MatchMakingOptions
private static Action OnDisconnectCallback = null;

[DllImport("__Internal")]
private static extern void InsertCoinInternal(string options, Action onLaunchCallback, Action<string> onQuitInternalCallback, Action onDisconnectCallback);
private static extern void InsertCoinInternal(
string options,
Action onLaunchCallback,
Action<string> onQuitInternalCallback,
Action onDisconnectCallback,
Action<string> onError);

[MonoPInvokeCallback(typeof(Action))]
private static void InvokeInsertCoin()
Expand All @@ -73,7 +78,15 @@ private static void InvokeInsertCoin()

}

// optional InitOptions
[MonoPInvokeCallback(typeof(Action<string>))]
private static void InvokeOnErrorInsertCoin(string error)
{
onError?.Invoke(error);
Debug.LogException(new Exception(error));
}

private static Action<string> onError;

public static void InsertCoin(InitOptions options = null, Action onLaunchCallBack = null, Action onDisconnectCallback = null)
{
if (IsRunningInBrowser())
Expand All @@ -95,7 +108,7 @@ public static void InsertCoin(InitOptions options = null, Action onLaunchCallBac
#endif
}

InsertCoinInternal(optionsJson, InvokeInsertCoin, __OnQuitInternalHandler, onDisconnectCallbackHandler);
InsertCoinInternal(optionsJson, InvokeInsertCoin, __OnQuitInternalHandler, onDisconnectCallbackHandler, InvokeOnErrorInsertCoin);
}
else
{
Expand Down
11 changes: 8 additions & 3 deletions Assets/PlayroomKit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ mergeInto(LibraryManager.library, {
optionsJson,
onLaunchCallBack,
onQuitInternalCallback,
onDisconnectCallback
onDisconnectCallback,
onError
) {

function OnLaunchCallBack() {
Expand All @@ -19,7 +20,7 @@ mergeInto(LibraryManager.library, {
dynCall("v", onDisconnectCallback, []);
}

var options = optionsJson ? JSON.parse(UTF8ToString(optionsJson)) : {};
var options = optionsJson ? JSON.parse(UTF8ToString(optionsJson)) : {};

if (!window.Playroom) {
console.error(
Expand All @@ -43,7 +44,11 @@ mergeInto(LibraryManager.library, {

})
.catch((error) => {
console.error("Error inserting coin:", error);
var jsonString = JSON.stringify(error);
var bufferSize = lengthBytesUTF8(jsonString) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(jsonString, buffer, bufferSize);
dynCall("vi", onError, [buffer]);
});
},

Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class GameManager : MonoBehaviour

private void Awake()
{

PlayroomKit.InsertCoin(new PlayroomKit.InitOptions()
{
maxPlayersPerRoom = 2,
Expand Down

0 comments on commit 70a7969

Please sign in to comment.