Desktop

The HTML page that contains Unity Web Player content can communicate with that content and vice versa. Basically there are two communication directions:

HTML页面Unity网络播放器内容可以和浏览器通信,反之亦然。主要有2个通信方向:

The web page calls functions inside the Unity web player content.

网页在Unity网络播放器内容中调用函数。

The Unity web player content calls functions in the web page.

Unity网络播放器内容在网页调用函数。

Each of these communication directions is described in more detail below.

下面详细描述每个通信方向。

Calling Unity web player content functions from the web page

从网页调用Unity网络播放器内容函数。

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.

Unity网络播放器对象有一个函数SendMessage(),可以从网页在Unity网络播放器内容中调用这个函数。这个函数非常相似Unity脚本API中的GameObject.SendMessage函数。当从网页调用时,传递一个对象名,一个函数名和一个参数,并且SendMessage()将在给定的游戏物体调用给定的函数。

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 unityObject.getObjectById() function to obtain a reference to the object. Here is an example JavaScript function that would execute the SendMessage() function on the Unity web player content with placeholder id value of UnityContent; in turn SendMessage() will then call the function MyFunction() on the game object named MyObject, passing a piece of string data as an argument:

为了调用Unity网络播放器的SendMessage()函数,你必须首先获得一个一个引用到Unity网络播放器对象。可以使用unityObject.getObjectById()函数以获取一个引用到对象。下面是一个JavaScript函数例子,在带有占位符ID,UnityContent的值的Unity网络播放器内容,将执行SendMessage()函数。反过来SendMessage()将在名为MyObject的游戏物体调用MyFunction()函数,传递字一段符串数据作为参数。

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:

在Unity网络播放器内容中必须有一个脚本附加到名为MyObject的游戏物体,并该脚本需要执行名为MyFunction的函数:

function MyFunction(param : String)

{

Debug.Log(param);

}

Note: keep in mind that if the function doesn't have any arguments, then an empty string ("") should be passed as an argument.

注意:如果函数没有任何参数,将传递一个空字符串作为参数。

A single string, integer or float argument must be passed when using SendMessage(), the parameter is required on the calling side. If you don't need it then just pass a zero or other default value and ignore it on the Unity side. Additionally, the game object specified by the name can be given in the form of a path name. For example, /MyObject/SomeChild where SomeChild must be a child of MyObject and MyObject must be at the root level due to the '/' in front of its name.

当使用SendMessage()时,必须传递入一个字符串、整数或浮点数参数,这个参数是必须的。如果你并不需要它那么仅传递0或其他默认的值,Unity将忽略它。此外,通过名称指定的游戏物体可以是给定的路径名形式。例如,/MyObject/SomeChild,SomeChild必须是MyObject的子物体,并且MyObject必须在根目录,因为它的名称前面有字符'/'。

Calling web page functions from Unity web player content

从Unity网络播放器内容调用网页函数

In order to call a web page function from within your Unity web player content you must use the Application.ExternalCall() function. Using that function you can call any JavaScript function defined in the web page, passing any number of parameters to it. Here is an example Unity script that uses the Application.ExternalCall() function to call a function named SayHello() found within the web page, passing a piece of string data as an argument:

为了从Unity网络播放器内容中调用网页函数,必须使用Application.ExternalCall()函数。使用该函数可以调用任意网页中定义的JavaScript函数,传递任意参数给它。这儿是Unity脚本的一个例子,使用Application.ExternalCall()函数来调用网页中名为SayHello()的函数。

Application.ExternalCall( "SayHello", "The game says hello!" );

The web page would need to define the SayHello() function, for example:

网页中必须定义SayHello()函数,例如:

Executing arbitrary browser code from Unity web player content

从Unity网络播放器内容执行任意浏览器的代码

You don't even have to define functions in the embedding web page, instead you can use the Application.ExternalEval() function to execute arbitrary browser code from the web player content.

你甚至无需在嵌入的网页定义函数,反而可以使用Application.ExternalEval()函数从网络播放器内容来执行任意浏览器代码。

The following example checks that the page embedding the web player content is fetched from a certain host (unity3d.com), if that's not the case then it will redirect to another URL. This technique can be used to prevent deep linking to your web player content:

下面的例子检查,页面嵌入的网络播放器内容是否取自某个主机(unity3d.com),如果不是,将重定向另一个URL。这种技术可以用来防止深层链接到您的网络播放器的内容:

【深层链接是指绕开被链网页的主页而使用户直接进入其某一个分页的链接方式。】

