深层链接是直接指向应用程序内部内容的链接。Unity 使用 Application.absoluteURL
属性和 Application.deepLinkActivated
事件在以下平台上支持深层链接:
通过深层链接 URL 来激活应用程序时,Unity 会调用 Application.deepLinkActivated
事件。要在这种情况下处理深层链接,您可以:
Application.absoluteURL
。Application.deepLinkActivated
事件。例如,可以将以下代码附加到位于启动场景中的游戏对象:
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))
{
// 冷启动而且 Application.absoluteURL 不为 null,因此处理深层链接。
onDeepLinkActivated(Application.absoluteURL);
}
// 初始化 DeepLink Manager 全局变量。
else deeplinkURL = "[none]";
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
private void onDeepLinkActivated(string url)
{
// 更新 DeepLink Manager 全局变量,以便可以从任何位置访问 URL。
deeplinkURL = url;
// 解码 URL 以确定操作。
// 在此示例中,应用程序期望链接的格式如下:
// unitydl://mylink?scene1
string sceneName = url.Split("?"[0])[1];
bool validScene;
switch (sceneName)
{
case "scene1":
validScene = true;
break;
case "scene2":
validScene = true;
break;
default:
validScene = false;
break;
}
if (validScene) SceneManager.LoadScene(sceneName);
}
}
配置应用程序来对特定 URL 进行响应的过程取决于具体平台。
有两种方法可以让应用程序对深层链接做出反应:URL 方案和通用链接。
要添加 URL 方案,请遵循以下步骤:
unitydl
)。这样使应用程序可以打开任何以 unitydl://
开头的链接,并允许您处理 Application.deepLinkActivated
事件中的 URL。
无法将应用程序设置为使用 Unity Editor 中的通用链接,因为这需要外部网站。有关更多信息,请参阅有关启用通用链接 (Enabling Universal Links) 的 Apple 文档。
要启用深层链接,需要设置一个覆盖标准应用程序清单的特制过滤器,以便为活动 (Activity) 包含特定的 intent-filter
部分。
最简单的方法是将以下 AndroidManifest.xml
文件放入项目的 Assets/Plugins/Android
文件夹中。构建应用程序时,Unity 将自动处理此文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<application>
<activity android:name="com.unity3d.player.UnityPlayerActivity" android:theme="@style/UnityThemeSelector" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="unitydl" android:host="mylink" />
</intent-filter>
</activity>
</application>
</manifest>
这样使应用程序可以打开任何以 unitydl://
开头的链接,并允许您处理 Application.deepLinkActivated
事件中的 URL。
要将自定义 URI 方案添加到应用程序,请按照以下步骤操作:
unitydl
。这样使应用程序可以打开任何以 unitydl://
开头的链接。
要测试深层链接,可以创建一个 HTML 文件,将此文件托管在本地 Web 服务器上,然后从设备上的 Web 浏览器访问这个文件:
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
</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>
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.