- 最后登录
- 2013-9-29
- 注册时间
- 2012-8-20
- 阅读权限
- 90
- 积分
- 6371
- 纳金币
- 6372
- 精华
- 0
|
Calling Unity web player content functions from the web page
The Unity Web Player object has a function, SendMessage(), that can be called from a web page in order to call functions within Unity web player content. This function is very similar to the GameObject.SendMessage function in the Unity scripting API. When called from a web page you pass an object name, a function name and a single argument, and SendMessage() will call the given function in the given game object.
In order to call the Unity Web Player's SendMessage() function you must first get a reference to the Unity web player object. You can use the GetUnity() function in the default html generated by Unity to obtain a reference to the object. Here is an example JavaScript function that would***cute the SendMessage() function on the Unity web player; in turn SendMessage() will then call the function MyFunction() on the game object named MyObject, passing a piece of string data as an argument:
<script type="text/javascript" language="javascript"> <!-- function SaySomethingToUnity() { var unity = GetUnity(); unity.SendMessage("MyObject", "MyFunction", "Hello from a web page!"); } --> </script>
Inside of the Unity web player content you need to have a script attached to the GameObject named MyObject, and that script needs to implement a function named MyFunction:
function MyFunction(param : String) { Debug.Log(param); }
|
|