Sonali Gupta
Sonali Gupta

Reputation: 308

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?

Upvotes: 2

Views: 3339

Answers (2)

Tengku Fathullah
Tengku Fathullah

Reputation: 1370

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

Upvotes: 2

Programmer
Programmer

Reputation: 125445

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...

Upvotes: 0

Related Questions