url | 要打开的 URL。 |
遵循应用程序当前平台和环境的权限和限制,打开指定 URL。这采用不同方式进行处理(具体取决于 URL 的性质),并具有不同的安全限制(具体取决于运行时平台)。
注意:此方法不仅可以用于打开网页,因此具有必须了解的重要安全隐患。
最常见的是,此方法用于打开 HTTP(网页)URL。如果提供网页地址作为此方法的参数,则网页会在默认浏览器中打开。它还会将浏览器应用程序带到前台。
除了用于网页地址的 HTTP 协议,URL 中还可以使用其他类型的协议,如文件传输 (FTP)、电子邮件 (mailto)、数据库访问 (JDBC) 以及可能特定于某些平台的许多其他协议。在某些平台上,可以在 Unity 中使用 OpenURL 执行许多不同类型的任务。
因此,OpenURL 命令可能会异常强大。在某些平台上,能够通过平台和安全沙盒所支持的任何协议打开本地文件、运行命令或打开连接。
OpenURL 方法使用与应用程序本身相同的权限来运行。例如,如果应用程序作为 WebGL 播放器在桌面 Web 浏览器中运行,则无法访问计算机上的本地文件,因为 WebGL 平台本身在阻止该操作的安全沙盒内运行。如果是以其他平台为目标(如独立 EXE 应用程序),则应用程序运行时的安全限制较少并且没有安全沙盒,因此此方法更加强大。
因此,必须非常小心不要向此函数提供字符串,这可能会由第三方恶意制作或修改。
在独立平台上,应考虑此方法具有与 eval 类型函数(在许多其他编程语言中存在)相似的安全隐患。
如果应用程序使用 OpenURL 打开来自第三方或使用任何用户提供的数据组合在一起的 URL 字符串,则用户提供的数据应视为不受信任,可能用于在应用程序本身的相同权限下运行任意代码。
必须净化不受信任的数据并验证它是否为应用程序的预期输入。
Android:由于 Android 7.0 中的安全更改(更多信息),Application.OpenURL 无法再用于打开本地应用程序文件,需要使用 FileProvider,这可用于将文件与其他应用程序共享。
iOS:Application.OpenURL 无法用于打开本地文件。
示例:
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { void Start() { Application.OpenURL("http://unity3d.com/"); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.