Z
Z
ZerglingRMG2017-09-28 16:59:45
MySQL
ZerglingRMG, 2017-09-28 16:59:45

Unity 3d and OK JS SDK, API_callback is not defined, what am I doing wrong?

Hello!
We are translating the game into WebGL and launching it in Odnoklassniki. We build the game under WebGL, the index.html code is as follows:

<!DOCTYPE html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>GameTitle</title>
    <script type="text/javascript" src="//api.ok.ru/js/fapi5.js" defer="defer"></script>
    <script src="Build/UnityLoader.js"></script>
    <script>
      var gameInstance = UnityLoader.instantiate("gameContainer", "Build/game.json");
    </script>
  </head>
  <body>
    <div id="gameContainer" style="width: 690px; height: 920px; margin: auto"></div>
  </body>
</html>

In C# code I do the following:
Application.ExternalEval(
                "function API_callback(method, result, data) { }" +
                "FAPI.UI.showInvite(\"" + inviteText + "\");"
            );

After the call, I get an error like:
API_callback is not defined.
Where should the API_callback function be located?
Is it really necessary to download the fapi5.js file and add the missing function in it?
Thank you! :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Melentiev, 2019-06-25
@MaxxxNSK

CURRENT_DATE(),1 clmn

Z
ZerglingRMG, 2017-09-28
@ZerglingRMG

Problem solved. It was enough to replace

<script>
      var gameInstance = UnityLoader.instantiate("gameContainer", "Build/game.json");
    </script>

on the
<script>
      var gameInstance = UnityLoader.instantiate("gameContainer", "Build/game.json");

      function API_callback(method, result, data)
      {
        var str = "{ \"method\": \"" + method + "\", \"result\": \"" + result + "\", \"data\": \"" + data + "\",}";
        console.log(str);

        gameInstance.SendMessage(*gameObjectName*, *methodName*, str);
      }
    </script>

Then the result of the method execution will be passed to gameObjectName via methodName as a string parameter. Then this string can be treated as JSON and process the result of the request.
And in C# code "function API_callback(method, result, data) { }" does not make sense, it can be removed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question