文档

Unity Engine

Unity Engine

深度链接

Explains about enabling deep linking within your application and the platforms that support this feature.
阅读时间3 分钟最后更新于 1 个月前

深度链接是超链接,可将用户带到应用程序中的特定位置而不是网站。当用户选择深度链接时,应用程序将在指定位置打开,例如 Unity 应用程序中的特定场景。Unity 使用 Application.absoluteURL 属性和 Application.deepLinkActivated 事件来支持深度链接。

启用深度链接

在处理深度链接之前,需要配置应用程序以对其做出反应。配置应用程序来对特定 URL 进行响应的过程取决于具体平台。Unity 支持以下平台的深度链接:
根据应用程序的当前状态,有两种处理深度链接的方法:
在这两种情况下,使用 Application.absoluteURL 并选择要在应用程序中打开的场景。
以下代码示例展示了如何根据应用程序的当前状态处理深度链接:
using UnityEngine;using UnityEngine.SceneManagement;public class ProcessDeepLinkMngr : MonoBehaviour{ public static ProcessDeepLinkMngr Instance { get; private set; } public string deeplinkURL; private void Awake() { if (Instance == null) { Instance = this; Application.deepLinkActivated += onDeepLinkActivated; if (!string.IsNullOrEmpty(Application.absoluteURL)) { // Cold start and Application.absoluteURL not null so process Deep Link. onDeepLinkActivated(Application.absoluteURL); } // Initialize DeepLink Manager global variable. else deeplinkURL = "[none]"; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } } private void onDeepLinkActivated(string url) { // Update DeepLink Manager global variable, so URL can be accessed from anywhere. deeplinkURL = url; // Decode the URL to determine action. // In this example, the application expects a link formatted like this:// unitydl://mylink?scene1 string sceneName = url.Split('?')[1]; bool validScene; switch (sceneName) { case "scene1": validScene = true; break; case "scene2": validScene = true; break; default: validScene = false; break; } if (validScene) SceneManager.LoadScene(sceneName); }}
要测试深度链接,请执行以下步骤:
  1. 创建一个包含要测试的深度链接的 HTML 文件。
  2. 在本地 Web 服务器上托管 HTML 文件。
  3. 从设备上的 Web 浏览器打开 HTML 文件,然后单击深度链接。

HTML 文件示例

这是一个 HTML 文件示例,可用于测试深度链接。要重定向链接,请更改其中一个
<a>
元素中的
href
属性。
<html> <head> <meta charset="utf-8"> </head> <body > <h1>My Deep Link Test page</h1> <p><a href="unitydl://mylink">Launch</a></p> <p><a href="unitydl://mylink?parameter">Launch with Parameter</a></p> </body></html>