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
0
Question by msknapp · Oct 05, 2011 at 07:16 PM · browsercallbackcommunicationembedunityobject

UnityObject callback function never runs

I'm trying to make my browser send a message to the unity game, I already got this to work when the user clicks a button on the web page, but my goal is for the function to execute as soon as the unity game has finished loading in the browser. I tried using the "callback" parameter of the embedUnity function, but it never executes. I have been following the example found here:

http://unity3d.com/support/documentation/Manual/Working%20with%20UnityObject.html

my web page (abridged) javascript looks similar to this:

 if (typeof unityObject != "undefined") {
             unityObject.embedUnity("unityPlayer", "Example.unity3d", 600, 450, null, null, onUnityLoaded);
         }
         function onUnityLoaded(result) {
             if (result.success) {
                 setVariables();
     alert("Successfully reached callback and set variables.");
             }
             else {
                 alert("Unity failed to load game.");
             }
         }
 
 function setVariables() {
     getUnityObject().SendMessage("Main Camera","setVariable","myArgument");
 }

In my web page there is a button which calls the setVariables function when it is clicked, and that works. I also tried using UnityObject's "AddLoadEvent" and "AddDomLoadEvent", neither of those worked. I tried calling my "setVariables" function from the body "onLoad" event handler, but that does not work because the page finishes loading before the unity game is loaded. So the function "setVariables" was executed before the unity game was ready. So my question is:

Why is the callback function not working? How can I make it work?

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 msknapp · Oct 05, 2011 at 07:22 PM 0
Share

just for clarification, the "onUnityLoaded" function is never reached. Neither of the alert messages that function contains appear when I go to the page.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Oct 05, 2011 at 07:54 PM

Javascript or JScript is an immediate dynamic language, it isn't compiled. So when the browser reaches the embed line, onUnityLoaded is undefined because you create that function after that line. I guess that's your problem ;)

Just move the function in front of your embed line or use the just-in-place-method like in the example ;)

Comment
Add comment · Show 2 · 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 msknapp · Oct 06, 2011 at 02:48 PM 0
Share

I have tried both of your suggestions, neither worked. I found out that the callback function is being executed, but it runs immediately ins$$anonymous$$d of waiting for the unity player to finish loading. I think this is a bug in UnityObject.

avatar image Bunny83 · Oct 07, 2011 at 12:40 PM 0
Share

Finish loading? If you expect it should run when the player has finished loading your scene / game then you're wrong ;)

It's an automatic callback of the player itself which fires when the player is loaded / started. Your game / content is streamed and during the strea$$anonymous$$g the player is already loaded / started.

If you want an event in JScript that fires when your scene is loaded, do it yourself from Unity via browser communication

avatar image
0

Answer by msknapp · Oct 06, 2011 at 02:51 PM

I think the workaround for this problem is to have the unity application notify the web page when it is done loading. The callback function always runs before the unity player has finished loading.

Comment
Add comment · Show 1 · 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 msknapp · Oct 06, 2011 at 03:12 PM 0
Share

In my unity application I added this line to a start function:

Application.ExternalCall("onUnityLoaded","");

and in my web page I wrote a function for "onUnityLoaded" that will configure the unity game, similar to the one in my question.

This has fixed my problem.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

communication between unity and browser using webRTC 0 Answers

WebPlayer embedding question 1 Answer

ExternalCall in WebGL not working 0 Answers

Browser to unity communication 2 Answers

Embed Web Browser into unity 4 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