Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by jakejolli · Jul 11, 2016 at 03:13 PM · webglbrowser communication

Easily getting variable values from WebGL in Browser

I'm developing some 3D interactions in Unity, and they will be embedded in a webpage which will have other interactive components. I'm going to need to communicate between these other components, and I'm wondering if there's an easy way to return a value from Unity into JS variables in the browser.

I know I can use SendMessage() to call functions in Unity from external JS, but can I return values from these calls?

I don't know how to efficiently do this, so the solution I'm thinking of right now looks like this:

Browser:

    var health;
    var exp;
         
         function GetHealth(){
             SendMessage("Player", "GetHealth");
         }
         
         function ReturnValue(varName, val){
            if(varName == "health"){
               health = val;
            }
     
        if(varName == "exp"){
               exp = val;
            }
 
         //etc.

         }

Unity:

 public void GetHealth(){
     Application.ExternalCall("ReturnValue", "health", health); 
 }

This is a really ugly solution that would be ripe with problems. Ideally, I'd like to do something like

Browser:

 function GetHealth(){
    return SendMessage("Player", "GetHealth");
 }

Unity:

 public int GetHealth(){
    return health;
 }

Any ideas?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image elenzil · Oct 31, 2017 at 11:37 PM 0
Share

i'm also interested in doing this.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by elenzil · Nov 01, 2017 at 04:53 PM

this thread in the forums seems pretty relevant.

edit actually it provides a demo of the requested functionality, plus an approach w/ significantly lower runtime overhead than SendMessage, altho it's only significant if you're calling into C# several thousands of times per frame, imo.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Bunny83 · Oct 31, 2017 at 11:50 PM

In the past we had methods like Application.ExternalEval or Application.ExternalCall. However it seems they got deprecated. Instead you should use a "jslib" plugin and use external method definitions in C#. According to the "AddNumbers" example you should be able to return values.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image elenzil · Nov 01, 2017 at 04:40 PM 1
Share

Hi Bunny - thanks for your answer here, i always read your answers carefully since they're pretty knowledgable.

yes, the jslib plugin approach works well for calling JavaScript from C# and passing values back to C#. I've also confirmed that you can call javascript functions hosted in the main page from within the plugin code, as well as access elements in the DO$$anonymous$$ from within the plugin code.

this question is actually about the reverse tho: calling C# from javascript and getting a return value. Send$$anonymous$$essage() works well for sending a string or number (or nothing) to Unity, but it has no provision for synchronously returning a value.

i'm hoping to look into this a bit more. there might be some way to use marshaling to pass a C# callback function to JS and then have JS call the callback. i have no evidence this is actually possible, but it seems like the only open avenue of approach, given the APIs at hand.

any thoughts greatly appreciated.

avatar image bravo075 elenzil · Jan 22, 2019 at 11:50 PM 0
Share

@elenzil I know this is old but I also need to retrieve a variable from Unity using javascript in the browser, were you able to achieve this?

avatar image elenzil bravo075 · Jan 22, 2019 at 11:56 PM 1
Share

check out the thread linked to in the other answer here. "this thread in the forums seems pretty relevant."

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

51 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

EntryPointNotFoundException when trying webgl browser communication jslib example 2 Answers

How to send multiple values from browser to unity webgl game? 1 Answer

Interact across browser javascipt and unity webgl 2 Answers

Check if Javascript function exists on webpage before calling ExternalCall 1 Answer

Open in link in lightbox/colorbox from webgl 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges