Good afternoon, dear colleagues. I'm trying to adapt a Unity WEBGL program for a some social network on the WEB. The social network has an API and integrated simply with Unity via JavaScript. It is possible to store user data to variables that will be stored on social network. I have written an interface on Unity that allows to interact with a social network, but there is a problem that I cannot solve. The Unity program uses the following code to get stored data:
public class PlayerPrefsMapProgressManager : IMapProgressManager
{
// some code
public int LoadLevelStarsCount(int level)
{
return PlayerPrefs.GetInt(GetLevelKey(level), 0);
}
}
I want to replace PlayerPrefsfunction on my own.
In my code, it consists of two parts, the first function sends a get request, then the social network API calls the call back function with the returned value.
bridge.JSStorageGet("scope", ResultStorageGet);
public void ResultStorageGet(string value)
{
Debug.Log("Got value: " + value);
}
When I try to replace in the source code, the callback function does not have time to return the value:
public class PlayerPrefsMapProgressManager : IMapProgressManager
{
// some code
string Data;
public JSBridgeController bridge;
public void ResultStorageGet(string value)
{
Data = value;
}
public int LoadLevelStarsCount(int level)
{
bridge.JSStorageSet("scope", "100");
// I thing should be delay here, ResultStorageGet finished later
return Data; // return empty value
}
}
How can I make a delay to wait for the call back function to be called, or how can I do it differently? Ideas, examples, links. I 've tried everything I can .
Full text of the interaction with the API of the site:
// Test.cs file ****************************************
public class Test : MonoBehaviour
{
public JSBridgeController bridge;
public void ClickStoradgeGet() // click some button
{
bridge.JSStorageGet("scope", ResultStorageGet);
}
public void ResultStorageGet(string value)
{
Debug.Log("Got value: " + value);
}
public void ClickStoradgeSet() // click some button
{
bridge.JSStorageSet("scope", "100");
}
}
//JSBridgeController.cs file ***************************************************
public class JSBridgeController : MonoBehaviour
{
[DllImport("__Internal")]
public static extern void _JSStorageSet(string key, string value);
[DllImport("__Internal")]
public static extern string _JSStorageGet(string key);
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void JSStorageSet(string key, string value)
{
#if !UNITY_EDITOR
_JSStorageSet(key, value);
#endif
}
public void JSStorageGet(string key, UnityAction<string> action)
{
#if !UNITY_EDITOR
// TODO : Вместо стринга сделать отдельную структуру, в которой будет отслеживаться ошибка
_actionStorageGet = action;
_JSStorageGet(key);
#endif
}
public UnityAction<string> _actionStorageGet;
}
// JSBridgeHandler.cs file ********************************************
public class JSBridgeHandler : MonoBehaviour
{
private JSBridgeController controller;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
controller = this.GetComponent<JSBridgeController>();
}
// Update is called once per frame
void Update()
{
}
public void ResultStorageGet(string result)
{
controller._actionStorageGet.Invoke(result);
}
}
// jsbridge.jslib file ******************************************************
mergeInto(LibraryManager.library, {
_JSStorageSet: function (str, value) {
// Deal with api
console.log(UTF8ToString(value));
},
_JSStorageGet: function (str) {
// Deal with api and callback
ss.SendMessage('JSBridge', 'ResultStorageGet', "5");
}
});
Thanks you very much for the reply. The int option looks better and simpler.
Faced with the following problem. The API code inside the _JS StorageGet function is executed asynchronously and fineshed after calling return value, i.e. _JS Storage Get returns an empty value.
How can I wait for the value to be received in the example, or any other solution?
_JSStorageGet: function (keyPtr, fallbackValue) {
// you first need to actually read them out!
var keyString = UTF8ToString(keyPtr);
// Deal with api
myBridge.send("StorageGet", {"keys": [keyString]}).then(data => {
if (data.keys[0].value) {
console.log(JSON.parse(data.keys[0].value));
var value = JSON.parse(data.keys[0].value);
return value ; // return too early, when value is not ready yet
} else
return fallbackValue;
}
Additional Information.
I think i don't explain my question correctly. Try again. I put console alert in code:
_JSStorageGet: function (keyPtr, fallbackValue) {
// you first need to actually read them out!
var keyString = UTF8ToString(keyPtr);
// Deal with api and callback
//return fallbackValue;
console.log('_JSStorageGet complete');
return 42;
}
public static int GetInt(string key, int fallback = 0)
{
int retval;
retval = _JSStorageGet(key, fallbackValue);
Debug.Log(" GetInt: " + retval );
return retval;
}
if run GetInt I got:
GetInt: 0
_JSStorageGet complete
I'm expecting
_JSStorageGet complete
GetInt: 42