Unity WebGL communicate with web page

I am noob at unity. So, I am trying my best to explain the problem where I am stuck and I have no clue how to approach. I have a Ruby on Rails website running.

I am building a small game on unity which is much like an inventory management system. But, the inventory is specific to a user and the user can make changes to it. The user sees the game as a part of his dashboard. What I want to do:

  1. The moment the user lands on his dashboard, the unity game should load with the signed in users inventory.
  2. For this, I believe the unity webgl needs to interact with my application and get the user.

Also,

  1. How can I test all this without having to build the application?

Answers


You can test user sign in/sign out and dashboard in the Editor without building to WebGl. It will work but you should always test your game for WebGl once in a while during development to make sure that what you are doing will work in the future. Without doing this, your final game may not work because you used unsupported API for WebGL. That's why you must test the game but no all the time, just once in a while.

For communication with your server, you should use the new Unity API UnityWebRequest instead of WWW. The WWW class will be removed this month or in the coming months. UnityWebRequest fully supports WebGL build. .Net classes are NOT supported in WebGL Build. So you can't use anything like HttpWebRequest or Sockets...


If you want to communicate directly with the browser, then

from WebGL to index.html - Application.ExternalCall("MyFunction", MyValue)

from index.html to WebGL - SendMessage("MyGameObject","MyFunction", MyValue)

So in your case the moment user land on his dashboard the unity should get something like id of user sign-in from browser which is need to call function from index.html and return it back to unity with the data you get from session.

WebGL

public void ReloadInventoryBaseOnUser()
{
    Application.ExternalCall("GetUserID");
}

index.html

function GetUserID(){
    //$.ajax here to get session id
    SendMessage("Manager","SetUserID", userID);
    focus();
}

WebGL

public void SetUserID(string ID){
    userID = ID;
    ReloadInventory(userID);
}

The session is server side thing, You can write an Http handler (that will share the session id if any) and return the value from there using $.ajax. so assuming that you have store the id into js variable userID


Need Your Help

R - order of legend ggplot

r ggplot2 legend stacked

I have the following data frame:

Wordpress Custom Fields

php wordpress woocommerce

I'm new to PHP, so this is confusing me a little. I'm using the Woocommerce plugin for Wordpress and I'm trying to add a custom field to display rental prices for certain products. However, not all