Legacy Documentation: Version 5.6 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Application.ExternalEval

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function ExternalEval(script: string): void;
public static void ExternalEval(string script);

Parameters

script The Javascript function to call.

Description

Execution of a script function in the contained web page.

The HTML Javascript host page can have functions executed. The ExternalEval function specifies which function should be executed.

The example shows how HTML scripts can be called from the game code. Two functions are created in the HTML page, jsFunc1 and jsFunc2. These functions can be called by clicking the buttons in the game code. The first button calls jsFunc1 which changes the h1 header. The second button causes the HTML jsFunc2 function to call back into the WebGL script which changes a text string.

#pragma strict
// Buttons that create calls into the HTML.
var webMessage: String = "Unity";
function Start() {
	var cam: Camera = GetComponent.<Camera>();
	cam.clearFlags = CameraClearFlags.SolidColor;
}
function OnGUI() {
	if (GUI.Button(new Rect(20, 20, 150, 30), "Change HTML header")) {
		Application.ExternalEval("jsFunc1()");
	}
	if (GUI.Button(new Rect(20, 60, 150, 30), "Change WebGL string")) {
		Application.ExternalEval("jsFunc2()");
	}
	GUI.Label(new Rect(20, 100, 200, 30), webMessage);
}
function ExampleFunction(s: String) {
	webMessage = webMessage + s;
}
// Buttons that create calls into the HTML.
using UnityEngine;

public class ExampleClass : MonoBehaviour { string webMessage = "Unity";

void Start() { Camera cam = GetComponent<Camera>(); cam.clearFlags = CameraClearFlags.SolidColor; }

void OnGUI() { if (GUI.Button(new Rect(20, 20, 150, 30), "Change HTML header")) { Application.ExternalEval("jsFunc1()"); }

if (GUI.Button(new Rect(20, 60, 150, 30), "Change WebGL string")) { Application.ExternalEval("jsFunc2()"); }

GUI.Label(new Rect(20, 100, 200, 30), webMessage); }

void ExampleFunction(string s) { webMessage = webMessage + s; } }

The following example is the HTML Javascript code that is called into by the WebGL Unity script above. The jsFunc1 and jsFunc2 script functions are called by the Unity script as described above. The jsFunc1 changes the HTML h1 header. The jsFunc2 responses back to the calling WebGL ExampleFunction which changes the message.

#pragma strict
// Note this is presented as a Unity script comment, but is an HTML page
/*<!DOCTYPE html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Unity WebGL Player Example</title>
    <link rel="shortcut icon" href="TemplateData/favicon.ico">
    <link rel="stylesheet" href="TemplateData/style.css">
    <script src="TemplateData/UnityProgress.js"></script>
    <script src="Build/UnityLoader.js"></script>
    <script>
      var gameInstance = UnityLoader.instantiate("gameContainer", "Build/test.json", {onProgress: UnityProgress});
    </script>
  </head>
  <body>
    <h1 id="unityExample">Unity</h1>
    <div class="webgl-content">
      <div id="gameContainer" style="width: 600px; height: 400px"></div>
    </div>
    <script>
      function jsFunc1( )
      {
        document.getElementById('unityExample').innerHTML = 'Unity Example';
      }

function jsFunc2( ) { gameInstance.sendMessage('MainCamera', 'ExampleFunction', ' Example'); } </script> </body> </html> */
// Note this is presented as a Unity script comment, but is an HTML page
/*<!DOCTYPE html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Unity WebGL Player Example</title>
    <link rel="shortcut icon" href="TemplateData/favicon.ico">
    <link rel="stylesheet" href="TemplateData/style.css">
    <script src="TemplateData/UnityProgress.js"></script>
    <script src="Build/UnityLoader.js"></script>
    <script>
      var gameInstance = UnityLoader.instantiate("gameContainer", "Build/test.json", {onProgress: UnityProgress});
    </script>
  </head>
  <body>
    <h1 id="unityExample">Unity</h1>
    <div class="webgl-content">
      <div id="gameContainer" style="width: 600px; height: 400px"></div>
    </div>
    <script>
      function jsFunc1( )
      {
        document.getElementById('unityExample').innerHTML = 'Unity Example';
      }

function jsFunc2( ) { gameInstance.sendMessage('MainCamera', 'ExampleFunction', ' Example'); } </script> </body> </html> */

Note: The web player is not supported from 5.4.0 onwards.

See Also: Application.ExternalCall.