Version: 5.6
public static void ExternalEval (string script);

パラメーター

script 呼び出す Javascript 関数

説明

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.

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

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