Application.ExternalEval(

"if(document.location.host != 'unity3d.com') { document.location='http://unity3d.com'; }"

);

unity使用html播放器,Unity Web Player and browser communication Unity网络播放器和浏览器通信...相关推荐

  1. c语言树莓派音乐播放器,使用web端来控制我的树莓派播放音乐

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? web端控制树莓派播放音乐,PHP+NodeJS+mpg123,粗糙版.实现后还可以做个app什么的. 代码 git 用 ...

  2. java web接收tcp_Java多线程实现TCP网络Socket编程(C/S通信)

    开篇必知必会 在前一篇<基于TCP协议网络socket编程(java实现C/S通信)>,实际存在一个问题,如果服务器端在建立连接后发送多条信息给客户端,客户端是无法全部接收的,原因在于客户 ...

  3. php项目网页音乐播放器插件,10个免费开源的JS音乐播放器插件

    10个免费开源的JS音乐播放器插件 三月 24, 2015 评论 (3) Sponsor 音乐播放器在网页设计中有时候会用到,比如一些时尚类.音乐或影视类等项目,但这些网页播放器插件比较少见,所以这里 ...

  4. 安装网络阅读器 - Tiny Tiny RSS Reader

    安装网络阅读器 - Tiny Tiny RSS Reader 我在网上最主要的活动是搜索和用阅读器跟踪热门站点的源. 搜索引擎使用的是Google.阅读器原来有用过鲜果的,后来改用Google阅读器. ...

  5. 适用于64位Windows的Unity 3.4 Web Player

    Update Dec. 4 2012: The unsupported 64-bit Windows web player is not currently in working state in U ...

  6. “failed to update unity web player”失败的问题:

    我们使用浏览器加载unity 3d 文件,不同的网络可能会产生failed to update unity  web player的错误,详细信息为: unity web player update ...

  7. unity 程序win7在进程里运行_Win7系统中unity web player是什么程序

    unity web player是什么程序,可不可以卸载?下面教程给大家详细讲解"unity web player"程序.以及可以不可以卸载!一起去看看吧! 如果你安装好Win7没 ...

  8. Unity3D之Web Player与unity的互相传值交互

    Web Player是Unity3d自带的一个Web端插件.它可以作为普通的网站来用也可以与Unity3d进行交互. 现在先讲unity传向Web player的方法.非常简单只有一句代码: Appl ...

  9. unity 程序win7在进程里运行_win7系统中出现的unity web player是什么程序?

    当我们使用不纯净的win7系统安装包重新安装win7系统的时候,就会有一些不知名的程序运行在系统中,最近就有一位小伙伴问小编,说他的电脑有一个叫做"Unity Web Player" ...

  10. Offline Web Player installers(unity web play 离线版 webplayer)

    原文地址:https://forum.unity.com/threads/offline-web-player-installers.367715/ With the recent announcem ...

最新文章

  1. C/C++ static和const关键字的作用
  2. 咕咚技术总监唐平麟:神策使我们的数据平台成本降低约 75%,迭代效率提升 2~3 倍...
  3. 【双100%解法】剑指 Offer 22. 链表中倒数第k个节点
  4. python判断点在矩形内_判断平面内矩形和圆是否有交点的python实现
  5. K-means聚类 —— matlab
  6. 《小学生C++趣味编程》第42课 最小公倍数
  7. 为什么要搭建自己的缓存管理模块?
  8. excel分类_Excel小技巧61:将输入的数字显示为中文
  9. 看 设计模式之策略模式探讨初步 有感,并摘取部份内容,学习之
  10. UNIX操作系统发展史简介
  11. 网页直接加QQ群/QQ好友
  12. 大数据时代,数据的应用场景
  13. ipa shell自动打包
  14. [Anaconda学习]本地查看代理ip,anaconda挂代理
  15. 21-4-21 USB接口
  16. springboot状态机模式
  17. @media 的使用规范
  18. html form表单提交
  19. Python快速学习08:模块的操作
  20. 2021年G1工业锅炉司炉考试试卷及G1工业锅炉司炉模拟考试题

热门文章

  1. Python学习-第一天-函数和模块的使用
  2. React ~ 生命周期
  3. 使用Mesos和Marathon管理Docker集群
  4. input type=text和textarea的区别
  5. java -classpath or -cp 的设置和解释
  6. html5提供类似“JQuery”中操作类名的方法
  7. vue实现一个移动端屏蔽滑动的遮罩层
  8. 用UltraEdit软件替换回车换行的窍门
  9. 解决键盘老是不消失实现delegate委托实例化过程
  10. NSTimer(2)