# 深度链接

> Explains about enabling deep linking within your application and the platforms that support this feature.

深度链接是超链接，可将用户带到应用程序中的特定位置而不是网站。当用户选择深度链接时，应用程序将在指定位置打开，例如 Unity 应用程序中的特定场景。Unity 使用 [Application.absoluteURL](/engine/6000.0/script-reference/unity-engine/application/absolute-url.md) 属性和 [Application.deepLinkActivated](/engine/6000.0/script-reference/unity-engine/application/deep-link-activated.md) 事件来支持深度链接。

## 启用深度链接##enable-deep-linking

在处理深度链接之前，需要配置应用程序以对其做出反应。配置应用程序来对特定 URL 进行响应的过程取决于具体平台。Unity 支持以下平台的深度链接：

* [iOS](/engine/6000.0/manual/platform-specific/iphone/ios-developing/deep-linking-ios.md)
* [Android](/engine/6000.0/manual/platform-specific/android/developing/deep-linking.md)
* [通用 Windows 平台](/engine/6000.0/manual/platform-specific/windows-store/uwp-developing/deep-linking-universal-windows-platform.md)
* [macOS](/engine/6000.0/manual/platform-specific/apple-mac/macosdevelopment/deep-linking-macos.md)

## 使用深度链接##use-deep-links

根据应用程序的当前状态，有两种处理深度链接的方法：

* 应用程序未运行：设备会打开应用程序，而 [Application.absoluteURL](/engine/6000.0/script-reference/unity-engine/application/absolute-url.md) 会存储设备传递给应用程序的深度链接 URL。
* 应用程序已运行：设备将 URL 传递给应用程序，并使用深度链接 URL 作为参数调用 [Application.deepLinkActivated](/engine/6000.0/script-reference/unity-engine/application/deep-link-activated.md) 事件。[Application.absoluteURL](/engine/6000.0/script-reference/unity-engine/application/absolute-url.md) 同时更新。

在这两种情况下，使用 [Application.absoluteURL](/engine/6000.0/script-reference/unity-engine/application/absolute-url.md) 并选择要在应用程序中打开的场景。

以下代码示例展示了如何根据应用程序的当前状态处理深度链接：

```lang-csharp
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);
    }
}
```

## 测试深度链接##test-a-deep-link

要测试深度链接，请执行以下步骤：

1. 创建一个包含要测试的深度链接的 [HTML 文件](#testing-example-html-file)。
2. 在本地 Web 服务器上托管 HTML 文件。
3. 从设备上的 Web 浏览器打开 HTML 文件，然后单击深度链接。

### HTML 文件示例##example-html-file

这是一个 HTML 文件示例，可用于测试深度链接。要重定向链接，请更改其中一个 `<a>` 元素中的 `href` 属性。

```lang-html
<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>
```
