-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fixed sending and converting callback keys
- Loading branch information
Showing
5 changed files
with
103 additions
and
119 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.InteropServices; | ||
using UnityEngine; | ||
|
||
namespace Playroom | ||
{ | ||
public static class CallbackManager | ||
{ | ||
private static Dictionary<string, Delegate> callbacks = new(); | ||
|
||
public static string RegisterCallback(Action callback, string key = null) | ||
{ | ||
if (string.IsNullOrEmpty(key)) | ||
key = GenerateKey(); | ||
|
||
|
||
if (!callbacks.ContainsKey(key)) | ||
{ | ||
callbacks.Add(key, callback); | ||
} | ||
else | ||
{ | ||
callbacks[key] = callback; | ||
} | ||
|
||
return key; | ||
} | ||
|
||
|
||
public static void InvokeCallback(string key) | ||
{ | ||
if (callbacks.TryGetValue(key, out Delegate callback)) | ||
{ | ||
(callback as Action)?.Invoke(); | ||
} | ||
else | ||
{ | ||
Debug.LogError($"Callback with key {key} not found!"); | ||
} | ||
} | ||
|
||
private static string GenerateKey() | ||
{ | ||
return Guid.NewGuid().ToString(); | ||
} | ||
|
||
|
||
|
||
/// <summary> | ||
/// Calls an external method to convert a string associated with a given key, called from the JS side only. | ||
/// </summary> | ||
/// <param name="key">The key associated with the string to be converted.</param> | ||
[DllImport("__Internal")] | ||
private static extern void ConvertString(string key); | ||
|
||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters