Support » Plugin: HoweScape Unity3d WebGL » Retrieve data

  • Resolved krghost

    (@krghost)


    Is there any way to retrieve data from the WebGL build to WordPress?
    I am creating a level editor and after the editing I wanted the value in WordPress to insert it into the database

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Author pthowe

    (@pthowe)

    Hello Krghost,

    I am not sure what you are trying to accomplish? When a Unity3D WebGL is running there are function calls which can interact with the browser and the WEB. Without knowing more about the environment and conditions I can not offer any suggestions.

    Thread Starter krghost

    (@krghost)

    I am new to using webgl on wordpress so sorry if the question seems naive.
    I wanted to know how to use function like sendMessage() here in this plugin.
    Is it callable? If yes, how?. Where do I write it?

    I am making a level editor where user will create a level for the game and then upload the level data to firebase database.
    Initially, I was thinking of retrieving the level data from unity webgl to the browser and then pushing it to the database as I need the user detail present on the website to push it into the database but I think there is no way to do so.

    So I was thinking of sending the user details to unity webgl from the browser (through a function like sendMessage()) and from unity I’ll push the game data into the database

    So is there any way to do either of those mentioned above
    I am using unity 2019.4

    Thank you for your quick reply

    Plugin Author pthowe

    (@pthowe)

    @krghost, I have done very little with Unity3D. I would be happy to work with your as someone to discuss ideas. I have done a couple of the demo’s. I have created one unique game which is just a simple maze game.
    Is your level Editor a separate Unity3D application from the game?
    Where are the levels from your game stored?
    If you can help me understand how levels work I can help you figure out how to use them with the plugin.

    Peter

    Thread Starter krghost

    (@krghost)

    Great

    So my level editor is a stand-alone (i.e a separate WebGL build apart from the game).
    The level will be like a chessboard where the level creator will put predefined NPCs on the squares. There will be a limited number of NPCs with id 1 to n. One of these NPC will be placed on each square on the board.
    That is the whole level.

    To add it into the database when the creator submits, I simply create a (2 x 2)JSON of the current board with the NPC id as a value and then push the JSON into the Level creator’s account.

    Now here, the Level JSON info is in the unity WebGL and the user info is on the web browser.
    I somehow have to bring them together and push them into the database.

    Your help would be greatly appreciated
    Thank you for your prompt reply

    Plugin Author pthowe

    (@pthowe)

    @krghost , Did a quick look at Unity3d. The thought that comes to mind if the levels can be stored as a JSON file you. You could use the UnityWebRequest to read/write the JSON file from the server and load the information into the game.

    I can see that there would be security issues with the write operations. It should be possible to write to the media folder.

    There are also REST API functions. For testing the REST API there is the a tool called POSTMAN. This is a great tool for testing how a REST API works, what you need to send and what gets sent back.

    If you have a Unity3d test of concept, and a JSON file which describes a level I would be happy to look at the design.

    Let me know what you need.
    Peter

    Thread Starter krghost

    (@krghost)

    @pthowe , so is there no way to get the browser and Unity3d to communicate directly? Rather than going through an API?
    I mean like “sendMessage()” function in JS

    • This reply was modified 11 months ago by krghost.
    Plugin Author pthowe

    (@pthowe)

    @krghost , Let me see what I can put together as a test case.

    Thread Starter krghost

    (@krghost)

    Hey @pthowe any update?
    any feedback (whether possible or not) will be highly appreciated.

    Thanks

    Plugin Author pthowe

    (@pthowe)

    @krghost ,
    My research keeps leading me back to something like:
    https://weeklyhow.com/unity-restful-api/
    https://hub.packtpub.com/using-rest-api-unity-part-1-what-rest-and-basic-queries/

    I do believe that some browser information can be extracted. What type of information are you trying to extract from the browser?

    I believe you can pass parameters into a Unity3D game.

    Let me know what you are trying to accomplish

    Pthowe

    Thread Starter krghost

    (@krghost)

    @pthowe the data I want to pass is simply a string that is the user Id of the user logged into the website.

    Plugin Author pthowe

    (@pthowe)

    @krghost , For the first part of the project. I am sure you are already aware,

    wp_get_current_user()->ID

    Will get you the current logged in user. The next part is to use the parameters which are passed to the Unity3d object as a parameter.

    with the plugin I created you have:
    [hs_unity3d_web_gl_game src=”Roll-a-ball” height=”500″ width=”600″ u3dver=”2019.4″]

    Nothing prevents you from adding an additional parameter such as:

    [hs_unity3d_web_gl_game src=”Roll-a-ball” height=”500″ width=”600″ u3dver=”2019.4″ User=wp_get_current_user()->ID]

    The trick I have not worked out is how do you deal with special symbols and spaces because this is part of a URL.

    I will have to go look for the code, to extract the parameter into a variable in the UNITY code.

    Look for update

    Thread Starter krghost

    (@krghost)

    @pthowe ok so you mean that sending it through shortcode might work?
    I’ll definitely give it a try.
    Please update if you find out something and I’ll do the same too.

    Thanks

    Plugin Author pthowe

    (@pthowe)

    Hello @krghost ,

    I know this is not quite what you are trying to accomplish but it is communication between browser and the Unit3D game using my plugin.

    Reference URL From Code Monkey:https://unitycodemonkey.com/video.php?v=3g0N__K7Wlo

    For this I created a Unity3D game. The game is very simple and based on the Code Monkey example above. I created a game object of a circle, 2 UI Text Elements, and a game object linked to a C# script.

    The code in the Script: is the same as in the Code Monkey

    The Code Monkey, has buttons which are in the HTML page that send information into Unity3D game.

    Now there is a name change in the game object used by the plugin. The HTML to add the buttons looks like:

    <div style="border: solid 1px black; width: 200px; height: 30px; margin: auto; text-align: center;" 
    onclick="gameInstance.SendMessage('JavaScriptHook','TintRed')">Red</div>

    The Code Monkey used “unityInstance” the plugin you need to use “gameInstance”.

    To put the HTML buttons on the game page I added the code below the tag which loads the game.

    <script> 
    	var testObject = { name: "John", age: 34};
    	var jsonString = JSON.stringify(testObject);
    </script>
    [hs_unity3d_web_gl_game src="LevelBuild" height="300" width="500" u3dver="2019.4"]
    
    <div style="border: solid 1px black; width: 200px; height: 30px; margin: auto; text-align: center;" onclick="gameInstance.SendMessage('JavaScriptHook','TintRed')">Red</div>

    Let me know if you have questions.

    Thanks
    Peter

    Plugin Author pthowe

    (@pthowe)

    @krghost ,

    I think I have worked it out. If you look at the online documentation
    https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

    1. In the Assets create a folder Plugins
    2. Create a file in the folder which has the extension “.jslib”
    3. This file will look like:

    mergeInto(LibraryManager.library, {
      ReturnUserID: function() {
    	return current_userID;
      },
    });

    4. Create a C# Script “AccessJavaScript”

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.Runtime.InteropServices;
    
    public class AccessJavaScript : MonoBehaviour
    {
        [DllImport("__Internal")]
        private static extern int ReturnUserID();
    
        void Start() 
        {
          int result = ReturnUserID();
          Debug.Log(result);
        }
    }
    

    Because this is accessing the browser JavaScript, you can no longer use the Unity3d Editor Play.
    In my test I created in the HTML page:

    
    <script>current_userID=1;current_userName=xxxx;</script>
    

    From a short code which extracted current user info and placed in the page.

    Hope this is helpful.
    Please post them.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Retrieve data’ is closed to new replies.