- Home /
Facebook external eval ?
Hi, I have a unity web game which uses a external .js plugin which I source in the unity html ...But facebook uses the .unity3d file directly and I am not able to source in the .js plugin or the html file...on the facebook developer page they say...
Injecting your own HTML/JS
You may wish to add HTML or Javascript code around the Unity object. You can do this using Unity Web Player's DOM mutation/JS execution functions, such as Application.ExternalEval().
Example
Add a div element to the top of the iframe containing the game, which might contain a header for cross-promotion or related uses.
string injection = "var headerElement = document.createElement('div');" + "headerElement.textContent = ('Check out our other great games: ...');" + "var body = document.getElementsByTagName("body")[0];" + "var insertionPoint = body.children[0]; " + "body.insertBefore(headerElement, insertionPoint);"; Application.ExternalEval(injection);
..I dont know how to or where to put in the code to get it work...here is what i put in the unity html file to reference the .js plugin...
Can you tell me how we can achieve this...reference the html and js plugin ? Please Help !
type="text/javascript" src="JAVAPLUGIN.js">
(the ( script>)start not getting typed..)
Answer by Vizionz · Aug 19, 2014 at 07:21 PM
Assuming you are using C# - Try creating the Javascript code in this format:
string injection = "var yourScript = document.createElement(\"script\");" +
"yourScript.type = \"text/javascript\";" +
"yourScript.src = \"//URLtoSource\"" +
"document.getElementsByTagName(\"body\")[0].appendChild(yourScript);";
Application.ExternalEval(injection);
For HTML this can be done the same way, but you need to select which tag add to in "var yourScript = document.createElement(\"script_-or-div-or-_header\");" as well as the .type and where you append the code within the HTML body.
To execute this code, add this snippet to the appDelegate. Application.ExternalEval() will inject the code when the unity player loads and when the script is called. This can be done at the beginning (in appDelegate) or at a later time when it is needed. For my purposes, it was done at the very beginning because we have code that is dependent on this injection being completed before hand.
If the source code being injected is from an external source, I recommend conducting the injection as early as possible, because you will not know when the source injection has been completed. (Technically you can know when the source completes, but will take more setup by using callbacks from the webplayer. Research Unity Web Player and browser communication.)
EDIT: Additional information.